Commit 8eab7c9f6394e860a333e2434f34f12d8c9d4925

Authored by Yarik
1 parent b5becf98

test

common/modules/comment/Controller.php
@@ -100,7 +100,7 @@ @@ -100,7 +100,7 @@
100 if(!empty( $post[ 'comment_id' ] )) { 100 if(!empty( $post[ 'comment_id' ] )) {
101 $model = \common\modules\comment\models\Comment::find() 101 $model = \common\modules\comment\models\Comment::find()
102 ->where([ 'comment_id' => $post[ 'comment_id' ] ]) 102 ->where([ 'comment_id' => $post[ 'comment_id' ] ])
103 - ->with('parent', 'author') 103 + ->with('parent', 'user')
104 ->one(); 104 ->one();
105 if($model) { 105 if($model) {
106 /** 106 /**
@@ -125,5 +125,85 @@ @@ -125,5 +125,85 @@
125 return [ 'error' => 'Missing comment_id' ]; 125 return [ 'error' => 'Missing comment_id' ];
126 } 126 }
127 } 127 }
  128 +
  129 + public function actionUpdateAnswer()
  130 + {
  131 + \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  132 + $post = \Yii::$app->request->post();
  133 + if(!empty( $post[ 'CommentProjectAnswer' ][ 'comment_id' ] )) {
  134 + if($model = \common\modules\comment\models\CommentProjectAnswer::findOne($post[ 'CommentProjectAnswer' ][ 'comment_id' ])) {
  135 + /**
  136 + * @var \common\modules\comment\models\CommentProjectAnswer $model
  137 + */
  138 + $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\CommentProjectAnswer::SCENARIO_USER : \common\modules\comment\models\CommentProjectAnswer::SCENARIO_GUEST;
  139 + $model->load($post);
  140 + if(empty( $post[ 'CommentProjectAnswer' ][ 'comment_pid' ] )) {
  141 + $model->comment_pid = NULL;
  142 + }
  143 + if($model->updateComment()) {
  144 + $model->rating->load($post);
  145 + if($model->rating->save()) {
  146 + return [
  147 + 'result' => [
  148 + 'text' => 'Comment successfully updated',
  149 + 'html' => $this->renderAjax('@common/modules/comment/widgets/views/_question_comment_view', [ 'model' => $model ]),
  150 + ],
  151 + ];
  152 + } else {
  153 + return [
  154 + 'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
  155 + 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [
  156 + 'model' => $model,
  157 + ]),
  158 + ];
  159 + }
  160 + } else {
  161 + return [
  162 + 'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
  163 + 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [
  164 + 'model' => $model,
  165 + ]),
  166 + ];
  167 + }
  168 + } else {
  169 + return [ 'error' => 'Comment not found' ];
  170 + }
  171 + } else {
  172 + return [ 'error' => 'Missing comment_id' ];
  173 + }
  174 + }
  175 +
  176 + public function actionFormAnswer()
  177 + {
  178 + \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  179 + $post = \Yii::$app->request->post('CommentProjectAnswer');
  180 + if(!empty( $post[ 'comment_id' ] )) {
  181 + $model = \common\modules\comment\models\CommentProjectAnswer::find()
  182 + ->where([ 'comment_id' => $post[ 'comment_id' ] ])
  183 + ->with('parent', 'user')
  184 + ->one();
  185 + if($model) {
  186 + /**
  187 + * @var \common\modules\comment\models\CommentProjectAnswer $model
  188 + */
  189 + $model->scenario = is_int(\Yii::$app->user->getId()) ? \common\modules\comment\models\CommentProjectAnswer::SCENARIO_USER : \common\modules\comment\models\CommentProjectAnswer::SCENARIO_GUEST;
  190 + if($model->checkUpdate()) {
  191 + return [
  192 + 'result' => [
  193 + 'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-answer', [
  194 + 'model' => $model,
  195 + ]),
  196 + ],
  197 + ];
  198 + } else {
  199 + return [ 'error' => 'You are not able to update this comment' ];
  200 + }
  201 + } else {
  202 + return [ 'error' => 'Comment not found' ];
  203 + }
  204 + } else {
  205 + return [ 'error' => 'Missing comment_id' ];
  206 + }
  207 + }
