<?php

class CommentWidget extends ShortCodeWidget {

    public $layout = 'default';
    public $shortCode = 'comment';

    public function run() {

        $model = new Comment;
        if (isset($this->_options['owner_route']) && isset($this->_options['owner_params'])) {
            $model->owner_route = $this->_options['owner_route'];
            $model->owner_params = CJSON::encode($this->_options['owner_params']);


            $criteria = new CDbCriteria();
            $criteria->compare('status', 1);
            $criteria->compare('owner_route', $model->owner_route);
            $criteria->compare('owner_params', $model->owner_params);

            $dataProvider = new CActiveDataProvider('Comment', array(
                'pagination' => array(
                    'pageSize' => 3
                ),
                'criteria' => $criteria,
            ));

            $this->render("{$this->shortCode}/{$this->layout}", array('model' => $model, 'dataProvider' => $dataProvider,));
        }
    }

    /**
     * Performs the AJAX validation.
     * @param Comment $model the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'comment-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
