<?php

/**
 * This is the model class for table "page".
 *
 * The followings are the available columns in table 'page':
 * @property integer $id
 * @property string $title
 * @property string $slug
 * @property string $content
 * @property string $language
 * @property string $layout
 * @property string $en_title
 * @property string $en_content
 * @property string $attachment_url
 * @property string $attachment_title
 * @property string $meta
 
 */
class Page extends FlexActiveRecord {

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

    protected function beforeSave() {
        if (strlen($this->slug) == 0) {
            $this->slug = Common::sanitize($this->title);
        }
        return parent::beforeSave();
    }

    /**
     * @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,content, layout, ', 'required'),
            array('title, en_title', 'length', 'max' => 200),
            array('slug', 'length', 'max' => 80),
            array('en_content, meta', 'safe'),
			 array('image', 'file', 'types' => 'jpg,jpeg,png,gif', 'maxSize' => 1024 * 1024 * 15 /* 15MB */, 'allowEmpty' => true, 'safe' => true),
            array('language', 'length', 'max' => 2),
            array('layout', 'length', 'max' => 45),
            array('attach','file', 'types' => 'pdf', 'maxSize' => 1024 * 1024 * 15 /* 15MB */, 'allowEmpty' => true, 'safe' => true),
            array('attachment_url, attachment_title', 'length', 'max'=>100),
            array('en_content', 'safe'),
            array('slug+language', 'ext.validators.uniqueMultiColumnValidator',),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, title, slug, content, language, layout, en_content', '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(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => 'Title',
            'slug' => 'Slug',
            'content' => 'Content',
            'language' => 'Language',
            'layout' => 'Layout',
			'image' => Yii::t('admin', 'Image'),
            'en_title' => Yii::t('admin','En Title'),
            'en_content' => Yii::t('admin','En Content'),
            'attachment_url' => Yii::t('admin','Attachment Url'),
            'attachment_title' => Yii::t('admin','Attachment Title'),
            'meta'=>'Meta',
        );
    }

    /**
     * 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('title', $this->title, true);
        $criteria->compare('slug', $this->slug, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('language', $this->language, true);
        $criteria->compare('layout', $this->layout, true);

        $criteria->compare('en_title',$this->en_title,true);
		$criteria->compare('en_content',$this->en_content,true);

        $criteria->compare('attachment_url',$this->attachment_url,true);
		$criteria->compare('attachment_title',$this->attachment_title,true);

        $criteria->compare('meta',$this->meta,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 Page the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function getLayoutFiles() {
        $files = array();
        $path = Yii::app()->viewPath . DIRECTORY_SEPARATOR . 'layouts';
        if (is_dir($path)) {
            $results = glob($path . DIRECTORY_SEPARATOR . '*.php');
            if ($results) {
                foreach ($results as $file) {
                    $parts = explode(DIRECTORY_SEPARATOR, $file);
                    $parts = rtrim(end($parts), '.php');
                    if (substr($parts, 0, 1) !== '_') {
                        $files [$parts] = $this->camelToText($parts);
                    }
                }
            }
        }
        return $files;
    }

    /**
     * Converto Camel Case string to Human Readable Text
     * @param string $str
     */
    public function camelToText($str) {

        $str = preg_replace('/([A-Z])/', ' $1', $str);
        $str = preg_replace('/([0-9])/', ' $1', $str);
        return ucwords($str);
    }

    public function scopes() {
        return array(
            'filterLanguage' => array(
                'condition' => "t.language='" . Yii::app()->language . "'",
            ),
        );
    }
	public function getThumbnailSizes() {
        return array(
            'thumb' => array(161, 121),
        );
    }

}
