<?php

class PostComment extends CAction {

    public $layout;
    public $owner_route;
    public $owner_params;

    public function run() {
        if (Yii::app()->request->isAjaxRequest) {
            $model = new Comment;

            if (isset($_POST['Comment'])) {
                $response = array();
                $model->attributes = $_POST['Comment'];

                if ($model->save()) {
                    $flashMessage = Yii::t('admin', 'Your comment posted successfully, it will appear after reviewing.');
                    $flashMessage = '<div class="alert alert-success">' . $flashMessage . '</div>';
                    $response = array(
                        'status' => 'success',
                        'message' => $flashMessage,
                    );
                } else {
                    $response = array(
                        'status' => 'error',
                        'message' => CHtml::errorSummary($model),
                    );
                }
                echo CJSON::encode($response);
                Yii::app()->end();
            }
        }
        $this->controller->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('/site/admin'));
    }

}
