<?php

/**
 * This is the model class for table "branch".
 *
 * The followings are the available columns in table 'branch':
 * @property integer $id
 * @property string $name_ar
 * @property string $name_en
 * @property double $longitude
 * @property double $latitudes
 * @property integer $governorate
 * @property integer $area
 * @property string $address
 * @property string $address_en
 * @property integer $phone
 * @property integer $fax
 * @property integer $atm
 * @property string $type
 * @property integer $sort
 *
 * The followings are the available model relations:
 * @property Governorate $governorate0
 * @property Area $area0
 */
class Branch extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'branch';
	}

	/**
	 * @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_ar, name_en, longitude, latitudes, governorate, area, type', 'required'),
			array('governorate, area, atm, sort', 'numerical', 'integerOnly'=>true),
			array('longitude, latitudes', 'numerical'),
			array('name_ar, name_en', 'length', 'max'=>500),
			array('address, address_en', 'length', 'max'=>300),
			array('phone, fax', 'length', 'max'=>50),
			array('type', 'length', 'max'=>6),
			// '/([\+]{0,4}){9}|(0|9){9}/'
			//  /^0\d{8,9}$/ start with 0
			// ^(?:00|\\+)[0-9\\s.\\/-]{6,20}$
			// ^\d{4}-\d{3}-\d{4}$ (format: xxxx-xxx-xxxx):
			// \d{3}[\-]\d{3}[\-]\d{4} format of: 123-456-7890
			// ^0\d{3}-^((?!(0))[0-9]{7})$
			// ^0\d{3}-^[2-9][0-9]{7}$
			// /^0\d{3}-^[2-9][0-9]{7}$/
			// ^((?!(0))[0-9]{7})$
			// ^[0-9]{3}-[0-9]{7}$
			// ((011)|(021)|(033)|(031)|(041)|(043))-[0-9]{7}$ worked
			// ^(?:0|\(?\+33\)?\s?|0033\s?)[1-79](?:[\.\-\s]?\d\d){4}$
			// \d{3}[\-]\d{3}[\-]\d{4}
			// [0-9]{3}[ -][0-9]{3}[ -][0-9]{4}
			//array('phone','match','pattern'=>'/^[0-9]{3}-[0-9]{6-7}$/' ,'message' => 'phone number is wrong. Please write Phone number like 999-9999999'),
			//array('fax','match','pattern'=>'/^((011)|(021)|(033)|(031)|(041)|(043))-[0-9]{6-7}$/' ,'message' => 'Fax number is wrong. Please write Fax number like 999-9999999'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name_ar, name_en, longitude, latitudes, governorate, area, address, address_en, phone, fax, atm, type', '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(
			'governorate0' => array(self::BELONGS_TO, 'Governorate', 'governorate'),
			'area0' => array(self::BELONGS_TO, 'Area', 'area'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
                			'id' => Yii::t('admin','ID'),
                			'name_ar' => Yii::t('admin','Name Ar'),
                			'name_en' => Yii::t('admin','Name En'),
                			'longitude' => Yii::t('admin','Longitude'),
                			'latitudes' => Yii::t('admin','Latitudes'),
                			'governorate' => Yii::t('admin','Governorate'),
                			'area' => Yii::t('admin','Area'),
							'address' => Yii::t('admin','Address'),
                            'address_en' => Yii::t('admin','Address En'),
                            'phone' => Yii::t('admin','Phone'),
                            'fax' => Yii::t('admin','Fax'),
                            'atm' => Yii::t('admin','Atm'),
							'type' => Yii::t('admin','Type'),
							'sort' => Yii::t('admin','Sort'),
		);
	}

	/**
	 * 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_ar',$this->name_ar,true);
		$criteria->compare('name_en',$this->name_en,true);
		$criteria->compare('longitude',$this->longitude);
		$criteria->compare('latitudes',$this->latitudes);
		$criteria->compare('governorate',$this->governorate);
		$criteria->compare('area',$this->area);
		$criteria->compare('address',$this->address,true);
        $criteria->compare('address_en',$this->address_en,true);
        $criteria->compare('phone',$this->phone,true);
        $criteria->compare('fax',$this->fax,true);
        $criteria->compare('atm',$this->atm);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('sort',$this->sort);

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

	public function getGovernoraterName($id)
	{
		$governoraterName = Governorate::model()->findByPk($id);
		return $governoraterName->name_ar;
	}

	public function getAreaName($id)
	{
		$areaName=Area::model()->findByPk($id);
		return $areaName->name_ar;
	}
}