128 208
129 } 209 }
130 \ No newline at end of file 210 \ No newline at end of file
common/modules/comment/models/Comment.php
@@ -176,27 +176,30 @@ @@ -176,27 +176,30 @@
176 public static function getComments($model, $model_id) 176 public static function getComments($model, $model_id)
177 { 177 {
178 return self::find() 178 return self::find()
179 - ->where([  
180 - 'comment.model' => $model,  
181 - 'comment.model_id' => $model_id,  
182 - 'comment.status' => 1,  
183 - ])  
184 - ->with('rating'); 179 + ->where([
  180 + 'comment.model' => $model,
  181 + 'comment.model_id' => $model_id,
  182 + 'comment.status' => 1,
  183 + ])
  184 + ->with('rating');
185 } 185 }
186 186
187 public function postComment() 187 public function postComment()
188 { 188 {
189 if($this->checkCreate()) { 189 if($this->checkCreate()) {
  190 + if(!empty($this->comment_pid) && !$this->checkReply()) {
  191 + $this->addError('comment_id', 'You can`t reply to this message');
  192 + return false;
  193 + }
190 if($this->insert()) { 194 if($this->insert()) {
191 $this->clearSafe(); 195 $this->clearSafe();
192 return true; 196 return true;
193 } else { 197 } else {
194 return false; 198 return false;
195 } 199 }
196 - } else {  
197 - $this->addError('comment_id', 'You can`t post comment here');  
198 - return false;  
199 } 200 }
  201 + $this->addError('comment_id', 'You can`t post comment here');
  202 + return false;
200 } 203 }
201 204
202 public function updateComment() 205 public function updateComment()
common/modules/comment/models/CommentProjectAnswer.php
@@ -106,6 +106,16 @@ @@ -106,6 +106,16 @@
106 return $query; 106 return $query;
107 } 107 }
108 108
  109 + public function beforeDelete()
  110 + {
  111 + if(parent::beforeDelete()) {
  112 +
  113 + return false;
  114 + } else {
  115 + return false;
  116 + }
  117 + }
  118 +
109 public function checkCreate() 119 public function checkCreate()
110 { 120 {
111 if($this->getGuestComment()) { 121 if($this->getGuestComment()) {
@@ -124,16 +134,11 @@ @@ -124,16 +134,11 @@
124 if($this->scenario == self::SCENARIO_GUEST) { 134 if($this->scenario == self::SCENARIO_GUEST) {
125 return false; 135 return false;
126 } else { 136 } else {
127 - return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [  
128 - 'model' => $this->model,  
129 - 'model_id' => $this->model_id,  
130 - 'comment' => $this,  
131 - ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [  
132 - 'model' => $this->model,  
133 - 'model_id' => $this->model_id,  
134 - 'comment' => $this,  
135 - ]); 137 + if(!empty($this->comment_pid) && $this->user_id == \Yii::$app->user->id) {
  138 + return true;
  139 + }
136 } 140 }
  141 + return false;
137 } 142 }
138 143
139 public function checkDelete() 144 public function checkDelete()
@@ -158,7 +163,12 @@ @@ -158,7 +163,12 @@
158 if($this->scenario != self::SCENARIO_GUEST) { 163 if($this->scenario != self::SCENARIO_GUEST) {
159 if(!$this->isNewRecord && empty( $this->comment_pid )) { 164 if(!$this->isNewRecord && empty( $this->comment_pid )) {
160 $project = Project::findOne($this->model_id); 165 $project = Project::findOne($this->model_id);
161 - if($project->user_id == \Yii::$app->user->id) { 166 + if($project->user_id == \Yii::$app->user->id && empty($this->child)) {
  167 + return true;
  168 + }
  169 + } elseif($this->isNewRecord && !empty($this->comment_pid)) {
  170 + $parent = self::findOne($this->comment_pid);
  171 + if(!empty($parent) && $parent->checkReply()) {
162 return true; 172 return true;
163 } 173 }
164 } 174 }
common/modules/comment/resources/comment.js
@@ -112,4 +112,44 @@ $(function() { @@ -112,4 +112,44 @@ $(function() {
112 ) 112 )
113 } 113 }
114 ); 114 );
  115 +
  116 + $(document).on(
  117 + 'click', '.artbox_comment_update_answer', function(e)
  118 + {
  119 + e.preventDefault();
  120 + var container = $(this).parents('.artbox_comment_container').first();
  121 + var comment_id = $(container).data('key');
  122 + var form_name = $(container).data('form');
  123 + var object = {};
  124 + object[form_name] = {comment_id : comment_id};
  125 + $.post(
  126 + '/artbox-comment/form-answer', object, function(data, textStatus, jqXHR)
  127 + {
  128 + $(container).empty();
  129 + $(container).append(data.result.form);
  130 + }
  131 + );
  132 + }
  133 + );
  134 +
  135 + $(document).on(
  136 + 'click', '.artbox_comment_update_answer_submit', function(e)
  137 + {
  138 + e.preventDefault();
  139 + var container = $(this).parents('.artbox_comment_container').first();
  140 + $.post(
  141 + '/artbox-comment/update-answer', $(container).find('form').serialize(), function(data)
  142 + {
  143 + $(container).empty();
  144 + if(!data.error)
  145 + {
  146 + $(container).append('<p>'+data.result.text+'</p>');
  147 + $(container).append(data.result.html);
  148 + } else {
  149 + $(container).append(data.form);
  150 + }
  151 + }
  152 + )
  153 + }
  154 + );
115 }); 155 });
common/modules/comment/widgets/views/_question_comment_view.php
@@ -14,7 +14,9 @@ @@ -14,7 +14,9 @@
14 * @var User $user 14 * @var User $user
15 */ 15 */
16 $user = $model->user; 16 $user = $model->user;
17 - $model->buildButtons(['reply']); 17 + if(empty($model->comment_pid)) {
  18 + $model->buildButtons([ 'reply', 'delete' ]);
  19 + }
