Blame view

common/modules/comment/Controller.php 6.02 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
103
104
              if(!empty( $post[ 'comment_id' ] )) {
                  $model = \common\modules\comment\models\Comment::find()
                                                                 ->where([ 'comment_id' => $post[ 'comment_id' ] ])
                                                                 ->with('parent', 'author')
                                                                 ->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
          }
38a6e1dd   Yarik   test
128
          
2d107e9e   Yarik   test
129
      }