Controller.php 6.02 KB
<?php
    namespace common\modules\comment;

    class Controller extends \yii\web\Controller
    {

        public $enableCsrfValidation = false;

        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => \yii\filters\VerbFilter::className(),
                    'actions' => [
                        '*' => [ 'post' ],
                    ],
                ],
            ];
        }

        public function actionDelete()
        {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $post = \Yii::$app->request->post('Comment');
            $get = \Yii::$app->request->get();
            if(empty( $post[ 'comment_id' ] ) && !empty( $get[ 'comment_id' ] )) {
                $post[ 'comment_id' ] = $get[ 'comment_id' ];
            }
            if(!empty( $post[ 'comment_id' ] )) {
                if($model = \common\modules\comment\models\Comment::findOne($post[ 'comment_id' ])) {
                    /**
                     * @var \common\modules\comment\models\Comment $model
                     */
                    $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
                    if($model->deleteComment()) {
                        \Yii::$app->response->data = [ 'text' => 'Comment marked as deleted and will be check by administrators' ];
                    } else {
                        \Yii::$app->response->data = [ 'error' => $model->hasErrors('comment_id') ? $model->getFirstError('comment_id') : 'Cannot delete message' ];
                    }
                } else {
                    \Yii::$app->response->data = [ 'error' => 'Comment not found' ];
                };
            } else {
                \Yii::$app->response->data = [ 'error' => 'Missing comment_id' ];
            }
            \Yii::$app->response->send();
        }

        public function actionUpdate()
        {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $post = \Yii::$app->request->post();
            if(!empty( $post[ 'Comment' ][ 'comment_id' ] )) {
                if($model = \common\modules\comment\models\Comment::findOne($post[ 'Comment' ][ 'comment_id' ])) {
                    /**
                     * @var \common\modules\comment\models\Comment $model
                     */
                    $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
                    $model->load($post);
                    if(empty( $post[ 'Comment' ][ 'comment_pid' ] )) {
                        $model->comment_pid = NULL;
                    }
                    if($model->updateComment()) {
                        $model->rating->load($post);
                        if($model->rating->save()) {
                            return [
                                'result' => [
                                    'text' => 'Comment successfully updated',
                                    'html' => $this->renderAjax('@common/modules/comment/widgets/views/_review_comment_view', [ 'model' => $model ]),
                                ],
                            ];
                        } else {
                            return [
                                'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
                                'form'  => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                    'model' => $model,
                                ]),
                            ];
                        }
                    } else {
                        return [
                            'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
                            'form'  => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                'model' => $model,
                            ]),
                        ];
                    }
                } else {
                    return [ 'error' => 'Comment not found' ];
                }
            } else {
                return [ 'error' => 'Missing comment_id' ];
            }
        }

        public function actionForm()
        {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $post = \Yii::$app->request->post('Comment');
            if(!empty( $post[ 'comment_id' ] )) {
                $model = \common\modules\comment\models\Comment::find()
                                                               ->where([ 'comment_id' => $post[ 'comment_id' ] ])
                                                               ->with('parent', 'author')
                                                               ->one();
                if($model) {
                    /**
                     * @var \common\modules\comment\models\Comment $model
                     */
                    $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\Comment::SCENARIO_USER : \common\modules\comment\models\Comment::SCENARIO_GUEST;
                    if($model->checkUpdate()) {
                        return [
                            'result' => [
                                'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                    'model' => $model,
                                ]),
                            ],
                        ];
                    } else {
                        return [ 'error' => 'You are not able to update this comment' ];
                    }
                } else {
                    return [ 'error' => 'Comment not found' ];
                }
            } else {
                return [ 'error' => 'Missing comment_id' ];
            }
        }
        
    }