18 ?> 20 ?>
19 <div class="comments-name <?= CommentWidget::$baseClass[ 'comment_author' ] ?>"> 21 <div class="comments-name <?= CommentWidget::$baseClass[ 'comment_author' ] ?>">
20 <?= $model->getAuthor(' (Гость)') ?> 22 <?= $model->getAuthor(' (Гость)') ?>
@@ -30,13 +32,29 @@ @@ -30,13 +32,29 @@
30 <div class="comments-content"> 32 <div class="comments-content">
31 <?= $model->text ?> 33 <?= $model->text ?>
32 </div> 34 </div>
  35 +<?php
  36 + if(!empty( $model->child )) {
  37 + echo Html::tag('div', $this->render('@common/modules/comment/widgets/views/_question_comment_view', [
  38 + 'model' => $model->child,
  39 + 'key' => $key,
  40 + 'index' => $index,
  41 + 'widget' => $widget,
  42 + ]), [
  43 + 'class' => CommentWidget::$baseClass[ 'comment_container' ],
  44 + 'data' => [
  45 + 'key' => $model->child->comment_id,
  46 + 'form' => $model->formName(),
  47 + ],
  48 + ]);
  49 + }
  50 +?>
33 <div> 51 <div>
34 <?php 52 <?php
35 if(!empty( $model->buttons[ 'delete' ] )) { 53 if(!empty( $model->buttons[ 'delete' ] )) {
36 - echo Html::a(($model->user_id != NULL && $model->user_id == \Yii::$app->user->id)?'Удалить':'Пожаловаться ', $model->buttons[ 'delete' ], [ 'class' => CommentWidget::$baseClass[ 'comment_delete' ] ]); 54 + echo Html::a(( $model->user_id != NULL && $model->user_id == \Yii::$app->user->id ) ? 'Удалить' : 'Пожаловаться ', $model->buttons[ 'delete' ], [ 'class' => CommentWidget::$baseClass[ 'comment_delete' ] ]);
37 } 55 }
38 if(!empty( $model->buttons[ 'update' ] )) { 56 if(!empty( $model->buttons[ 'update' ] )) {
39 - echo Html::a('Редактировать', $model->buttons[ 'update' ], [ 'class' => CommentWidget::$baseClass[ 'comment_update' ] ]); 57 + echo Html::a('Редактировать', $model->buttons[ 'update' ], [ 'class' => 'artbox_comment_update_answer' ]);
40 } 58 }
41 if(!empty( $model->buttons[ 'reply' ] )) { 59 if(!empty( $model->buttons[ 'reply' ] )) {
42 echo Html::a('Ответить', $model->buttons[ 'reply' ], [ 'class' => CommentWidget::$baseClass[ 'comment_reply' ] ]); 60 echo Html::a('Ответить', $model->buttons[ 'reply' ], [ 'class' => CommentWidget::$baseClass[ 'comment_reply' ] ]);
@@ -44,15 +62,6 @@ @@ -44,15 +62,6 @@
44 ?> 62 ?>
45 </div> 63 </div>
46 <?php 64 <?php
47 - if(!$model->isNewRecord) {  
48 - $this->registerJs("$('div.rating').rating(  
49 - {  
50 - fx : 'full', readOnly : 'true', url : 'rating.php'  
51 - }  
52 - );");  
53 - }  
54 -?>  
55 -<?php  
56 /* == PROJECT INFO == 65 /* == PROJECT INFO ==
57 ?> 66 ?>
58 <div class="comments-project-link">Проект: <a href="#">Ремонт спальни</a></div> 67 <div class="comments-project-link">Проект: <a href="#">Ремонт спальни</a></div>
common/modules/comment/widgets/views/form-comment-answer.php
@@ -9,12 +9,30 @@ @@ -9,12 +9,30 @@
9 use yii\helpers\Html; 9 use yii\helpers\Html;
10 10
11 ?> 11 ?>
12 -<div class="new-portf-comm-count">Вопросов: <?= $dataProvider->totalCount ?></div> 12 +<?php
  13 + if(!empty( $dataProvider )) {
  14 + ?>
  15 + <div class="new-portf-comm-count">Вопросов: <?= $dataProvider->totalCount ?></div>
  16 + <?php
  17 + }
  18 +?>
13 19
14 <div class="new-portf-add-comm style"> 20 <div class="new-portf-add-comm style">
15 21
16 <?php 22 <?php
17 $form = ActiveForm::begin(); 23 $form = ActiveForm::begin();
  24 + if(!$model->isNewRecord) {
  25 + echo $form->field($model, 'comment_id')
  26 + ->hiddenInput()
  27 + ->label(false)
  28 + ->error(false);
  29 + }
  30 + if(!empty($model->comment_pid)) {
  31 + echo $form->field($model, 'comment_pid')
  32 + ->hiddenInput()
  33 + ->label(false)
  34 + ->error(false);
  35 + }
18 if($model->scenario == $model::SCENARIO_GUEST) { 36 if($model->scenario == $model::SCENARIO_GUEST) {
19 echo $form->field($model, 'user_name', [ 37 echo $form->field($model, 'user_name', [
20 'options' => [ 38 'options' => [
@@ -48,11 +66,17 @@ @@ -48,11 +66,17 @@
48 'class' => 'custom-area-4', 66 'class' => 'custom-area-4',
49 ], 67 ],
50 ]) 68 ])
51 - ->label('Вопрос') 69 + ->label($model->isNewRecord?'Вопрос':'Ответ')
52 ->textarea(); 70 ->textarea();
53 ?> 71 ?>
54 <div class="input-blocks-comm-button style"> 72 <div class="input-blocks-comm-button style">
55 - <?= Html::submitButton('Задать вопрос') ?> 73 + <?php
  74 + if($model->isNewRecord) {
  75 + echo Html::submitButton('Добавить комментарий');
  76 + } else {
  77 + echo Html::submitButton('Обновить комментарий', [ 'class' => 'artbox_comment_update_answer_submit' ]);
  78 + }
  79 + ?>
56 </div> 80 </div>
57 <?php 81 <?php
58 $form->end(); 82 $form->end();
common/modules/comment/widgets/views/form-comment-review.php
@@ -70,7 +70,7 @@ @@ -70,7 +70,7 @@
70 ->textInput(); 70 ->textInput();
71 } 71 }
72 if(!empty( $model->comment_pid )) { 72 if(!empty( $model->comment_pid )) {
73 - 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' ] ]); 73 + 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' ] ]);
74 } else { 74 } else {
75 echo Html::tag('div', '', [ 'class' => CommentWidget::$baseClass[ 'reply_block' ].' test_class' ]); 75 echo Html::tag('div', '', [ 'class' => CommentWidget::$baseClass[ 'reply_block' ].' test_class' ]);
76 } 76 }