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

    class Controller extends \yii\web\Controller
    {
        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');
            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()) {
                        \Yii::$app->response->data = ['text' => 'Comment successfully updated'];
                    } else {
                        \Yii::$app->response->data = ['error' => $model->hasErrors()?$model->getFirstErrors():'Cannot update 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 actionForm()
        {
            $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 $this->renderAjax('@common/modules/comment/views/comment_form', [
                            'model' => $model,
                        ]);
                    } else {
                        \Yii::$app->response->data = ['error' => 'You are not able to update this comment'];
                    }
                }else {
                    \Yii::$app->response->data = ['error' => 'Comment not found'];
                };
            } else {
                \Yii::$app->response->data = ['error' => 'Missing comment_id'];
            }
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            \Yii::$app->response->send();
        }
        
    }