<?php

/**
 * This is the model class for table "perfil".
 *
 * The followings are the available columns in table 'perfil':
 * @property integer $id
 * @property integer $usuario_id
 * @property string $nombre
 * @property string $departamento
 * @property string $telefono
 * @property string $direccion
 * @property string $fecha_nacimiento
 * @property string $twitter
 */
class Perfil extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'perfil';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('usuario_id','required'),
			array('nombre, departamento, twitter', 'length', 'max'=>100),
                    array('telefono', 'numerical'),
			array('direccion, fecha_nacimiento,telefono,twitter','required'),
                    array('twitter', 'match' ,

    'pattern'=> '/^[A-Za-z0-9_]+$/u',

    'message'=> 'Usuario twitter no debe contener @. '

    ),

			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, usuario_id, nombre, departamento, telefono, direccion, fecha_nacimiento, twitter', 'safe', 'on'=>'search'),
		);
	}
         

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'usuario_id' => 'Usuario',
			'nombre' => 'Nombre',
			'departamento' => 'Departamento',
			'telefono' => 'Telefono',
			'direccion' => 'Direccion',
			'fecha_nacimiento' => 'Fecha Nacimiento',
			'twitter' => 'Twitter',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('usuario_id',$this->usuario_id);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('departamento',$this->departamento,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('fecha_nacimiento',$this->fecha_nacimiento,true);
		$criteria->compare('twitter',$this->twitter,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function buscarcedula()
	{
     if(Yii::app()->user->name==false) {
         return 0;
     }    else{ 
$condition = 'usuario_id=:usuario_id';
$params=array(':usuario_id' => Yii::app()->user->name);
$cedula=Perfil::model()->exists($condition,$params);
if($cedula){
    return 1;
    
}
else{
    return 0;
    
     }};
          
           

		
	}
         public function linkactualizar()
	{
             if(Yii::app()->user->id==FALSE) {
         return '0';
     }    else{ 
         $condition = 'usuario_id=:usuario_id';
$params=array(':usuario_id' => Yii::app()->user->name);
$cedula=Perfil::model()->exists($condition,$params);
           if($cedula){
           $row = Perfil::model()->find('usuario_id=:usuario_id',
                        array(
                          ':usuario_id'=>Yii::app()->user->name,

                        ));
          
      return $row->id;
           }else{return '0';}  
           
     }
		
	}


	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Perfil the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
         protected function afterFind ()
    {

                if($this->fecha_nacimiento <> '')
                {
                        // mise en forme de class_date
            list($y, $m, $d) = explode('-', $this->fecha_nacimiento);
                $mk=mktime(0, 0, 0, $m, $d, $y);
                $this->fecha_nacimiento = date ('d/m/Y', $mk);
                }
        
        return parent::afterFind ();
    }
         protected function beforeSave ()
    {
                if($this->fecha_nacimiento <> '')
                {
                        // mise en forme de class_date
                list($d, $m, $y) = explode('/', $this->fecha_nacimiento);
                $mk=mktime(0, 0, 0, $m, $d, $y);
                $this->fecha_nacimiento = date ('Y-m-d', $mk);
                }
                                
        return parent::beforeSave ();
    } 

}
