Blame view

common/modules/comment/widgets/CommentWidget.php 12.6 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
5
6
7
8
9
10
  <?php
      
      namespace common\modules\comment\widgets;
      
      use common\modules\comment\assets\CommentAsset;
      use common\modules\comment\models\interfaces\CommentInterface;
      use common\modules\comment\models\RatingModel;
      use common\modules\comment\Module;
      use Yii;
      use yii\base\InvalidConfigException;
5c2eb7c8   Yarik   Big commit almost...
11
      use yii\base\Model;
d8c1a2e0   Yarik   Big commit artbox
12
13
      use yii\base\Widget;
      use yii\data\ActiveDataProvider;
5c2eb7c8   Yarik   Big commit almost...
14
      use yii\db\ActiveRecord;
d8c1a2e0   Yarik   Big commit artbox
15
16
17
18
19
20
      use yii\helpers\ArrayHelper;
      use yii\helpers\Html;
      use yii\helpers\Json;
      
      /**
       * Class CommentWidget
5c2eb7c8   Yarik   Big commit almost...
21
       * @property Model $model Model, to which comment attached
d8c1a2e0   Yarik   Big commit artbox
22
23
24
25
26
27
28
       * @package common\modules\comment\widgets
       */
      class CommentWidget extends Widget
      {
          
          /**
           * Model, to which comment attached
5c2eb7c8   Yarik   Big commit almost...
29
           * @var Model Model
d8c1a2e0   Yarik   Big commit artbox
30
31
32
33
34
35
36
           */
          //public $model;
          
          /**
           * Options
           * @var array
           */
5c2eb7c8   Yarik   Big commit almost...
37
38
39
40
          public $options = [
              'class' => 'artbox_comment_container comments-start',
              'id'    => 'artbox-comment',
          ];
d8c1a2e0   Yarik   Big commit artbox
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
          
          /**
           * @var string the view file that will render comment form.
           */
          public $formView = '@artbox-comment/views/artbox_comment_form';
          
          /**
           * Form options
           * @var array
           */
          public $formOptions = [
              'class' => 'artbox_form_container',
          ];
          
          /**
           * Params to be passed to form
           * @var array
           */
5c2eb7c8   Yarik   Big commit almost...
59
          public $formParams = [];
d8c1a2e0   Yarik   Big commit artbox
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
          
          /**
           * @var string the view file that will render comments list.
           */
          public $listView = '@artbox-comment/views/artbox_comment_list';
          
          /**
           * List options
           * @var array
           */
          public $listOptions = [
              'class' => 'artbox_list_container',
          ];
          
          /**
           * List params
           * @var array
           */
5c2eb7c8   Yarik   Big commit almost...
78
          public $listParams = [];
d8c1a2e0   Yarik   Big commit artbox
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
          
          /**
           * Reply options
           * @var array
           */
          public $replyOptions = [
              'style' => 'display: none;',
              'class' => 'artbox_comment_reply_container',
          ];
          
          /**
           * Reply view
           * @var string
           */
          public $replyView = '@artbox-comment/views/artbox_comment_reply';
          
          /**
           * Comment form ID. If you have multiple forms on the same page, please use unique IDs.
           * @var string Form ID
           */
          public $formId = 'artbox-comment-form';
          
          /**
           * Comment list ID. If you have multiple forms on the same page, please use unique IDs.
           * @var string List ID
           */
          public $listId = 'artbox-comment-list';
          
          /**
           * Item view
           * @var string
           */
          public $itemView = '@artbox-comment/views/artbox_comment_item';
          
          /**
           * Item options
           * @var array
           */
          public $itemOptions = [
5c2eb7c8   Yarik   Big commit almost...
118
119
              'class'     => 'artbox_item_container',
              'itemprop'  => 'review',
d8c1a2e0   Yarik   Big commit artbox
120
              'itemscope' => 'itemscope',
5c2eb7c8   Yarik   Big commit almost...
121
              'itemtype'  => 'http://schema.org/Review',
d8c1a2e0   Yarik   Big commit artbox
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
          ];
          
          /**
           * Entity ID attribute, default to primaryKey() if ActiveRecord and throws exception if not
           * set
           * @var string entity id attribute
           */
          public $entityIdAttribute;
          
          /**
           * Info to be passed to Comment Model
           * @var string $info Additional info
           */
          public $info = NULL;
          
          /**
           * Client options to be passed to JS
           * @var array comment widget client options
           */
5c2eb7c8   Yarik   Big commit almost...
141
          public $clientOptions = [];
d8c1a2e0   Yarik   Big commit artbox
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
          
          /**
           * @todo Check if needed
           * @var string pjax container id
           */
          public $pjaxContainerId;
          
          public $layout = "<div class='comments-border'></div>{form} {reply_form} {list}";
          
          /**
           * Model fully namespaced classname
           * @var string Model namespace
           */
          protected $entity;
          
          /**
           * Entity ID for attached model
           * @var integer Entity ID
           */
          protected $entityId;
          
          /**
           * Encrypted data to be passed to Controller. Consist of:
           * * Model::className()
           * * entityId
           * * info (optional)
           * @var string encrypted entity key
           */
          protected $encryptedEntityKey;
          
          /**
           * Parts for widget
           * @var array $parts
           */
          protected $parts;
          
          /**
           * Initializes the widget params.
           */
          public function init()
          {
              // Module init
              Yii::$app->getModule(Module::$name);
              // Model init
              $model = $this->getModel();
              
              /**
               * @todo Check if needed
               */
              if(empty( $this->pjaxContainerId )) {
                  $this->pjaxContainerId = 'comment-pjax-container-' . $this->getId();
              }
              
              $this->entity = $model::className();
              // Entity ID init
              if(!empty( $this->entityIdAttribute ) && $this->model->hasProperty($this->entityIdAttribute)) {
                  $this->entityId = $this->model->{$this->entityIdAttribute};
              } else {
5c2eb7c8   Yarik   Big commit almost...
200
                  if($this->model instanceof ActiveRecord && !empty( $this->model->getPrimaryKey() )) {
d8c1a2e0   Yarik   Big commit artbox
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
                      $this->entityId = (int) $this->model->getPrimaryKey();
                  } else {
                      throw new InvalidConfigException(/*Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.')*/);
                  }
              }
              
              // Generate encryptedEntityKey
              $this->encryptedEntityKey = $this->generateEntityKey();
              
              $this->registerAssets();
          }
          
          /**
           * Executes the widget.
           * @return string the result of widget execution to be outputted.
           */
          public function run()
          {
              /* @var Module $module */
              $module = Yii::$app->getModule(Module::$name);
              $commentModelClass = $module->commentModelClass;
              $commentModel = $this->createModel($commentModelClass, [
                  'entity'          => $this->entity,
                  'entityId'        => $this->entityId,
                  'encryptedEntity' => $this->encryptedEntityKey,
5c2eb7c8   Yarik   Big commit almost...
226
                  'scenario'        => \Yii::$app->user->getIsGuest() ? $commentModelClass::SCENARIO_GUEST : $commentModelClass::SCENARIO_USER,
d8c1a2e0   Yarik   Big commit artbox
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
              ]);
              if($module::$enableRating) {
                  $ratingModelClass = $module->ratingModelClass;
                  $ratingModel = $this->createRating($ratingModelClass);
              } else {
                  $ratingModel = NULL;
              }
              
              $comments = $commentModelClass::getTree($this->entity, $this->entityId);
              
              $this->buildParts($commentModel, $comments, $ratingModel);
              
              return $this->renderWidget();
          }
          
          /**
           * Register assets.
           */
          protected function registerAssets()
          {
              $this->clientOptions[ 'formSelector' ] = '#' . $this->formId;
              $this->clientOptions[ 'listSelector' ] = '#' . $this->listId;
              $options = Json::encode($this->clientOptions);
              $view = $this->getView();
              CommentAsset::register($view);
              $view->registerJs("jQuery('#{$this->formId}').artbox_comment({$options});");
          }
          
          /**
           * Get encrypted entity key
           * @return string
           */
          protected function generateEntityKey()
          {
              return Yii::$app->getSecurity()
                              ->encryptByKey(Json::encode([
                                  'entity'    => $this->entity,
                                  'entity_id' => $this->entityId,
                                  'info'      => $this->info,
                              ]), Module::$encryptionKey);
          }
          
          /**
           * Create comment model
           *
           * @param string $className Full namespaced model
           * @param array  $config    Init config
           *
           * @return CommentInterface Comment model
           * @throws InvalidConfigException If object not instance of \yii\base\Model
           */
5c2eb7c8   Yarik   Big commit almost...
278
          protected function createModel(string $className, array $config = []): CommentInterface
d8c1a2e0   Yarik   Big commit artbox
279
280
281
282
283
284
285
286
          {
              $options = array_merge($config, [ 'class' => $className ]);
              $object = Yii::createObject($options);
              if($object instanceof CommentInterface) {
                  return $object;
              }
              throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'Comment model must be instance of CommentInterface.\')*/);
          }
5c2eb7c8   Yarik   Big commit almost...
287
          
d8c1a2e0   Yarik   Big commit artbox
288
289
290
291
292
293
          /**
           * Create rating model
           *
           * @param string $className Full namespaced model
           * @param array  $config    Init config
           *
5c2eb7c8   Yarik   Big commit almost...
294
           * @return CommentInterface|RatingModel Comment model
d8c1a2e0   Yarik   Big commit artbox
295
296
           * @throws InvalidConfigException If object not instance of \yii\base\Model
           */
5c2eb7c8   Yarik   Big commit almost...
297
          protected function createRating(string $className, array $config = []): RatingModel
d8c1a2e0   Yarik   Big commit artbox
298
299
300
301
302
303
          {
              $options = array_merge($config, [ 'class' => $className ]);
              $object = Yii::createObject($options);
              if($object instanceof RatingModel) {
                  return $object;
              }
5c2eb7c8   Yarik   Big commit almost...
304
              throw new InvalidConfigException(Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.'));
d8c1a2e0   Yarik   Big commit artbox
305
306
307
308
309
310
311
          }
          
          /**
           * Build parts for rendering widget
           *
           * @param CommentInterface   $commentModel
           * @param ActiveDataProvider $comments
5c2eb7c8   Yarik   Big commit almost...
312
           * @param null|RatingModel   $ratingModel
d8c1a2e0   Yarik   Big commit artbox
313
314
315
316
317
318
319
320
321
           */
          protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = NULL)
          {
              $form_options = $this->formOptions;
              $this->parts[ 'form' ] = Html::tag(ArrayHelper::remove($form_options, 'tag', 'div'), $this->render($this->formView, [
                  'comment_model' => $commentModel,
                  'form_params'   => $this->formParams,
                  'model'         => $this->getModel(),
                  'formId'        => $this->formId,
5c2eb7c8   Yarik   Big commit almost...
322
                  'rating_model'  => $ratingModel,
d8c1a2e0   Yarik   Big commit artbox
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
              ]), $form_options);
              
              if(!\Yii::$app->user->isGuest) {
                  $reply_options = $this->replyOptions;
                  $this->parts[ 'reply_form' ] = Html::tag(ArrayHelper::remove($reply_options, 'tag', 'div'), $this->render($this->replyView, [
                      'comment_model' => $commentModel,
                      'form_params'   => $this->formParams,
                      'model'         => $this->getModel(),
                      'formId'        => $this->formId,
                  ]), $reply_options);
              }
              
              $list_options = array_merge($this->listOptions, [ 'id' => $this->listId ]);
              $this->parts[ 'list' ] = Html::tag(ArrayHelper::remove($list_options, 'tag', 'div'), $this->render($this->listView, [
                  'comment_model' => $commentModel,
                  'list_params'   => $this->listParams,
                  'model'         => $this->getModel(),
                  'comments'      => $comments,
                  'item_options'  => $this->itemOptions,
                  'item_view'     => $this->itemView,
              ]), $list_options);
          }
          
          /**
           * @return string
           */
          protected function renderWidget(): string
          {
              $layout = $this->layout;
              $parts = $this->parts;
              $options = $this->options;
              $layout = preg_replace('/{list}/', ArrayHelper::getValue($parts, 'list', ''), $layout);
              $layout = preg_replace('/{form}/', ArrayHelper::getValue($parts, 'form', ''), $layout);
              $layout = preg_replace('/{reply_form}/', ArrayHelper::getValue($parts, 'reply_form', ''), $layout);
              $tag = ArrayHelper::remove($options, 'tag', 'div');
              return Html::tag($tag, $layout, $options);
          }
          
5c2eb7c8   Yarik   Big commit almost...
361
          public function setModel(Model $model)
d8c1a2e0   Yarik   Big commit artbox
362
363
364
365
          {
              $this->model = $model;
          }
          
5c2eb7c8   Yarik   Big commit almost...
366
          public function getModel(): Model
d8c1a2e0   Yarik   Big commit artbox
367
368
369
370
371
372
373
          {
              if(!empty( $this->model )) {
                  return $this->model;
              }
              throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'The "model" property must be set.\')*/);
          }
      }