diff --git a/common/modules/comment/Controller.php b/common/modules/comment/Controller.php index 6b39f7c..5e65752 100644 --- a/common/modules/comment/Controller.php +++ b/common/modules/comment/Controller.php @@ -100,7 +100,7 @@ if(!empty( $post[ 'comment_id' ] )) { $model = \common\modules\comment\models\Comment::find() ->where([ 'comment_id' => $post[ 'comment_id' ] ]) - ->with('parent', 'author') + ->with('parent', 'user') ->one(); if($model) { /** @@ -125,5 +125,85 @@ 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' ]; + } + } } \ No newline at end of file diff --git a/common/modules/comment/models/Comment.php b/common/modules/comment/models/Comment.php index a1fe7f5..1b8704d 100644 --- a/common/modules/comment/models/Comment.php +++ b/common/modules/comment/models/Comment.php @@ -176,27 +176,30 @@ public static function getComments($model, $model_id) { return self::find() - ->where([ - 'comment.model' => $model, - 'comment.model_id' => $model_id, - 'comment.status' => 1, - ]) - ->with('rating'); + ->where([ + 'comment.model' => $model, + 'comment.model_id' => $model_id, + 'comment.status' => 1, + ]) + ->with('rating'); } public function postComment() { if($this->checkCreate()) { + if(!empty($this->comment_pid) && !$this->checkReply()) { + $this->addError('comment_id', 'You can`t reply to this message'); + return false; + } if($this->insert()) { $this->clearSafe(); return true; } else { return false; } - } else { - $this->addError('comment_id', 'You can`t post comment here'); - return false; } + $this->addError('comment_id', 'You can`t post comment here'); + return false; } public function updateComment() diff --git a/common/modules/comment/models/CommentProjectAnswer.php b/common/modules/comment/models/CommentProjectAnswer.php index e8f9ec6..b66d963 100644 --- a/common/modules/comment/models/CommentProjectAnswer.php +++ b/common/modules/comment/models/CommentProjectAnswer.php @@ -106,6 +106,16 @@ return $query; } + public function beforeDelete() + { + if(parent::beforeDelete()) { + + return false; + } else { + return false; + } + } + public function checkCreate() { if($this->getGuestComment()) { @@ -124,16 +134,11 @@ if($this->scenario == self::SCENARIO_GUEST) { return false; } else { - return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [ - 'model' => $this->model, - 'model_id' => $this->model_id, - 'comment' => $this, - ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [ - 'model' => $this->model, - 'model_id' => $this->model_id, - 'comment' => $this, - ]); + if(!empty($this->comment_pid) && $this->user_id == \Yii::$app->user->id) { + return true; + } } + return false; } public function checkDelete() @@ -158,7 +163,12 @@ if($this->scenario != self::SCENARIO_GUEST) { if(!$this->isNewRecord && empty( $this->comment_pid )) { $project = Project::findOne($this->model_id); - if($project->user_id == \Yii::$app->user->id) { + if($project->user_id == \Yii::$app->user->id && empty($this->child)) { + return true; + } + } elseif($this->isNewRecord && !empty($this->comment_pid)) { + $parent = self::findOne($this->comment_pid); + if(!empty($parent) && $parent->checkReply()) { return true; } } diff --git a/common/modules/comment/resources/comment.js b/common/modules/comment/resources/comment.js index 27572ef..b0f3954 100644 --- a/common/modules/comment/resources/comment.js +++ b/common/modules/comment/resources/comment.js @@ -112,4 +112,44 @@ $(function() { ) } ); + + $(document).on( + 'click', '.artbox_comment_update_answer', function(e) + { + e.preventDefault(); + var container = $(this).parents('.artbox_comment_container').first(); + var comment_id = $(container).data('key'); + var form_name = $(container).data('form'); + var object = {}; + object[form_name] = {comment_id : comment_id}; + $.post( + '/artbox-comment/form-answer', object, function(data, textStatus, jqXHR) + { + $(container).empty(); + $(container).append(data.result.form); + } + ); + } + ); + + $(document).on( + 'click', '.artbox_comment_update_answer_submit', function(e) + { + e.preventDefault(); + var container = $(this).parents('.artbox_comment_container').first(); + $.post( + '/artbox-comment/update-answer', $(container).find('form').serialize(), function(data) + { + $(container).empty(); + if(!data.error) + { + $(container).append('
'+data.result.text+'
'); + $(container).append(data.result.html); + } else { + $(container).append(data.form); + } + } + ) + } + ); }); diff --git a/common/modules/comment/widgets/views/_question_comment_view.php b/common/modules/comment/widgets/views/_question_comment_view.php index 5ec0ceb..4d5a1c0 100644 --- a/common/modules/comment/widgets/views/_question_comment_view.php +++ b/common/modules/comment/widgets/views/_question_comment_view.php @@ -14,7 +14,9 @@ * @var User $user */ $user = $model->user; - $model->buildButtons(['reply']); + if(empty($model->comment_pid)) { + $model->buildButtons([ 'reply', 'delete' ]); + } ?>