a2cde075
Yarik
first commit
|
1
2
|
<?php
|
faff2c48
Yarik
Artbox comment cr...
|
3
|
namespace artbox\webcomment\widgets;
|
a2cde075
Yarik
first commit
|
4
|
|
faff2c48
Yarik
Artbox comment cr...
|
5
6
7
8
9
|
use artbox\webcomment\assets\CommentAsset;
use artbox\webcomment\models\CommentModel;
use artbox\webcomment\models\interfaces\CommentInterface;
use artbox\webcomment\models\RatingModel;
use artbox\webcomment\Module;
|
a2cde075
Yarik
first commit
|
10
11
12
13
14
15
16
17
18
19
20
21
|
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Class CommentWidget
|
faff2c48
Yarik
Artbox comment cr...
|
22
|
*
|
a2cde075
Yarik
first commit
|
23
|
* @property Model $model Model, to which comment attached
|
a2cde075
Yarik
first commit
|
24
25
26
|
*/
class CommentWidget extends Widget
{
|
b5639b59
Anastasia
comment widget
|
27
28
29
30
|
/**
* @var string what field is displayed in the user name
*/
public $display_name = 'username';
|
a2cde075
Yarik
first commit
|
31
32
33
|
/**
* Model, to which comment attached
|
faff2c48
Yarik
Artbox comment cr...
|
34
|
*
|
a2cde075
Yarik
first commit
|
35
36
37
38
39
40
|
* @var Model Model
*/
//public $model;
/**
* Options
|
faff2c48
Yarik
Artbox comment cr...
|
41
|
*
|
a2cde075
Yarik
first commit
|
42
43
44
45
46
47
48
49
50
51
|
* @var array
*/
public $options = [
'class' => 'artbox_comment_container comments-start',
'id' => 'artbox-comment',
];
/**
* @var string the view file that will render comment form.
*/
|
faff2c48
Yarik
Artbox comment cr...
|
52
|
public $formView = '@artbox/webcomment/views/artbox_comment_form';
|
a2cde075
Yarik
first commit
|
53
54
55
|
/**
* Form options
|
faff2c48
Yarik
Artbox comment cr...
|
56
|
*
|
a2cde075
Yarik
first commit
|
57
58
59
60
61
62
63
64
|
* @var array
*/
public $formOptions = [
'class' => 'artbox_form_container',
];
/**
* Params to be passed to form
|
faff2c48
Yarik
Artbox comment cr...
|
65
|
*
|
a2cde075
Yarik
first commit
|
66
67
68
69
70
71
72
|
* @var array
*/
public $formParams = [];
/**
* @var string the view file that will render comments list.
*/
|
faff2c48
Yarik
Artbox comment cr...
|
73
|
public $listView = '@artbox/webcomment/views/artbox_comment_list';
|
a2cde075
Yarik
first commit
|
74
75
76
|
/**
* List options
|
faff2c48
Yarik
Artbox comment cr...
|
77
|
*
|
a2cde075
Yarik
first commit
|
78
79
80
81
82
83
84
85
|
* @var array
*/
public $listOptions = [
'class' => 'artbox_list_container',
];
/**
* List params
|
faff2c48
Yarik
Artbox comment cr...
|
86
|
*
|
a2cde075
Yarik
first commit
|
87
88
89
90
91
92
|
* @var array
*/
public $listParams = [];
/**
* Reply options
|
faff2c48
Yarik
Artbox comment cr...
|
93
|
*
|
a2cde075
Yarik
first commit
|
94
95
96
97
98
99
100
101
102
|
* @var array
*/
public $replyOptions = [
'style' => 'display: none;',
'class' => 'artbox_comment_reply_container',
];
/**
* Reply view
|
faff2c48
Yarik
Artbox comment cr...
|
103
|
*
|
a2cde075
Yarik
first commit
|
104
105
|
* @var string
*/
|
faff2c48
Yarik
Artbox comment cr...
|
106
|
public $replyView = '@artbox/webcomment/views/artbox_comment_reply';
|
a2cde075
Yarik
first commit
|
107
108
109
|
/**
* Comment form ID. If you have multiple forms on the same page, please use unique IDs.
|
faff2c48
Yarik
Artbox comment cr...
|
110
|
*
|
a2cde075
Yarik
first commit
|
111
112
113
114
115
116
|
* @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.
|
faff2c48
Yarik
Artbox comment cr...
|
117
|
*
|
a2cde075
Yarik
first commit
|
118
119
120
121
122
123
|
* @var string List ID
*/
public $listId = 'artbox-comment-list';
/**
* Item view
|
faff2c48
Yarik
Artbox comment cr...
|
124
|
*
|
a2cde075
Yarik
first commit
|
125
126
|
* @var string
*/
|
faff2c48
Yarik
Artbox comment cr...
|
127
|
public $itemView = '@artbox/webcomment/views/artbox_comment_item';
|
a2cde075
Yarik
first commit
|
128
129
130
|
/**
* Item options
|
faff2c48
Yarik
Artbox comment cr...
|
131
|
*
|
a2cde075
Yarik
first commit
|
132
133
134
135
136
137
138
139
140
141
142
143
|
* @var array
*/
public $itemOptions = [
'class' => 'artbox_item_container',
'itemprop' => 'review',
'itemscope' => 'itemscope',
'itemtype' => 'http://schema.org/Review',
];
/**
* Entity ID attribute, default to primaryKey() if ActiveRecord and throws exception if not
* set
|
faff2c48
Yarik
Artbox comment cr...
|
144
|
*
|
a2cde075
Yarik
first commit
|
145
146
147
148
149
150
|
* @var string entity id attribute
*/
public $entityIdAttribute;
/**
* Info to be passed to Comment Model
|
faff2c48
Yarik
Artbox comment cr...
|
151
|
*
|
a2cde075
Yarik
first commit
|
152
153
|
* @var string $info Additional info
*/
|
faff2c48
Yarik
Artbox comment cr...
|
154
|
public $info = null;
|
a2cde075
Yarik
first commit
|
155
156
157
|
/**
* Client options to be passed to JS
|
faff2c48
Yarik
Artbox comment cr...
|
158
|
*
|
a2cde075
Yarik
first commit
|
159
160
161
162
163
|
* @var array comment widget client options
*/
public $clientOptions = [];
/**
|
a2cde075
Yarik
first commit
|
164
165
166
167
168
169
170
171
|
* @var string pjax container id
*/
public $pjaxContainerId;
public $layout = "<div class='comments-border'></div>{form} {reply_form} {list}";
/**
* Model fully namespaced classname
|
faff2c48
Yarik
Artbox comment cr...
|
172
|
*
|
a2cde075
Yarik
first commit
|
173
174
175
176
177
178
|
* @var string Model namespace
*/
protected $entity;
/**
* Entity ID for attached model
|
faff2c48
Yarik
Artbox comment cr...
|
179
|
*
|
a2cde075
Yarik
first commit
|
180
181
182
183
184
185
186
187
188
|
* @var integer Entity ID
*/
protected $entityId;
/**
* Encrypted data to be passed to Controller. Consist of:
* * Model::className()
* * entityId
* * info (optional)
|
faff2c48
Yarik
Artbox comment cr...
|
189
|
*
|
a2cde075
Yarik
first commit
|
190
191
192
193
194
195
|
* @var string encrypted entity key
*/
protected $encryptedEntityKey;
/**
* Parts for widget
|
faff2c48
Yarik
Artbox comment cr...
|
196
|
*
|
a2cde075
Yarik
first commit
|
197
198
199
200
201
202
203
204
205
206
|
* @var array $parts
*/
protected $parts;
/**
* Initializes the widget params.
*/
public function init()
{
// Module init
|
faff2c48
Yarik
Artbox comment cr...
|
207
208
209
210
|
$module = Yii::$app->getModule(Module::$name);
if (!$module) {
Module::registerMe();
}
|
a2cde075
Yarik
first commit
|
211
212
|
// Model init
$model = $this->getModel();
|
faff2c48
Yarik
Artbox comment cr...
|
213
214
|
if (empty($this->pjaxContainerId)) {
|
a2cde075
Yarik
first commit
|
215
216
217
218
219
|
$this->pjaxContainerId = 'comment-pjax-container-' . $this->getId();
}
$this->entity = $model::className();
// Entity ID init
|
faff2c48
Yarik
Artbox comment cr...
|
220
|
if (!empty($this->entityIdAttribute) && $this->model->hasProperty($this->entityIdAttribute)) {
|
a2cde075
Yarik
first commit
|
221
222
|
$this->entityId = $this->model->{$this->entityIdAttribute};
} else {
|
faff2c48
Yarik
Artbox comment cr...
|
223
|
if ($this->model instanceof ActiveRecord && !empty($this->model->getPrimaryKey())) {
|
a2cde075
Yarik
first commit
|
224
225
|
$this->entityId = (int) $this->model->getPrimaryKey();
} else {
|
faff2c48
Yarik
Artbox comment cr...
|
226
227
228
|
throw new InvalidConfigException(
Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.')
);
|
a2cde075
Yarik
first commit
|
229
230
231
232
233
234
235
236
237
238
239
|
}
}
// Generate encryptedEntityKey
$this->encryptedEntityKey = $this->generateEntityKey();
$this->registerAssets();
}
/**
* Executes the widget.
|
faff2c48
Yarik
Artbox comment cr...
|
240
|
*
|
a2cde075
Yarik
first commit
|
241
242
243
244
245
246
|
* @return string the result of widget execution to be outputted.
*/
public function run()
{
/* @var Module $module */
$module = Yii::$app->getModule(Module::$name);
|
faff2c48
Yarik
Artbox comment cr...
|
247
248
249
|
if (!$module) {
Module::registerMe();
}
|
a2cde075
Yarik
first commit
|
250
|
$commentModelClass = $module->commentModelClass;
|
faff2c48
Yarik
Artbox comment cr...
|
251
252
253
254
255
256
257
258
259
260
|
$commentModel = $this->createModel(
$commentModelClass,
[
'entity' => $this->entity,
'entityId' => $this->entityId,
'encryptedEntity' => $this->encryptedEntityKey,
'scenario' => \Yii::$app->user->getIsGuest(
) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER,
]
);
|
fd5e93c2
Yarik
Artbox comment pr...
|
261
|
if ($module->enableRating) {
|
a2cde075
Yarik
first commit
|
262
263
264
|
$ratingModelClass = $module->ratingModelClass;
$ratingModel = $this->createRating($ratingModelClass);
} else {
|
faff2c48
Yarik
Artbox comment cr...
|
265
266
267
268
269
270
|
$ratingModel = null;
}
if (method_exists($commentModelClass, 'getTree')) {
$comments = $commentModelClass::getTree($this->entity, $this->entityId);
} else {
$comments = [];
|
a2cde075
Yarik
first commit
|
271
|
}
|
a2cde075
Yarik
first commit
|
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
$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
|
faff2c48
Yarik
Artbox comment cr...
|
293
|
*
|
a2cde075
Yarik
first commit
|
294
295
296
297
298
|
* @return string
*/
protected function generateEntityKey()
{
return Yii::$app->getSecurity()
|
faff2c48
Yarik
Artbox comment cr...
|
299
300
301
302
303
304
305
306
307
308
|
->encryptByKey(
Json::encode(
[
'entity' => $this->entity,
'entity_id' => $this->entityId,
'info' => $this->info,
]
),
Module::$encryptionKey
);
|
a2cde075
Yarik
first commit
|
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
}
/**
* 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
*/
protected function createModel(string $className, array $config = []): CommentInterface
{
$options = array_merge($config, [ 'class' => $className ]);
$object = Yii::createObject($options);
|
faff2c48
Yarik
Artbox comment cr...
|
324
|
if ($object instanceof CommentInterface) {
|
a2cde075
Yarik
first commit
|
325
326
|
return $object;
}
|
faff2c48
Yarik
Artbox comment cr...
|
327
328
329
|
throw new InvalidConfigException(
Yii::t('artbox-comment', 'Comment model must be instance of CommentInterface.')
);
|
a2cde075
Yarik
first commit
|
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
}
/**
* Create rating model
*
* @param string $className Full namespaced model
* @param array $config Init config
*
* @return CommentInterface|RatingModel Comment model
* @throws InvalidConfigException If object not instance of \yii\base\Model
*/
protected function createRating(string $className, array $config = []): RatingModel
{
$options = array_merge($config, [ 'class' => $className ]);
$object = Yii::createObject($options);
|
faff2c48
Yarik
Artbox comment cr...
|
345
|
if ($object instanceof RatingModel) {
|
a2cde075
Yarik
first commit
|
346
347
|
return $object;
}
|
faff2c48
Yarik
Artbox comment cr...
|
348
349
350
|
throw new InvalidConfigException(
Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.')
);
|
a2cde075
Yarik
first commit
|
351
352
353
354
355
356
357
358
359
|
}
/**
* Build parts for rendering widget
*
* @param CommentInterface $commentModel
* @param ActiveDataProvider $comments
* @param null|RatingModel $ratingModel
*/
|
faff2c48
Yarik
Artbox comment cr...
|
360
|
protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = null)
|
a2cde075
Yarik
first commit
|
361
362
|
{
$form_options = $this->formOptions;
|
faff2c48
Yarik
Artbox comment cr...
|
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
$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,
'rating_model' => $ratingModel,
]
),
$form_options
);
if (!\Yii::$app->user->isGuest) {
|
a2cde075
Yarik
first commit
|
379
|
$reply_options = $this->replyOptions;
|
faff2c48
Yarik
Artbox comment cr...
|
380
381
382
383
384
385
386
387
388
389
390
391
392
|
$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
);
|
a2cde075
Yarik
first commit
|
393
394
395
|
}
$list_options = array_merge($this->listOptions, [ 'id' => $this->listId ]);
|
faff2c48
Yarik
Artbox comment cr...
|
396
397
398
399
400
401
402
403
404
405
406
|
$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,
|
b5639b59
Anastasia
comment widget
|
407
408
|
'display_name' => $this->display_name,
|
faff2c48
Yarik
Artbox comment cr...
|
409
410
411
412
|
]
),
$list_options
);
|
a2cde075
Yarik
first commit
|
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
}
/**
* @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);
}
public function setModel(Model $model)
{
$this->model = $model;
}
public function getModel(): Model
{
|
faff2c48
Yarik
Artbox comment cr...
|
437
|
if (!empty($this->model)) {
|
a2cde075
Yarik
first commit
|
438
439
|
return $this->model;
}
|
faff2c48
Yarik
Artbox comment cr...
|
440
|
throw new InvalidConfigException(Yii::t('artbox-comment', 'The "model" property must be set.'));
|
a2cde075
Yarik
first commit
|
441
442
|
}
}
|