<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 
/**
 * This is the model class for table "ti_type".
 *
 * The followings are the available columns in table 'ti_type':
 * @property integer $id
 * @property string $name
 * @property integer $validity_days
 * @property string $validity_mode
 * @property string $status
 * @property integer $created_by
 * @property string $created_date
 * @property integer $last_edited_by
 * @property string $last_edited_date
 *
 * The followings are the available model relations:
 * @property TidStatus[] $tidStatuses
 * @author Amaney
 * @package application.models
 */
class TiType extends TidansActiveRecord {

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

    /**
     * @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('name, validity_mode, status', 'required'),
            array('validity_days', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 150),
            array('name', 'unique'),
            array('validity_days', 'validateValidityDays'),
            array('validity_mode', 'length', 'max' => 22),
            array('status', 'length', 'max' => 8),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, validity_days, validity_mode, status', '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(
            'tidStatuses' => array(self::HAS_MANY, 'TidStatus', 'ti_type_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'name' => Yii::t('default', 'Name'),
            'validity_days' => Yii::t('default', 'Validity Days'),
            'validity_mode' => Yii::t('default', 'Validity Mode'),
            'status' => Yii::t('default', 'Status'),
        );
    }

    /**
     * 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 = $this->ModelCriteria();

        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

    /**
     * Get the query used to get the current model information to be used in export to excel action
     * @return CDbCriteria the criteria used to get the current model information
     */
    public function ModelCriteria() {
        $criteria = new CDbCriteria;

        $criteria->compare('name', $this->name, true);
        $criteria->compare('validity_days', $this->validity_days);
        $criteria->compare('validity_mode', $this->validity_mode, true);
        $criteria->compare('status', $this->status);


        return $criteria;
    }

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

    /**
     * Returns the validity mode options
     * 
     * @return Array the validity mode options
     */
    public static function getValidityModeOptions() {
        return array(
            'Permanent' => Yii::t('default', 'Permanent'),
            'Until Contract Ends' => Yii::t('default', 'Until Contract Ends'),
            'Based on Validity Days' => Yii::t('default', 'Based on Validity Days')
        );
    }

    /**
     * Check If the validity mode is "Based on Validity Days" then Validity days must be filled
     */
    public function validateValidityDays($attribute, $params) {
        if ($this->validity_mode == 'Based on Validity Days' && ($this->validity_days == null || $this->validity_days == '')) {
            $this->addError($attribute, Yii::t('default', 'Validity Days can’t be blank'));
        }
    }

    /**
     * The Following function is responsible for return array of all TiType names
     *
     * @return string[] array of names of TiType models .
     */
    public static function getNamesForView() {
        $result = TiType::getNameLists();
        $names = array();
        foreach ($result as $res) {
            $names[$res] = $res;
        }
        return $names;
    }

    /**
     * The Following function is responsible for return array of all TiType names
     *
     * @return string[] array of names of TiType models
     */
    public static function getNameLists() {
        $model = TiType::model();
        $criteria = new CDbCriteria;
        $criteria->select = 'name';
        $criteria->order = 'name';
        $criteria->condition = "status = 'Active'";
        return $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryColumn();
    }

    /**
     * The Following function is responsible for return array of all TiType names to display in drop dwon
     *
     * @return string[] array of names TiType models .
     */
    public static function getNamesForDropDown() {
        $model = TiType::model();
        $criteria = new CDbCriteria;
        $criteria->select = 'id,name';
        $criteria->order = 'name';
        $criteria->condition = "status = 'Active'";
        $result = $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryAll();
        $names = array();
        foreach ($result as $res) {
            $names[$res['id']] = $res['name'];
        }
        return $names;
    }

    /**
     * The Following function is responsible for return the permanent ti type id
     *
     * @return integer the id of Permanent TiType
     */
    public static function getPermanentTiType() {
        $model = TiType::model()->findByAttributes(array('validity_mode' => 'Permanent'));
        if ($model != null)
            return $model->id;
    }

}
