<?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 "item".
 *
 * The followings are the available columns in table 'item':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $status
 * @property integer $created_by
 * @property string $created_date
 * @property integer $last_edited_by
 * @property string $last_edited_date
 * @property integer $location_id
 *
 * The followings are the available model relations:
 * @property Location $location
 * @property ItemRequestDetail[] $itemRequestDetails
 */
class Item extends LsmsActiveRecord {

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

    /**
     * @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, location_id', 'required'),
	    array('name', 'unique'),
	    array('location_id', 'numerical', 'integerOnly' => true),
	    array('description', 'length', 'max' => 255),
	    array('name', 'length', 'max' => 50),
	    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, description, status, created_by, created_date, last_edited_by, last_edited_date, location_id', '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(
	    'location' => array(self::BELONGS_TO, 'Location', 'location_id'),
	    'itemRequestDetails' => array(self::HAS_MANY, 'ItemRequestDetail', 'item_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'name' => Yii::t('default', 'Name'),
	    'description' => Yii::t('default', 'Description'),
	    'status' => Yii::t('default', 'Status'),
	    'location_id' => Yii::t('default', 'Location'),
	);
    }

    /**
     * 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('name', $this->name, true);
	$criteria->compare('description', $this->description, true);
	$criteria->compare('status', $this->status);
	$criteria->compare('location_id', $this->location_id);

	$pageSize = $this->yiiparam('pageSize');
	return new CActiveDataProvider($this, array(
	    'pagination' => array('pageSize' => $pageSize,),
	    'criteria' => $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 Item the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

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

    /**
     * The Following function is responsible for return array of all Items
     *
     * @return Item[] array of Item model .
     */
    public static function getNameLists($location_id, $item_name) {
	if ($location_id != null && $location_id != "") {
	    $model = Item::model();
	    $criteria = new CDbCriteria;
	    $criteria->select = 'name';
	    $criteria->order = 'name';
	    if ($item_name != null && $item_name != "")
		$criteria->condition = "status = 'Active' and location_id=" . $location_id." and name like '%".$item_name."%'";
	    else
		$criteria->condition = "status = 'Active' and location_id=" . $location_id;
	    return $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryColumn();
	}
	return array();
    }

    /**
     * The Following function is responsible for return array of all Items to display in drop dwon
     *
     * @return Item[] array of Item model .
     */
    public static function getNamesForDropDown() {
	$model = Item::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['name']] = $res['name'];
	}
	return $names;
    }

    /**
     * The Following function is responsible for return id of specific item given its name
     *
     * @return Integer the id of Item model .
     */
    public static function getIdByName($name) {
	$model = Item::model();
	$criteria = new CDbCriteria;
	$criteria->select = 'id';
	$criteria->condition = "name = '" . $name . "'";
	return $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryScalar();
    }

}
