<?php

/**
* This is the model class for table "post".
*
* The followings are the available columns in table 'post':
* @property integer $id_event
* @property string $created_date
* @property string $title
* @property string $description
* @property string $image
* @property integer $status
* @property string $tags
* @property string $keyword
* @property string $url
* @property integer $views
* @property integer $likes
* @property integer $id_user
* @property integer $id_category
* @property integer $id_tag
*/
class Event extends CActiveRecord
{
	/**
	* Returns the static model of the specified AR class.
	* @param string $className active record class name.
	* @return Post 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 'event';
	}

	/**
	* @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('title, description, status, id_category, id_tag, start_date, start_time', 'required','on'=>'add'),
			array('status', 'required','on'=>'status'),
			array('created_id, update_id, status, views, likes, id_category, id_tag, quota, status_event, status_album, status_streaming, reschedule_id, status_private, status_certificate, id_album', 'numerical', 'integerOnly'=>true),
			array('title, image, tags, keyword, url, location, powered_by, youtube_link, zoom_link, youtube_intro, register_date, no_sertifikat', 'length', 'max'=>255),
			array('lat, lang', 'length', 'max'=>50),
			array('start_date, end_date, start_time, end_time, created_date, update_date, reschedule_description, reschedule_date, register_date, description', 'safe'),
			// array('title','unique'),

			//Validation Image
			array('image', 'required', 'on'=>'changeImage'),
			// array('image', 'file','types'=>'jpg, gif, png','allowEmpty'=>false,'on'=>'create'),

			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_event, created_date, title, description, image, status, id_category, created_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(
			'CreatedBy'=>array(self::BELONGS_TO,'Users','created_id'),
			'Category'=>array(self::BELONGS_TO,'Category','id_category'),
			'Tag'=>array(self::BELONGS_TO,'Tag','id_tag'),
		);
	}

	/**
	* @return array customized attribute labels (name=>label)
	*/
	public function attributeLabels()
	{
		return array(
			'id_event' => 'Post ID',
			'created_id' => 'Publish By',
			'created_date' => 'Tanggal Posting',
			'title' => 'Nama Acara',
			'description' => 'Konten',
			'image' => 'Image',
			'status' => 'Status',
			'views' => 'Views',
			'likes' => 'Likes',
			'tags' => 'Tags',
			'keyword' => 'Keyword',
			'id_tag' => 'Tag',
			'id_category' => 'Kategori',
			'status_album' => 'Gallery',
			'status_event' => 'WJIS',
			'status_streaming' => 'Streaming Event',
			'status_private' => 'Event Privat',
			'start_date' => 'Tanggal Mulai',
			'end_date' => 'Tanggal Berakhir',
			'start_time' => 'Waktu Mulai',
			'end_time' => 'Waktu Akhir',
			'youtube_intro' => 'Link Youtube (Teaser)',
		);
	}

