Blame view

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