-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathComment.php
210 lines (177 loc) · 5.37 KB
/
Comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace yii2mod\comments\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\data\ArrayDataProvider;
use yii\helpers\Json;
use yii2mod\comments\CommentAsset;
use yii2mod\comments\models\CommentModel;
use yii2mod\comments\traits\ModuleTrait;
/**
* Class Comment
*
* @package yii2mod\comments\widgets
*/
class Comment extends Widget
{
use ModuleTrait;
/**
* @var \yii\db\ActiveRecord|null Widget model
*/
public $model;
/**
* @var string relatedTo custom text, for example: cms url: about-us, john comment about us page, etc.
* By default - class:primaryKey of the current model
*/
public $relatedTo;
/**
* @var string the view file that will render the comment tree and form for posting comments
*/
public $commentView = '@vendor/yii2mod/yii2-comments/widgets/views/index';
/**
* @var string comment form id
*/
public $formId = 'comment-form';
/**
* @var string pjax container id
*/
public $pjaxContainerId;
/**
* @var null|int maximum comments level, level starts from 1, null - unlimited level;
*/
public $maxLevel = 10;
/**
* @var string entity id attribute
*/
public $entityIdAttribute = 'id';
/**
* @var array DataProvider config
*/
public $dataProviderConfig = [
'pagination' => [
'pageSize' => false,
],
];
/**
* @var array ListView config
*/
public $listViewConfig = [
'emptyText' => '',
];
/**
* @var array comment widget client options
*/
public $clientOptions = [];
/**
* @var string hash(crc32) from class name of the widget model
*/
protected $entity;
/**
* @var int primary key value of the widget model
*/
protected $entityId;
/**
* @var string encrypted entity
*/
protected $encryptedEntity;
/**
* @var string comment wrapper tag id
*/
protected $commentWrapperId;
/**
* Initializes the widget params.
*/
public function init()
{
parent::init();
if (empty($this->model)) {
throw new InvalidConfigException(Yii::t('yii2mod.comments', 'The "model" property must be set.'));
}
if (empty($this->pjaxContainerId)) {
$this->pjaxContainerId = 'comment-pjax-container-' . $this->getId();
}
if (empty($this->model->{$this->entityIdAttribute})) {
throw new InvalidConfigException(Yii::t('yii2mod.comments', 'The "entityIdAttribute" value for widget model cannot be empty.'));
}
$this->entity = hash('crc32', get_class($this->model));
$this->entityId = $this->model->{$this->entityIdAttribute};
if (empty($this->relatedTo)) {
$this->relatedTo = get_class($this->model) . ':' . $this->entityId;
}
$this->encryptedEntity = $this->getEncryptedEntity();
$this->commentWrapperId = $this->entity . $this->entityId;
$this->registerAssets();
}
/**
* Executes the widget.
*
* @return string the result of widget execution to be outputted
*/
public function run()
{
$commentClass = $this->getModule()->commentModelClass;
$commentModel = Yii::createObject([
'class' => $commentClass,
'entity' => $this->entity,
'entityId' => $this->entityId,
]);
$commentDataProvider = $this->getCommentDataProvider($commentClass);
return $this->render($this->commentView, [
'commentDataProvider' => $commentDataProvider,
'commentModel' => $commentModel,
'maxLevel' => $this->maxLevel,
'encryptedEntity' => $this->encryptedEntity,
'pjaxContainerId' => $this->pjaxContainerId,
'formId' => $this->formId,
'listViewConfig' => $this->listViewConfig,
'commentWrapperId' => $this->commentWrapperId,
]);
}
/**
* Get encrypted entity
*
* @return string
*/
protected function getEncryptedEntity()
{
return utf8_encode(Yii::$app->getSecurity()->encryptByKey(Json::encode([
'entity' => $this->entity,
'entityId' => $this->entityId,
'relatedTo' => $this->relatedTo,
]), $this->getModule()->id));
}
/**
* Register assets.
*/
protected function registerAssets()
{
$view = $this->getView();
CommentAsset::register($view);
$view->registerJs("jQuery('#{$this->commentWrapperId}').comment({$this->getClientOptions()});");
}
/**
* @return string
*/
protected function getClientOptions()
{
$this->clientOptions['pjaxContainerId'] = '#' . $this->pjaxContainerId;
$this->clientOptions['formSelector'] = '#' . $this->formId;
return Json::encode($this->clientOptions);
}
/**
* Get comment ArrayDataProvider
*
* @param CommentModel $commentClass
*
* @return ArrayDataProvider
*/
protected function getCommentDataProvider($commentClass)
{
$dataProvider = new ArrayDataProvider($this->dataProviderConfig);
if (!isset($this->dataProviderConfig['allModels'])) {
$dataProvider->allModels = $commentClass::getTree($this->entity, $this->entityId, $this->maxLevel);
}
return $dataProvider;
}
}