From 8eab7c9f6394e860a333e2434f34f12d8c9d4925 Mon Sep 17 00:00:00 2001 From: Yarik Date: Thu, 21 Apr 2016 18:24:02 +0300 Subject: [PATCH] test --- common/modules/comment/Controller.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- common/modules/comment/models/Comment.php | 21 ++++++++++++--------- common/modules/comment/models/CommentProjectAnswer.php | 30 ++++++++++++++++++++---------- common/modules/comment/resources/comment.js | 40 ++++++++++++++++++++++++++++++++++++++++ common/modules/comment/widgets/views/_question_comment_view.php | 33 +++++++++++++++++++++------------ common/modules/comment/widgets/views/form-comment-answer.php | 30 +++++++++++++++++++++++++++--- common/modules/comment/widgets/views/form-comment-review.php | 2 +- 7 files changed, 202 insertions(+), 36 deletions(-) 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' ]); + } ?>
getAuthor(' (Гость)') ?> @@ -30,13 +32,29 @@
text ?>
+child )) { + echo Html::tag('div', $this->render('@common/modules/comment/widgets/views/_question_comment_view', [ + 'model' => $model->child, + 'key' => $key, + 'index' => $index, + 'widget' => $widget, + ]), [ + 'class' => CommentWidget::$baseClass[ 'comment_container' ], + 'data' => [ + 'key' => $model->child->comment_id, + 'form' => $model->formName(), + ], + ]); + } +?>
buttons[ 'delete' ] )) { - echo Html::a(($model->user_id != NULL && $model->user_id == \Yii::$app->user->id)?'Удалить':'Пожаловаться ', $model->buttons[ 'delete' ], [ 'class' => CommentWidget::$baseClass[ 'comment_delete' ] ]); + echo Html::a(( $model->user_id != NULL && $model->user_id == \Yii::$app->user->id ) ? 'Удалить' : 'Пожаловаться ', $model->buttons[ 'delete' ], [ 'class' => CommentWidget::$baseClass[ 'comment_delete' ] ]); } if(!empty( $model->buttons[ 'update' ] )) { - echo Html::a('Редактировать', $model->buttons[ 'update' ], [ 'class' => CommentWidget::$baseClass[ 'comment_update' ] ]); + echo Html::a('Редактировать', $model->buttons[ 'update' ], [ 'class' => 'artbox_comment_update_answer' ]); } if(!empty( $model->buttons[ 'reply' ] )) { echo Html::a('Ответить', $model->buttons[ 'reply' ], [ 'class' => CommentWidget::$baseClass[ 'comment_reply' ] ]); @@ -44,15 +62,6 @@ ?>
isNewRecord) { - $this->registerJs("$('div.rating').rating( - { - fx : 'full', readOnly : 'true', url : 'rating.php' - } - );"); - } -?> - diff --git a/common/modules/comment/widgets/views/form-comment-answer.php b/common/modules/comment/widgets/views/form-comment-answer.php index 43ec915..248ae6b 100644 --- a/common/modules/comment/widgets/views/form-comment-answer.php +++ b/common/modules/comment/widgets/views/form-comment-answer.php @@ -9,12 +9,30 @@ use yii\helpers\Html; ?> -
Вопросов: totalCount ?>
+ +
Вопросов: totalCount ?>
+
isNewRecord) { + echo $form->field($model, 'comment_id') + ->hiddenInput() + ->label(false) + ->error(false); + } + if(!empty($model->comment_pid)) { + echo $form->field($model, 'comment_pid') + ->hiddenInput() + ->label(false) + ->error(false); + } if($model->scenario == $model::SCENARIO_GUEST) { echo $form->field($model, 'user_name', [ 'options' => [ @@ -48,11 +66,17 @@ 'class' => 'custom-area-4', ], ]) - ->label('Вопрос') + ->label($model->isNewRecord?'Вопрос':'Ответ') ->textarea(); ?>
- + isNewRecord) { + echo Html::submitButton('Добавить комментарий'); + } else { + echo Html::submitButton('Обновить комментарий', [ 'class' => 'artbox_comment_update_answer_submit' ]); + } + ?>
end(); diff --git a/common/modules/comment/widgets/views/form-comment-review.php b/common/modules/comment/widgets/views/form-comment-review.php index 727dfbc..c6c3209 100644 --- a/common/modules/comment/widgets/views/form-comment-review.php +++ b/common/modules/comment/widgets/views/form-comment-review.php @@ -70,7 +70,7 @@ ->textInput(); } if(!empty( $model->comment_pid )) { - echo Html::tag('div', Html::activeHiddenInput($model, 'comment_pid') . Html::tag('p', $model->parent->authorName, [ 'class' => 'artbox_comment_reply_author' ]), [ 'class' => CommentWidget::$baseClass[ 'reply_block' ] ]); + echo Html::tag('div', Html::activeHiddenInput($model, 'comment_pid') . Html::tag('p', $model->parent->author, [ 'class' => 'artbox_comment_reply_author' ]), [ 'class' => CommentWidget::$baseClass[ 'reply_block' ] ]); } else { echo Html::tag('div', '', [ 'class' => CommentWidget::$baseClass[ 'reply_block' ].' test_class' ]); } -- libgit2 0.21.4