<?php

/**
 * This is the model class for table "public.departamento".
 *
 * The followings are the available columns in table 'public.departamento':
 * @property integer $id
 * @property string $nombre_dept
 * @property string $fecha_registro
 * @property boolean $blnborrado
 * @property boolean $nivel
 *
 * The followings are the available model relations:
 * @property Jerarquia[] $jerarquias
 */
class Departamento extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'public.departamento';
	}

	/**
	 * @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('nombre_dept, nivel', 'required'),
			array('nivel', 'validarNivel'),
			array('blnborrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nombre_dept, fecha_registro, blnborrado, nivel', '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(
			'jerarquias' => array(self::HAS_MANY, 'Jerarquia', 'id_dept'),
			'funcionarios' => array(self::HAS_MANY, 'Funcionario', 'id_dept'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nombre_dept' => 'Nombre del departamento',
			'fecha_registro' => 'Fecha de registro',
			'blnborrado' => 'Blnborrado',
			'nivel' => 'Nivel',
		);
	}

	/**
	 * 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('nombre_dept',$this->nombre_dept,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('nivel',$this->nivel);

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

	public function validarNivel(){

		if (!empty($this->nivel)) {
			
			$dpto_reg = self::model()->find('nivel=:lvl and blnborrado=false', array(':lvl'=>$this->nivel));

			if (!empty($dpto_reg)) {
				
				$this->addError('nivel', 'Ya existe un departamento con el nivel escogido!');
			}
			
		}
	}

	public static function convertirNombre($dept_name){

		$dept_name = trim($dept_name);
		$dept_name = str_replace(' ', '_', $dept_name);

		return $dept_name;
	}

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