<?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 "type_of_asset".
 *
 * The followings are the available columns in table 'type_of_asset':
 * @property integer $id
 * @property string $name
 * @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 Asset[] $assets
 * @property DahsboardAsset[] $dahsboardAssets
 *
 * @author Amaney
 * @package application.models
 */
class TypeOfAsset extends TidansActiveRecord {

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

    /**
     * @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, status', 'required'),
	    array('name', 'length', 'max' => 150),
	    array('name', 'unique'),
	    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, 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(
	    'assets' => array(self::HAS_MANY, 'Asset', 'type_of_asset_id'),
	    'dahsboardAssets' => array(self::HAS_MANY, 'DahsboardAsset', 'type_of_asset_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => Yii::t('default', 'ID'),
	    'name' => Yii::t('default', 'Name'),
	    '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('name', $this->name, 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 TypeOfAsset the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

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

    /**
     * The Following function is responsible for return array of all Type of Asset names
     *
     * @return string[] array of names of Type of Asset models
     */
    public static function getNameLists() {
	$model = TypeOfAsset::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 Type of Asset names to display in drop dwon
     *
     * @return string[] array of names Type of Asset models .
     */
    public static function getNamesForDropDown($type_of_asset_id = null) {
	$model = TypeOfAsset::model();
	$criteria = new CDbCriteria;
	$criteria->select = 'id,name';
	$criteria->order = 'name';
	$criteria->condition = "status = 'Active'";
	if ($type_of_asset_id != null)
	    $criteria->addCondition('id =' . $type_of_asset_id);
	$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 array of all Type of Asset names to display in drop dwon
     *
     * @return string[] array of names Type of Asset models .
     */
    public static function getVehiclRadioForDropDown() {

	$names = array();
	$vehicle_id = Setting::getVehicleTypeOfAsset();
	$radio_active_id = Setting::getRadioActiveTypeOfAsset();
	if ($vehicle_id != null && $vehicle_id != '')
	    $names[$vehicle_id] = TypeOfAsset::getNameById($vehicle_id);

	if ($radio_active_id != null && $radio_active_id != '')
	    $names[$radio_active_id] = TypeOfAsset::getNameById($radio_active_id);

	return $names;
    }

    /**
     * The Following function is responsible for getting the name of the type of asset
     * @param Integer $id the id of the type of asset.
     * @return string the name of the type of asset.
     */
    public static function getNameById($id) {
	$type_of_asset = TypeOfAsset::model()->findByPk($id);
	if ($type_of_asset != null)
	    return $type_of_asset->name;
    }

}
