<?php

/**
 * This is the model class for table "review".
 *
 * The followings are the available columns in table 'review':
 * @property integer $id
 * @property string $date
 * @property integer $reviews_root_id
 *
 * The followings are the available model relations:
 * @property ReviewI18n[] $i18ns
 * @property ReviewI18n $i18n
 *
 * @property ImageARBehavior $imageBehavior
 */
class Review extends CActiveRecord implements INodeCrudModel
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Review 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 'review';
    }

    /**
     * @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('dateRu', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, date', '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(
            'i18ns' => array(self::HAS_MANY, 'ReviewI18n', 'id', 'index' => 'lang'),
            'i18n' => array(self::HAS_ONE, 'ReviewI18n', 'id', 'condition' => 'lang=\'' . Yii::app()->language . '\''),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'date' => 'Дата',
            'image' => 'Фото',
        );
    }

    /**
     * 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', $this->id);
        $criteria->compare('reviews_root_id', $this->reviews_root_id);
        $criteria->compare('date', $this->date, true);

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

    public $image;

    public function behaviors()
    {
        return array(
            'imageBehavior' => array(
                'class' => 'ImageARBehavior',
                'attribute' => 'image', // this must exist
                'extension' => 'png, gif, jpg, jpeg', // possible extensions, comma separated
                'prefix' => 'owner_',
                'relativeWebRootFolder' => 'images/reviews', // this folder must exist

                'formats' => array(
                    'medium' => array(
                        'suffix' => '_medium',
                        'process' => array(
                            'resize' => array(140, null),
                        ),
                    ),
                    'normal' => array(),
                ),

                'defaultName' => 'default',
            ),
        );
    }


    public function getDateRu()
    {
        return ($this->date !== null)
            ? date('d.m.Y', CDateTimeParser::parse($this->date, 'yyyy-MM-dd'))
            : date('d.m.Y');
    }

    public function setDateRu($value)
    {
        $this->date = date('Y-m-d', CDateTimeParser::parse($value, 'dd.MM.yyyy'));
    }

    /**
     * @param Node $node
     * @return void
     */
    public function setNode($node)
    {
        $this->reviews_root_id = $node->data_id;
    }
}