<?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 "cca".
 *
 * The followings are the available columns in table 'cca':
 * @property integer $id
 * @property string $full_name
 * @property string $mobile
 * @property string $email
 * @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 Cca extends TidansActiveRecord {

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

    /**
     * @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('full_name, status', 'required'),
            array('full_name', 'length', 'max' => 150),
            array('full_name', 'unique'),
            array('mobile', 'length', 'max' => 50),
            array('email', 'length', 'max' => 255),
            array('email', 'email'),
            array('status', 'length', 'max' => 8),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, full_name, mobile, email, 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', 'cca_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'full_name' => Yii::t('default', 'Full Name'),
            'mobile' => Yii::t('default', 'Mobile'),
            'email' => Yii::t('default', 'Email'),
            '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() { // @todo Please modify the following code to remove attributes that should not be searched.
        $criteria = new CDbCriteria;

        //$criteria->compare('id',$this->id);
        $criteria->compare('id', $this->id);
        $criteria->compare('full_name', $this->full_name, true);
        $criteria->compare('mobile', $this->mobile, true);
        $criteria->compare('email', $this->email, 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 Cca the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * The Following function is responsible for return array of all CCAs
     *
     * @return Cca[] array of CCA model .
     */
    public static function getNamesForView() {
        $result = Cca::getFullNamesOfCCA();
        $names = array();
        foreach ($result as $res) {
            $names[$res] = $res;
        }
        return $names;
    }

    /**
     * The Following function is responsible for return array of all CCAs
     *
     * @return CCa[] array of CCA model .
     */
    public static function getFullNamesOfCCA() {
        $model = Cca::model();
        $criteria = new CDbCriteria;
        $criteria->select = 'full_name';
        $criteria->order = 'full_name';
        $criteria->condition = "status = 'Active'";
        return $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryColumn();
    }

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

}
