<?php

/**
* This is the model class for table "paper".
*
* The followings are the available columns in table 'paper':
* @property integer $id_paper
* @property string $created_date
* @property integer $created_id
* @property string $update_date
* @property integer $update_id
* @property integer $researchers_id
* @property string $paper_name
* @property string $paper_image
* @property string $paper_file
* @property integer $status
*/
class Paper extends CActiveRecord
{
	/**
	* @return string the associated database table name
	*/
	public function tableName()
	{
		return 'paper';
	}

	/**
	* @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('paper_name, paper_file, paper_image', 'required', 'on' => 'upload_karyatulis'),
			array('status', 'required', 'on' => 'verified'),
			array('created_id, update_id, researchers_id, status', 'numerical', 'integerOnly' => true),
			array('paper_name', 'length', 'max' => 150),
			array('paper_image, paper_file, status_description', 'length', 'max' => 255),
			array('created_date, update_date', 'safe'),
			array('paper_image', 'file', 'allowEmpty' => true, 'types' => 'ppt, pptx, mp4, pdf, m4a', 'on' => 'upload_cover'),
			array('paper_file', 'file', 'allowEmpty' => true, 'types' => 'pdf, doc, docx', 'on' => 'upload_pdf'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_paper, created_date, created_id, update_date, update_id, researchers_id, paper_name, paper_image, paper_file, status', '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(
			'Researchers' => array(self::BELONGS_TO, 'Users', 'researchers_id'),
		);
	}

	/**
	* @return array customized attribute labels (name=>label)
	*/
	public function attributeLabels()
	{
		return array(
			'id_paper' => 'Id Paper',
			'created_date' => 'Tanggal Verifikasi',
			'created_id' => 'Created',
			'update_date' => 'Tanggal Update',
			'update_id' => 'Update',
			'researchers_id' => 'Nama Tim',
			'paper_name' => 'Nama Karya Tulis',
			'paper_file' => 'File PDF (Karya Tulis)',
			'paper_image' => 'File Paparan (PPT)',
			'status' => 'Verifikasi',
			'status_description' => 'Catatan',
		);
	}

	/**
	* 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_paper', $this->id_paper);
			$criteria->compare('created_date', $this->created_date, true);
			$criteria->compare('created_id', $this->created_id);
			$criteria->compare('update_date', $this->update_date, true);
			$criteria->compare('update_id', $this->update_id);
			$criteria->compare('researchers_id', $this->researchers_id);
			$criteria->compare('paper_name', $this->paper_name, true);
			$criteria->compare('paper_image', $this->paper_image, true);
			$criteria->compare('paper_file', $this->paper_file, true);
			$criteria->compare('status', $this->status);

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

		public static function getNotification()
		{
			$sql = "SELECT status_description FROM paper WHERE status=2 AND researchers_id=" . YII::app()->user->record->id_user;
			$command = Yii::app()->db->createCommand($sql);
			return $command->queryScalar();
		}

		public static function getStatus()
		{
			$sql = "SELECT status FROM user WHERE status=2 AND id_user=" . YII::app()->user->record->id_user;
			$command = Yii::app()->db->createCommand($sql);
			return $command->queryScalar();
		}


		function getYoutubeEmbedUrl($url)
		{
			$shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
			$longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';

			if (preg_match($longUrlRegex, $url, $matches)) {
				$youtube_id = $matches[count($matches) - 1];
			}

			if (preg_match($shortUrlRegex, $url, $matches)) {
				$youtube_id = $matches[count($matches) - 1];
			}
			return 'https://www.youtube.com/embed/' . $youtube_id;
		}

		// public function afterFind()
		// {
			// 	$this->paper_file = Paper::model()->getYoutubeEmbedUrl($this->paper_file);
			// }
	}
