<?php

/**
 * This is the model class for table "public.funcionario".
 *
 * The followings are the available columns in table 'public.funcionario':
 * @property integer $id
 * @property integer $cedula
 * @property string $nombre
 * @property string $apellido
 * @property integer $id_jerarquia
 * @property string $fecha_nacimiento
 * @property string $dir_domic
 * @property string $telefono
 * @property string $fecha_registro
 * @property boolean $blnborrado
 * @property boolean $id_dpto
 *
 * The followings are the available model relations:
 * @property Jerarquia $idJerarquia
 */
class Funcionario extends CActiveRecord
{
	public $dept;

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'public.funcionario';
	}

	/**
	 * @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('cedula, nombre, apellido, id_jerarquia, id_dpto', 'required'),
			array('cedula, id_jerarquia, id_dpto', 'numerical', 'integerOnly'=>true),
			//array('fecha_nacimiento, dir_domic, telefono', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, cedula, nombre, apellido, id_jerarquia, fecha_nacimiento, dir_domic, telefono, fecha_registro, blnborrado, id_dpto', '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(
			'idJerarquia' => array(self::BELONGS_TO, 'Jerarquia', 'id_jerarquia'),
			'idDpto' => array(self::BELONGS_TO, 'Departamento', 'id_dpto'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cedula' => 'Cédula',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'id_jerarquia' => 'Nivel',
			'fecha_nacimiento' => 'Fecha de nacimiento',
			'dir_domic' => 'Dirección de domicilio',
			'telefono' => 'Teléfono',
			'fecha_registro' => 'Fecha de registro',
			'blnborrado' => 'Blnborrado',
			'id_dpto' => 'Departamento'
		);
	}

	/**
	 * 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('cedula',$this->cedula);
		$criteria->compare('UPPER(nombre||apellido)',strtoupper($this->nombre),true);
		//$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('id_jerarquia',$this->id_jerarquia);
		$criteria->compare('fecha_nacimiento',$this->fecha_nacimiento,true);
		$criteria->compare('dir_domic',$this->dir_domic,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('id_dpto',$this->id_dpto);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function findFuncionario($id_funcionario){

		$func = self::model()->find('id=:id and activo=true and blnborrado=false', array(':id'=>$id_funcionario));

		if (!empty($func)) {
			
			return $func;
		}
	}

	/**
	 * 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 Funcionario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