	/**
	* 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('id_event',$this->id_event);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('id_category',$this->id_category);
		$criteria->compare('id_tag',$this->id_tag);
		if(YII::app()->user->record->level==3){
			$criteria->compare('created_id',YII::app()->user->id,true);
		}

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'sort' => array(
				'defaultOrder' => 'created_date DESC',
			),
		));
	}

	protected function beforeSave()
	{
		$this->url = preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($this->title))));
		$this->created_date = date('Y-m-d', strtotime($this->created_date));
		// $this->start_date = date('Y-m-d', strtotime($this->start_date));
		// $this->end_date = date('Y-m-d', strtotime($this->end_date));
		return TRUE;
	}

	protected function afterFind()
	{
		$this->created_date = date('d-m-Y', strtotime($this->created_date));
		// $this->start_date = date('d F Y', strtotime($this->start_date));
		// $this->end_date = date('d F Y', strtotime($this->end_date));
		return TRUE;
	}

	public function status($a)
	{
		if($a==1){
			return "Approve";
		}elseif($a==2){
			return "Reject";
		}else{
			return "Waiting";
		}
	}

	public static function getLastPost(){
		$sql = "SELECT a.*, a.title as title, a.created_date as created_date, a.image as image, b.name as category, c.name as tag, a.views as views, a.url as url FROM event as a LEFT JOIN category as b ON a.id_category=b.id_category LEFT JOIN category_sub as c ON a.id_tag=c.id_category_sub WHERE a.status=1 AND start_date>=".date('Y-m-d')." AND end_date>=".date('Y-m-d')." AND a.status_private=0 ORDER BY a.created_date DESC LIMIT 6";
		$command = YII::app()->db->createCommand($sql);
		return $command->queryAll();
	}

	public static function getPopular(){
		$sql = "SELECT a.title as title, a.created_date as created_date, a.image as image, b.name as category, a.views as views, a.url as url FROM event LEFT JOIN category ON a.id_category=b.id_category WHERE a.status=1 AND a.status_private=0 ORDER BY views DESC LIMIT 3";
		$command = YII::app()->db->createCommand($sql);
		return $command->queryAll();
	}

	public static function getRandomPost(){
		$sql = "SELECT a.title as title, a.created_date as created_date, a.image as image, b.name as category, a.views as views, a.url as url FROM event LEFT JOIN category ON a.id_category=b.id_category WHERE a.status=1 ORDER BY RAND() DESC LIMIT 1";
		$command = YII::app()->db->createCommand($sql);
		return $command->queryAll();
	}

	public static function getLive(){
		$today = date('Y-m-d');
		$sql = "SELECT a.*, a.title as title, a.created_date as created_date, a.image as image, b.name as category, c.name as tag, a.views as views, a.url as url FROM event as a LEFT JOIN category as b ON a.id_category=b.id_category LEFT JOIN category_sub as c ON a.id_tag=c.id_category_sub WHERE a.status=1 AND start_date='".$today."' OR end_date='".$today."' ORDER BY a.start_date DESC";
		$command = YII::app()->db->createCommand($sql);
		return $command->queryAll();
	}

	public function seo($title){
		return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));
	}

	public function thumbSmall($imagefile){
		$image = new EasyImage(Yii::getPathOfAlias('webroot').'/image/news/big/'.$imagefile);
		$image->resize(128, 128);
		$image->save(Yii::getPathOfAlias('webroot').'/image/event/small/'.$imagefile);
	}

	public function thumbMiddle($imagefile){
		$image = new EasyImage(Yii::getPathOfAlias('webroot').'/image/news/big/'.$imagefile);
		$image->resize(480, 480);
		$image->save(Yii::getPathOfAlias('webroot').'/image/event/middle/'.$imagefile);
	}

	public function readmore($string){
		if (strlen($string) > 320) {
			$trimstring = substr($string, 0, 320)."...";
		} else {
			$trimstring = $string;
		}
		return strip_tags($trimstring);
	}

	public function dateFormat($data){
		return Yii::app()->dateFormatter->formatDateTime($data, 'medium', false);;
	}

	public function timeFormat($data){
		return substr($data,0,5);
	}

	public function dateEvent($data){
		$model = Event::model()->findBypk($data);
		if($model->end_date==''){
			return $this->dateFormat($model->start_date);
		}elseif($model->start_date==$model->end_date){
			return $this->dateFormat($model->start_date);
		}else{
			return $this->dateFormat($model->start_date) . " - " . $this->dateFormat($model->end_date);
		}
	}

	public function timeEvent($data){
		$model = Event::model()->findBypk($data);
		if($model->end_time==''){
			return $this->timeFormat($model->start_time);
		}else{
			return $this->timeFormat($model->start_time) . " - " . $this->timeFormat($model->end_time);
		}
	}

	public static function generateColor() {
		$dt = '';
		for($o=1;$o<=3;$o++)
		{
			$dt .= str_pad( dechex( mt_rand( 0, 127 ) ), 2, '0', STR_PAD_LEFT);
		}
		return '#'.$dt;
	}

}