Blame view

common/modules/comment/Controller.php 4.67 KB
2d107e9e   Yarik   test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <?php
      namespace common\modules\comment;
  
      class Controller extends \yii\web\Controller
      {
          public function behaviors()
          {
              return [
                  'verbs' => [
                      'class' => \yii\filters\VerbFilter::className(),
                      'actions' => [
                          '*' => ['post'],
                      ],
                  ],
              ];
          }
  
          public function actionDelete()
          {
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              $post = \Yii::$app->request->post('Comment');
0c0cdc9d   Yarik   test
22
23
24
25
              $get = \Yii::$app->request->get();
              if(empty($post['comment_id']) && !empty($get['comment_id'])) {
                  $post['comment_id'] = $get['comment_id'];
              }
2d107e9e   Yarik   test
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
              if(!empty($post['comment_id'])) {
                  if($model = \common\modules\comment\models\Comment::findOne($post['comment_id'])) {
                      /**
                       * @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()) {
                          \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();
          }
  
          public function actionUpdate()
          {
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              $post = \Yii::$app->request->post();
              if(!empty($post['Comment']['comment_id'])) {
                  if($model = \common\modules\comment\models\Comment::findOne($post['Comment']['comment_id'])) {
                      /**
                       * @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);
                      if(empty($post['Comment']['comment_pid'])) {
                          $model->comment_pid = null;
                      }
                      if($model->updateComment()) {
                          \Yii::$app->response->data = ['text' => 'Comment successfully updated'];
                      } else {
                          \Yii::$app->response->data = ['error' => $model->hasErrors()?$model->getFirstErrors():'Cannot update message'];
                      }
                  }else {
                      \Yii::$app->response->data = ['error' => 'Comment not found'];
                  };
              } else {
                  \Yii::$app->response->data = ['error' => 'Missing comment_id'];
              }
              \Yii::$app->response->send();
          }
  
          public function actionForm()
          {
              $post = \Yii::$app->request->post('Comment');
              if(!empty($post['comment_id'])) {
                  $model = \common\modules\comment\models\Comment::find()->where(['comment_id' => $post['comment_id']])->with('parent', 'author')->one();
                  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()) {
                          return $this->renderAjax('@common/modules/comment/views/comment_form', [
                              'model' => $model,
                          ]);
                      } else {
                          \Yii::$app->response->data = ['error' => 'You are not able to update this comment'];
                      }
                  }else {
                      \Yii::$app->response->data = ['error' => 'Comment not found'];
                  };
              } else {
                  \Yii::$app->response->data = ['error' => 'Missing comment_id'];
              }
              \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              \Yii::$app->response->send();
          }
38a6e1dd   Yarik   test
100
          
2d107e9e   Yarik   test
101
      }