<?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 "location".
 *
 * The followings are the available columns in table 'location':
 * @property integer $id
 * @property string $code
 * @property integer $geomarket_id
 * @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 Department[] $departments
 * @property Geomarket $geomarket
 * @property Pos[] $poses
 * @property User[] $users
 */
class Location extends LsmsActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Location the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

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

    /**
     * @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('code, geomarket_id, status', 'required'),
	    array('code', 'unique'),
	    array('geomarket_id', 'numerical', 'integerOnly' => true), //created_by, last_edited_by
	    array('code', 'length', 'max' => 255),
	    array('status', 'length', 'max' => 8),
//			array('created_date, last_edited_date', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, code, geomarket_id, status, created_by, created_date, last_edited_by, last_edited_date', '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(
	    'departments' => array(self::HAS_MANY, 'Department', 'location_id'),
	    'geomarket' => array(self::BELONGS_TO, 'Geomarket', 'geomarket_id'),
	    'poses' => array(self::HAS_MANY, 'Pos', 'location_id'),
	    'users' => array(self::HAS_MANY, 'User', 'location_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'code' => yii::t('default', 'Code'),
	    'geomarket_id' => yii::t('default', 'Geomarket'),
	    'status' => yii::t('default', 'Status'),
	);
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover() {
	return array(
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('code', $this->code, true);
	$criteria->compare('geomarket_id', $this->geomarket_id);
	$criteria->compare('status', $this->status, false);

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

    public static function getAll($status=null) {
	if ($status == "Active") {
	    return Location::model()->active()->findAll(array('order' => 'code'));
	} else {
	    return Location::model()->findAll(array('order' => 'code'));
	}
    }

    /*
     * The Following Function Is resposible for return All Department Alocated in the Location wich user belong to it
     */

    public function getArrayOfDepartments() {
	$result = array();
	foreach ($this->departments as $department) {
	    $result[$department->id] = $department->name;
	}
	return $result;
    }

    /*
     * The Following Function Is resposible for return All POSs Alocated in the Location wich user belong to it
     */

    public function getArrayOfPOSs() {
	$result = array();
	foreach ($this->poses as $pos) {
	    $result[$pos->id] = $pos->name;
	}
	return $result;
    }

    public static function getLocationCode($location_id) {
	$location = Location::model()->findByPk($location_id);
	if ($location != null)
	    return $location->code;
	return null;
    }

}