<?php

/**
 * This is the model class for table "suggestions".
 *
 * The followings are the available columns in table 'suggestions':
 * @property integer $id
 * @property string $name
 * @property integer $mobile
 * @property string $type
 * @property string $mail
 * @property integer $phone
 * @property string $call_time
 * @property string $note_date
 * @property string $place
 * @property string $relat
 * @property string $message
 * @property string $other_operation_name
 */
class Suggestions extends CActiveRecord
{
	// const WRONGNAME = 0;
	// const RIGHTNAME = 1;

	public $verifyCode;
	public $startDate;
	public $endDate;

	// public $other_operation_name;

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

	/**
	 * @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, mobile,note_date ,place,  message', 'required'),
			array('mobile, phone', 'numerical', 'integerOnly'=>true),
			array('name, mail, call_time, place, relat', 'length', 'max'=>200),
			array('type', 'length', 'max'=>100),
			array('verifyCode','captcha','allowEmpty'=>false),
			array('other_operation_name','safe'),
			array('mail','email'),
			array('note_date', 'compare', 'compareValue' => date("Y-m-d"), 'operator' => '<=','message' => Yii::t('suggestion','Note Date must be less than or equal to date that you entered')),
			array('startDate, endDate','safe'),
			// array('name','rightName'),
			// array('name','namePattern','truePattern'=>self::RIGHTNAME),
			 array('name','match', 'pattern' => '/^[\p{Arabic}a-zA-Z\- .ـ]+$/u' ,'message' => Yii::t('suggestion','Name_must_be_letters') ),
			// array('name','match','pattern'=> '^[a-zA-Z|\u0621-\u064A\u0660-\u0669 ]+$' ,'message' => 'name must be letters' ),
			// array('mobile','match','pattern'=>'/([\+]{0,4}){9}|(0|9){9}/','message' => 'mobile number is wrong'),
			//  array('phone','match','pattern'=>'/^((?!(0))[0-9]{7})$/' ,'message' => 'phone number is wrong'),

			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, mobile, type, mail, phone, call_time, note_date, place, relat, message,startDate, endDate, other_operation_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	* check if the user name is letters
	* This is the 'namePattern' validator as declared in rules().
	*/
	// public function rightName($attribute)
	// {
	// 	// if ($params['truePattern'] === self::WRONGNAME)
	// 	// 	$pattern = '[0-9]*'; 
	// 	// elseif ($params['truePattern'] === self::WRONGNAME)
	// 		// $pattern = '/^[a-zA-Z|\u0621-\u064A\u0660-\u0669 ]+$/';
	// 		$pattern = '/\p{Arabic}/u';
	// 	if(!preg_match($pattern, $this->$attribute))
	// 		$this->addError($attribute, 'Name must be letters');
		

	// }

	/**
	 * @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(
		 'related' => array(self::BELONGS_TO, 'Related', 'relat'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
                			'id' => Yii::t('admin','ID'),
                			'name' => Yii::t('admin','Name'),
                			'mobile' => Yii::t('admin','mobile'),
                			'type' => Yii::t('admin','Type'),
                			'mail' => Yii::t('admin','Mail'),
                			'phone' => Yii::t('admin','Phone'),
                			'call_time' => Yii::t('admin','Call Time'),
                			'note_date' => Yii::t('admin','Note Date'),
                			'place' => Yii::t('admin','Place'),
                			'relat' => Yii::t('admin','Relat'),
                			'message' => Yii::t('admin','Message'),
							'startDate' => Yii::t('admin','startDate'),
							'endDate' => Yii::t('admin','endDate'),
							'other_operation_name' => Yii::t('admin','Other Operation Name'),
		);
	}

	/**
	 * 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('mobile',$this->mobile);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('phone',$this->phone);
		$criteria->compare('call_time',$this->call_time,true);
		$criteria->compare('note_date',$this->note_date,true);
		$criteria->compare('place',$this->place,true);
		$criteria->compare('relat',$this->relat,true);
		$criteria->compare('message',$this->message,true);
		$criteria->compare('other_operation_name',$this->other_operation_name,true);
		//$criteria->addBetweenCondition('note_date', $this->startDate,$this->endDate);

		// $criteria->compare('startDate',$this->startDate,true);
		// $criteria->compare('endDate',$this->endDate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


		public function searchExport($startDate,$endDate)
	{
		// @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('mobile',$this->mobile);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('phone',$this->phone);
		$criteria->compare('call_time',$this->call_time,true);
		$criteria->compare('note_date',$this->note_date,true);
		$criteria->compare('place',$this->place,true);
		$criteria->compare('relat',$this->relat,true);
		$criteria->compare('message',$this->message,true);
		$criteria->addBetweenCondition('note_date', $startDate,$endDate);

		// $criteria->compare('startDate',$this->startDate,true);
		// $criteria->compare('endDate',$this->endDate,true);

		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 Suggestions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
