[ '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', 'user') ->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' ]; } } public function actionUpdateAnswer() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $post = \Yii::$app->request->post(); if(!empty( $post[ 'CommentProjectAnswer' ][ 'comment_id' ] )) { if($model = \common\modules\comment\models\CommentProjectAnswer::findOne($post[ 'CommentProjectAnswer' ][ 'comment_id' ])) { /** * @var \common\modules\comment\models\CommentProjectAnswer $model */ $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\CommentProjectAnswer::SCENARIO_USER : \common\modules\comment\models\CommentProjectAnswer::SCENARIO_GUEST; $model->load($post); if(empty( $post[ 'CommentProjectAnswer' ][ '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/_question_comment_view', [ 'model' => $model ]), ], ]; } else { return [ 'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message', 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [ 'model' => $model, ]), ]; } } else { return [ 'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message', 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [ 'model' => $model, ]), ]; } } else { return [ 'error' => 'Comment not found' ]; } } else { return [ 'error' => 'Missing comment_id' ]; } } public function actionFormAnswer() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $post = \Yii::$app->request->post('CommentProjectAnswer'); if(!empty( $post[ 'comment_id' ] )) { $model = \common\modules\comment\models\CommentProjectAnswer::find() ->where([ 'comment_id' => $post[ 'comment_id' ] ]) ->with('parent', 'user') ->one(); if($model) { /** * @var \common\modules\comment\models\CommentProjectAnswer $model */ $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\CommentProjectAnswer::SCENARIO_USER : \common\modules\comment\models\CommentProjectAnswer::SCENARIO_GUEST; if($model->checkUpdate()) { return [ 'result' => [ 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [ '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' ]; } } public function actionDeleteAnswer() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $post = \Yii::$app->request->post('CommentProjectAnswer'); $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\CommentProjectAnswer::findOne($post[ 'comment_id' ])) { /** * @var \common\modules\comment\models\Comment $model */ $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\CommentProjectAnswer::SCENARIO_USER : \common\modules\comment\models\CommentProjectAnswer::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(); } }