Blame view

controllers/DefaultController.php 5.92 KB
a2cde075   Yarik   first commit
1
  <?php
a2cde075   Yarik   first commit
2
      
faff2c48   Yarik   Artbox comment cr...
3
4
5
6
7
      namespace artbox\webcomment\controllers;
      
      use artbox\webcomment\models\CommentModel;
      use artbox\webcomment\models\RatingModel;
      use artbox\webcomment\Module;
a2cde075   Yarik   first commit
8
9
10
11
12
13
14
15
16
17
18
19
      use yii\filters\AccessControl;
      use yii\filters\VerbFilter;
      use yii\helpers\Json;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use yii\web\Response;
      
      class DefaultController extends Controller
      {
          
          /**
           * Returns a list of behaviors that this component should behave as.
faff2c48   Yarik   Artbox comment cr...
20
           *
a2cde075   Yarik   first commit
21
22
23
24
25
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
           * @return array
           */
          public function behaviors()
          {
              return [
                  'verbs'  => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'create' => [ 'post' ],
                          'delete' => [
                              'post',
                              'delete',
                          ],
                      ],
                  ],
                  'access' => [
                      'class' => AccessControl::className(),
                      'only'  => [ 'delete' ],
                      'rules' => [
                          [
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
                      ],
                  ],
              ];
          }
      
          /**
           * Create comment.
           *
           * @param string $entity
           *
faff2c48   Yarik   Artbox comment cr...
54
55
           * @return array|null|\yii\web\Response
           * @throws \yii\base\InvalidConfigException
a2cde075   Yarik   first commit
56
57
58
59
60
61
           */
          public function actionCreate(string $entity)
          {
              \Yii::$app->response->format = Response::FORMAT_JSON;
              /* @var $module Module */
              $module = \Yii::$app->getModule(Module::$name);
faff2c48   Yarik   Artbox comment cr...
62
63
64
              if (!$module) {
                  Module::registerMe();
              }
a2cde075   Yarik   first commit
65
66
              $entity_data_json = \Yii::$app->getSecurity()
                                            ->decryptByKey($entity, $module::$encryptionKey);
faff2c48   Yarik   Artbox comment cr...
67
              if ($entity_data_json != false) {
a2cde075   Yarik   first commit
68
69
70
71
72
                  $entity_data = Json::decode($entity_data_json);
                  $commentModelClass = $module->commentModelClass;
                  /**
                   * @var CommentModel $model
                   */
faff2c48   Yarik   Artbox comment cr...
73
74
75
76
77
78
79
                  $model = new $commentModelClass(
                      [
                          'scenario' => \Yii::$app->user->getIsGuest(
                          ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER,
                      ]
                  );
                  if ($model->load(\Yii::$app->request->post())) {
a2cde075   Yarik   first commit
80
                      $model->setAttributes($entity_data);
faff2c48   Yarik   Artbox comment cr...
81
                      if ($model->save()) {
fd5e93c2   Yarik   Artbox comment pr...
82
                          if (empty($model->parent_id) && $module->enableRating) {
a2cde075   Yarik   first commit
83
84
85
86
                              $ratingModelClass = $module->ratingModelClass;
                              /**
                               * @var RatingModel $rating
                               */
faff2c48   Yarik   Artbox comment cr...
87
88
89
90
91
92
93
                              $rating = new $ratingModelClass(
                                  [
                                      'model'    => $model::className(),
                                      'model_id' => $model->primaryKey,
                                  ]
                              );
                              if ($rating->load(\Yii::$app->request->post())) {
a2cde075   Yarik   first commit
94
95
96
                                  $rating->save();
                              }
                          }
faff2c48   Yarik   Artbox comment cr...
97
98
99
100
                          \Yii::$app->session->setFlash(
                              'artbox_comment_success',
                              \Yii::t('artbox-comment', 'Comment posted')
                          );
a2cde075   Yarik   first commit
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
                          return [ 'status' => 'success' ];
                      } else {
                          return [
                              'status' => 'error',
                              'errors' => $model->getFirstErrors(),
                          ];
                      }
                  }
              }
              return [
                  'status'  => 'error',
                  'message' => \Yii::t('artbox-comment', 'Oops, something went wrong. Please try again later.'),
              ];
          }
          
          /**
           * Delete comment.
           *
           * @param integer $id Comment ID
           *
faff2c48   Yarik   Artbox comment cr...
121
           * @return array Comment text
a2cde075   Yarik   first commit
122
123
124
125
126
           */
          public function actionDelete($id)
          {
              \Yii::$app->response->format = Response::FORMAT_JSON;
              $model = $this->findModel($id);
faff2c48   Yarik   Artbox comment cr...
127
              if ($model->deleteComment()) {
a2cde075   Yarik   first commit
128
129
                  return [
                      'status'  => 'success',
faff2c48   Yarik   Artbox comment cr...
130
                      'message' => \Yii::t('artbox-comment', 'Comment has been deleted.'),
a2cde075   Yarik   first commit
131
132
133
                  ];
              } else {
                  \Yii::$app->response->setStatusCode(500);
faff2c48   Yarik   Artbox comment cr...
134
135
136
137
                  return [
                      'status'  => 'error',
                      'message' => \Yii::t('artbox-comment', 'Comment has not been deleted. Please try again!'),
                  ];
a2cde075   Yarik   first commit
138
139
140
141
142
143
144
145
              }
          }
          
          /**
           * Find model by ID.
           *
           * @param integer|array $id Comment ID
           *
faff2c48   Yarik   Artbox comment cr...
146
147
148
           * @return \artbox\webcomment\models\CommentModel
           * @throws \yii\base\InvalidConfigException
           * @throws \yii\web\NotFoundHttpException
a2cde075   Yarik   first commit
149
150
151
           */
          protected function findModel(int $id): CommentModel
          {
faff2c48   Yarik   Artbox comment cr...
152
153
154
155
156
157
158
              /**
               * @var Module $module
               */
              $module = \Yii::$app->getModule(Module::$name);
              if (!$module) {
                  Module::registerMe();
              }
a2cde075   Yarik   first commit
159
              /** @var CommentModel $model */
faff2c48   Yarik   Artbox comment cr...
160
161
162
163
164
              $commentModelClass = $module->commentModelClass;
              if (method_exists($commentModelClass, 'findOne')) {
                  if ($model = $commentModelClass::findOne($id) !== null) {
                      return $model;
                  }
a2cde075   Yarik   first commit
165
              }
faff2c48   Yarik   Artbox comment cr...
166
              throw new NotFoundHttpException(\Yii::t('artbox-comment', 'The requested page does not exist.'));
a2cde075   Yarik   first commit
167
168
          }
      }