<?php

/**
 * This is the model class for table "comment".
 *
 * The followings are the available columns in table 'comment':
 * @property integer $id
 * @property string $sender_name
 * @property string $sender_email
 * @property string $sender_homepage
 * @property string $body
 * @property integer $ip_address
 * @property string $country
 * @property string $user_agent
 * @property integer $status
 * @property integer $create_datetime
 * @property integer $update_datetime
 * @property string $owner_route
 * @property string $owner_params
 * @property integer $parent_id
 *
 * The followings are the available model relations:
 * @property Comment $parent
 * @property Comment[] $comments
 */
class Comment extends CActiveRecord {

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

    /**
     * @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('sender_name, sender_email, body,  ip_address, user_agent, create_datetime, update_datetime, owner_route, owner_params', 'required'),
            array('ip_address, status, create_datetime, update_datetime, parent_id', 'numerical', 'integerOnly' => true),
            array('sender_name, sender_email, sender_homepage, owner_route', 'length', 'max' => 100),
            array('country', 'length', 'max' => 2),
            array('sender_email', 'email'),
            array('user_agent, owner_params', 'length', 'max' => 300),
            // array('email', 'required', 'on'=>'create'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, sender_name, sender_email, sender_homepage, body, ip_address, country, user_agent, status, create_datetime, update_datetime, owner_route, owner_params, parent_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(
            'parent' => array(self::BELONGS_TO, 'Comment', 'parent_id'),
            'comments' => array(self::HAS_MANY, 'Comment', 'parent_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'sender_name' => Yii::t('admin', 'Sender Name'),
            'sender_email' => Yii::t('admin', 'Sender Email'),
            'sender_homepage' => Yii::t('admin', 'Sender Homepage'),
            'body' => Yii::t('admin', 'Comment Body'),
            'ip_address' => Yii::t('admin', 'IP Address'),
            'country' => Yii::t('admin', 'Country'),
            'user_agent' => Yii::t('admin', 'User Agent'),
            'status' => Yii::t('admin', 'Status'),
            'create_datetime' => Yii::t('admin', 'Create Datetime'),
            'update_datetime' => Yii::t('admin', 'Update Datetime'),
            'owner_route' => Yii::t('admin', 'Owner Route'),
            'owner_params' => Yii::t('admin', 'Owner Params'),
            'parent_id' => Yii::t('admin', 'Parent'),
        );
    }

    /**
     * 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('sender_name', $this->sender_name, true);
        $criteria->compare('sender_email', $this->sender_email, true);
        $criteria->compare('sender_homepage', $this->sender_homepage, true);
        $criteria->compare('body', $this->body, true);
        $criteria->compare('ip_address', $this->ip_address);
        $criteria->compare('country', $this->country, true);
        $criteria->compare('user_agent', $this->user_agent, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('create_datetime', $this->create_datetime);
        $criteria->compare('update_datetime', $this->update_datetime);
        $criteria->compare('owner_route', $this->owner_route, true);
        $criteria->compare('owner_params', $this->owner_params, true);
        $criteria->compare('parent_id', $this->parent_id);

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

    public function defaultScope() {
        return array('order' => 'id desc');
    }

    public function beforeValidate() {
        if ($this->isNewRecord) {

            $ip_address = Common::getUserIP();
            $this->ip_address = ip2long($ip_address);
            $this->country = Yii::app()->geoip->lookupCountryCode($ip_address);
            $this->user_agent = Yii::app()->request->getUserAgent();

            $this->create_datetime = time();
        }
        $this->update_datetime = time();

        $body = $this->body;
        $body = strip_tags($body);
        $purify = new CHtmlPurifier();
        $this->body = $purify->purify($body);


        return parent::beforeValidate();
    }

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

    public function getStatusLabel() {
        $statuses = self::getStatuses();

        return $statuses[$this->status];
    }

    public static function getStatuses() {
        return array(
            0 => Yii::t("admin", "Draft"),
            1 => Yii::t("admin", "Published"),
        );
    }

}
