<?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', 'on' => 'crear',),
			array('nombre, departamento, twitter, cargo, tipo_nomina, rif,correo', 'length', 'max'=>100 , 'on' => 'crear',),

			
                    array('usuario_id','required', 'on' => 'crear',),

                    array('direccion', 'length','min'=>30, 'max'=>300, 'on' => 'crear'),

                        array('correo','email'),
                     array('usuario_id', 'required', 'on' => 'buscar',
                         'message'=>'Ingrese un {attribute} valida .'),

                     array('twitter', 'length','min'=>4, 'max'=>100, 'on' => 'crear'),
             
                 //   array('telefono_local,telefono_otro', 'numerical' , 'on' => 'crear',),
                 //   array('telefono', 'numerical' , 'on' => 'crear',),
			array('direccion, fecha_nacimiento,telefono, twitter, fecha_ingreso, cargo, tipo_nomina, telefono_local, rif, correo,gestor','required' , 'on' => 'crear',),
                    array('twitter', 'match' , 'pattern'=> '/^[A-Za-z0-9_]+$/u','message'=> 'Usuario twitter no debe contener @. ', 'on' => 'crear', ),

			// 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, fecha_ingreso,cargo,tipo_nomina,telefono_local,telefono_otro,rif,correo', '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' => 'Cedula',  
			'nombre' => 'Nombre',
                    'fecha_ingreso' => 'Fecha Ingreso',
                    'cargo' => 'Cargo',
                    'tipo_nomina' => 'Tipo Nomina',
                    'Telefono_local' => 'Telefono Local',
                    'telefono_otro' => 'Otro Telefono',
                    'rif' => 'rif',
                    'correo' => 'correo',
			'departamento' => 'Departamento',
			'telefono' => 'Telefono Personal',
			'direccion' => 'Direccion',
			'fecha_nacimiento' => 'Fecha Nacimiento',
			'twitter' => 'Twitter',
				'gestor' => 'Gestor',
                    
                         
		);
	}

	/**
	 * 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;




                        $gerencia = Perfil::model()->find('usuario_id=:usuario_id',
                        array(
                          ':usuario_id'=>Yii::app()->user->name,

                        ));




		$variables=$gerencia['departamento'];
		//$criteria->condition='departamento LIKE "%'.$variables.'%"';

		$criteria->condition = "departamento LIKE :departamento";
		$criteria->params = array(':departamento' => trim($variables) . '%');
  
		$criteria->compare('id',$this->id);
		$criteria->compare('usuario_id',$this->usuario_id);
                   
                $criteria->compare('fecha_ingreso',$this->fecha_ingreso,true);
                      $criteria->compare('cargo',$this->cargo,true);
                      $criteria->compare('tipo_nomina',$this->tipo_nomina,true);
                      $criteria->compare('telefono_local',$this->telefono_local,true);
                      $criteria->compare('telefono_otro',$this->telefono_otro,true);
                      $criteria->compare('rif',$this->rif,true);
                          $criteria->compare('correo',$this->correo,true);
		$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);
			$criteria->compare('gestor',$this->gestor,true);
			

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}




public function buscargerente(){
    
  
     
                       $condition = 'gestor =:gestor and usuario_id=:usuario_id ';
                       $params=array(':usuario_id' => Yii::app()->user->name,':gestor' => 1);
                       $gerente=Perfil::model()->exists($condition,$params);

                if($gerente){ 
                      return 1;                
                      }else{
                     return 0; 
                    }

          
       }
        







        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);
	}
        

}
