Blame view

common/modules/comment/Controller.php 11.8 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()) {
8a551494   Yarik   test
36
                          \Yii::$app->response->data = [ 'text' => 'Comment marked as deleted and will be check by administrators' ];
2d107e9e   Yarik   test
37
                      } else {
8a551494   Yarik   test
38
                          \Yii::$app->response->data = [ 'error' => $model->hasErrors('comment_id') ? $model->getFirstError('comment_id') : 'Cannot delete message' ];
2d107e9e   Yarik   test
39
                      }
8a551494   Yarik   test
40
41
                  } else {
                      \Yii::$app->response->data = [ 'error' => 'Comment not found' ];
2d107e9e   Yarik   test
42
43
                  };
              } else {
8a551494   Yarik   test
44
                  \Yii::$app->response->data = [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
45
46
47
48
49
50
51
52
              }
              \Yii::$app->response->send();
          }
  
          public function actionUpdate()
          {
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              $post = \Yii::$app->request->post();
8a551494   Yarik   test
53
54
              if(!empty( $post[ 'Comment' ][ 'comment_id' ] )) {
                  if($model = \common\modules\comment\models\Comment::findOne($post[ 'Comment' ][ 'comment_id' ])) {
2d107e9e   Yarik   test
55
56
57
58
59
                      /**
                       * @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
60
61
                      if(empty( $post[ 'Comment' ][ 'comment_pid' ] )) {
                          $model->comment_pid = NULL;
2d107e9e   Yarik   test
62
63
                      }
                      if($model->updateComment()) {
8a551494   Yarik   test
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                          $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,
                                  ]),
                              ];
                          }
2d107e9e   Yarik   test
80
                      } else {
8a551494   Yarik   test
81
82
83
84
85
86
                          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
87
                      }
8a551494   Yarik   test
88
89
90
                  } else {
                      return [ 'error' => 'Comment not found' ];
                  }
2d107e9e   Yarik   test
91
              } else {
8a551494   Yarik   test
92
                  return [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
93
              }
2d107e9e   Yarik   test
94
95
96
97
          }
  
          public function actionForm()
          {
8a551494   Yarik   test
98
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
2d107e9e   Yarik   test
99
              $post = \Yii::$app->request->post('Comment');
8a551494   Yarik   test
100
101
102
              if(!empty( $post[ 'comment_id' ] )) {
                  $model = \common\modules\comment\models\Comment::find()
                                                                 ->where([ 'comment_id' => $post[ 'comment_id' ] ])
8eab7c9f   Yarik   test
103
                                                                 ->with('parent', 'user')
8a551494   Yarik   test
104
                                                                 ->one();
2d107e9e   Yarik   test
105
106
107
108
109
110
                  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
111
112
113
114
115
116
117
                          return [
                              'result' => [
                                  'form' => $this->renderAjax('@common/modules/comment/widgets/views/form-comment-review', [
                                      'model' => $model,
                                  ]),
                              ],
                          ];
2d107e9e   Yarik   test
118
                      } else {
8a551494   Yarik   test
119
                          return [ 'error' => 'You are not able to update this comment' ];
2d107e9e   Yarik   test
120
                      }
8a551494   Yarik   test
121
122
123
                  } else {
                      return [ 'error' => 'Comment not found' ];
                  }
2d107e9e   Yarik   test
124
              } else {
8a551494   Yarik   test
125
                  return [ 'error' => 'Missing comment_id' ];
2d107e9e   Yarik   test
126
              }
2d107e9e   Yarik   test
127
          }
8eab7c9f   Yarik   test
128
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
  
          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
208
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
  
          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
236
          
2d107e9e   Yarik   test
237
      }