<?php

/**
 * This is the model class for table "{{mas_designation}}".
 *
 * The followings are the available columns in table '{{mas_designation}}':
 * @property integer $id
 * @property string $designation_name
 * @property string $isActive
 *
 * The followings are the available model relations:
 * @property MasCompany[] $masCompanies
 */
class MasDesignation extends CActiveRecord {

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

    /**
     * @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('designation_name', 'required'),
            array('designation_name', 'length', 'max' => 255),
            array('designation_name', 'unique'),
            array('designation_name', 'filter', 'filter'=>'trim'),
            array('isActive', 'length', 'max' => 1),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, designation_name, isActive', 'safe', 'on' => 'search'),
        );
    }
    function afterSave() {
        $action_type='';
        switch ($this->scenario) {
            case 'add':
                $action_type = 'Created';
                break;
            case 'update':
                $action_type = 'Updated';
                break;
            default:
                $action_type = 'Block/Unblock';
                break;
        }
        $model_audit_log = new HrAuditLog();
        $model_audit_log->functionality = 2;
        $model_audit_log->master_table_id = $this->id;
        $model_audit_log->performed_by = Yii::app()->user->id;
        $model_audit_log->performed_date_time = date('Y-m-d H:i:s');
        $model_audit_log->remarks =$action_type.' designation '.$this->designation_name;
        $model_audit_log->save();
    }

    /**
     * @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',
            'designation_name' => 'Designation',
            'isActive' => 'Is Active',
        );
    }

    /**
     * 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() {
        $criteria = new CDbCriteria;
        $searchterm = trim(Yii::app()->request->getParam('designation_name'));
        $sterm = trim(Yii::app()->request->getParam('search'));
     
        if($sterm)
        {
      $criteria->addCondition('designation_name like "%' . $sterm . '%"', 'AND');
        }
        if (!empty($searchterm)){
            $criteria->addCondition('designation_name like "%' . $searchterm . '%"', 'AND');
        }
        return new CActiveDataProvider($this, array(
            'pagination' => array(
                'pageSize' => 12,
            ),
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 't.designation_name ASC',
            ),
        ));
    }

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

    //function Written by Dikson

    public static function getDesignation() {
        $condition = 'isActive =  "Y"';
        $model = self::model()->findAll(array('condition' => $condition,'order'=>'designation_name asc'));
        return CHtml::listData($model, 'id', 'designation_name');
    }
    public static function getDesignationName() {
        $condition = 'isActive =  "Y" ';
        $model = self::model()->findAll(array('condition' => $condition,'order'=>'designation_name asc'));
        return CHtml::listData($model, 'id', 'designation_name');
    }

    public static function getFiiledDesignationName($id) {
        $model = self::model()->findByPk($id);
        return $model->designation_name;
    }
}
