Blame view

common/modules/comment/Controller.php 11.9 KB
2d107e9e   Yarik   test
1
2
3
4
5
  <?php
      namespace common\modules\comment;
  
      class Controller extends \yii\web\Controller
      {
8a551494   Yarik   test
6
7
8
  
          public $enableCsrfValidation = false;
  
2d107e9e   Yarik   test
9
10
11
12
          public function behaviors()
          {
              return [
                  'verbs' => [
8a551494   Yarik   test
13
                      'class'   => \yii\filters\VerbFilter::className(),
2d107e9e   Yarik   test
14
                      'actions' => [
8a551494   Yarik   test
15
                          '*' => [ 'post' ],
2d107e9e   Yarik   test
16
17
18
19
20
21
22
23
24
                      ],
                  ],
              ];
          }
  
          public function actionDelete()
          {
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              $post = \Yii::$app->request->post('Comment');
0c0cdc9d   Yarik   test
25
              $get = \Yii::$app->request->get();
8a551494   Yarik   test
26
27
              if(empty( $post[ 'comment_id' ] ) && !empty( $get[ 'comment_id' ] )) {
                  $post[ 'comment_id' ] = $get[ 'comment_id' ];
0c0cdc9d   Yarik   test
28
              }
8a551494   Yarik   test
29
30
              if(!empty( $post[ 'comment_id' ] )) {
                  if($model = \common\modules\comment\models\Comment::findOne($post[ 'comment_id' ])) {
2d107e9e   Yarik   test
31
32
33
34
35
                      /**
                       * @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()) {
9b93120e   Alex Savenko   review fix
36
                          $model->updateComment();
8a551494   Yarik   test
37
                          \Yii::$app->response->data = [ 'text' => 'Comment marked as deleted and will be check by administrators' ];
2d107e9e   Yarik   test
38
                      } else {
8a551494   Yarik   test
39
                          \Yii::$app->response->data = [ 'error' => $model->hasErrors('comment_id') ? $model->getFirstError('comment_id') : 'Cannot delete message' ];
2d107e9e   Yarik   test
40
                      }
8a551494   Yarik   test
41
42
                  } else {
                      \Yii::$app->response->data = [ 'error' => 'Comment not found' ];
2d107e9e   Yarik   test
43
44
                  };
              } else {
8a551494   Yarik   test
45
                  \Yii::$app->response->data = [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
46
47
48
49
50
51
52
53
              }
              \Yii::$app->response->send();
          }
  
          public function actionUpdate()
          {
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              $post = \Yii::$app->request->post();
8a551494   Yarik   test
54
55
              if(!empty( $post[ 'Comment' ][ 'comment_id' ] )) {
                  if($model = \common\modules\comment\models\Comment::findOne($post[ 'Comment' ][ 'comment_id' ])) {
2d107e9e   Yarik   test
56
57
58
59
60
                      /**
                       * @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);
8a551494   Yarik   test
61
62
                      if(empty( $post[ 'Comment' ][ 'comment_pid' ] )) {
                          $model->comment_pid = NULL;
2d107e9e   Yarik   test
63
64
                      }
                      if($model->updateComment()) {
8a551494   Yarik   test
65
66
67
68
                          $model->rating->load($post);
                          if($model->rating->save()) {
                              return [
                                  'result' => [
282b6e56   Yarik   Commit
69
                                      'text' => 'Мнение успешно отредактировано',
8a551494   Yarik   test
70
71
72
73
74
75
76
77
78
79
80
                                      '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,
                                  ]),
                              ];
                          }
2d107e9e   Yarik   test
81
                      } else {
8a551494   Yarik   test
82
83
84
85
86
87
                          return [
                              'error' => $model->hasErrors() ? $model->getFirstErrors() : 'Cannot update message',
                              'form'  => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                  'model' => $model,
                              ]),
                          ];
2d107e9e   Yarik   test
88
                      }
8a551494   Yarik   test
89
90
91
                  } else {
                      return [ 'error' => 'Comment not found' ];
                  }
2d107e9e   Yarik   test
92
              } else {
8a551494   Yarik   test
93
                  return [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
94
              }
2d107e9e   Yarik   test
95
96
97
98
          }
  
          public function actionForm()
          {
8a551494   Yarik   test
99
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
2d107e9e   Yarik   test
100
              $post = \Yii::$app->request->post('Comment');
8a551494   Yarik   test
101
102
103
              if(!empty( $post[ 'comment_id' ] )) {
                  $model = \common\modules\comment\models\Comment::find()
                                                                 ->where([ 'comment_id' => $post[ 'comment_id' ] ])
8eab7c9f   Yarik   test
104
                                                                 ->with('parent', 'user')
8a551494   Yarik   test
105
                                                                 ->one();
2d107e9e   Yarik   test
106
107
108
109
110
111
                  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()) {
8a551494   Yarik   test
112
113
114
115
116
117
118
                          return [
                              'result' => [
                                  'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                      'model' => $model,
                                  ]),
                              ],
                          ];
2d107e9e   Yarik   test
119
                      } else {
8a551494   Yarik   test
120
                          return [ 'error' => 'You are not able to update this comment' ];
2d107e9e   Yarik   test
121
                      }
8a551494   Yarik   test
122
123
124
                  } else {
                      return [ 'error' => 'Comment not found' ];
                  }
2d107e9e   Yarik   test
125
              } else {
8a551494   Yarik   test
126
                  return [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
127
              }
2d107e9e   Yarik   test
128
          }
8eab7c9f   Yarik   test
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  
          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' ];
              }
          }
d7f5a86c   Yarik   test
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  
          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();
          }
38a6e1dd   Yarik   test
237
          
2d107e9e   Yarik   test
238
      }