1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\comments\widgets; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use yii\base\Widget; |
8
|
|
|
use yii\data\ArrayDataProvider; |
9
|
|
|
use yii\helpers\Json; |
10
|
|
|
use yii2mod\comments\CommentAsset; |
11
|
|
|
use yii2mod\comments\models\CommentModel; |
12
|
|
|
use yii2mod\comments\traits\ModuleTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Comment |
16
|
|
|
* |
17
|
|
|
* @package yii2mod\comments\widgets |
18
|
|
|
*/ |
19
|
|
|
class Comment extends Widget |
20
|
|
|
{ |
21
|
|
|
use ModuleTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \yii\db\ActiveRecord|null Widget model |
25
|
|
|
*/ |
26
|
|
|
public $model; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string relatedTo custom text, for example: cms url: about-us, john comment about us page, etc. |
30
|
|
|
* By default - class:primaryKey of the current model |
31
|
|
|
*/ |
32
|
|
|
public $relatedTo; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string the view file that will render the comment tree and form for posting comments |
36
|
|
|
*/ |
37
|
|
|
public $commentView = '@vendor/yii2mod/yii2-comments/widgets/views/index'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string comment form id |
41
|
|
|
*/ |
42
|
|
|
public $formId = 'comment-form'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string pjax container id |
46
|
|
|
*/ |
47
|
|
|
public $pjaxContainerId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var null|int maximum comments level, level starts from 1, null - unlimited level; |
51
|
|
|
*/ |
52
|
|
|
public $maxLevel = 10; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string entity id attribute |
56
|
|
|
*/ |
57
|
|
|
public $entityIdAttribute = 'id'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array DataProvider config |
61
|
|
|
*/ |
62
|
|
|
public $dataProviderConfig = [ |
63
|
|
|
'pagination' => [ |
64
|
|
|
'pageSize' => false, |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array ListView config |
70
|
|
|
*/ |
71
|
|
|
public $listViewConfig = [ |
72
|
|
|
'emptyText' => '', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array comment widget client options |
77
|
|
|
*/ |
78
|
|
|
public $clientOptions = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string hash(crc32) from class name of the widget model |
82
|
|
|
*/ |
83
|
|
|
protected $entity; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var int primary key value of the widget model |
87
|
|
|
*/ |
88
|
|
|
protected $entityId; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string encrypted entity |
92
|
|
|
*/ |
93
|
|
|
protected $encryptedEntity; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var string comment wrapper tag id |
97
|
|
|
*/ |
98
|
|
|
protected $commentWrapperId; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Initializes the widget params. |
102
|
|
|
*/ |
103
|
|
|
public function init() |
104
|
|
|
{ |
105
|
|
|
parent::init(); |
106
|
|
|
|
107
|
|
|
if (empty($this->model)) { |
108
|
|
|
throw new InvalidConfigException(Yii::t('yii2mod.comments', 'The "model" property must be set.')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (empty($this->pjaxContainerId)) { |
112
|
|
|
$this->pjaxContainerId = 'comment-pjax-container-' . $this->getId(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (empty($this->model->{$this->entityIdAttribute})) { |
116
|
|
|
throw new InvalidConfigException(Yii::t('yii2mod.comments', 'The "entityIdAttribute" value for widget model cannot be empty.')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->entity = hash('crc32', get_class($this->model)); |
120
|
|
|
$this->entityId = $this->model->{$this->entityIdAttribute}; |
121
|
|
|
|
122
|
|
|
if (empty($this->relatedTo)) { |
123
|
|
|
$this->relatedTo = get_class($this->model) . ':' . $this->entityId; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->encryptedEntity = $this->getEncryptedEntity(); |
127
|
|
|
$this->commentWrapperId = $this->entity . $this->entityId; |
128
|
|
|
|
129
|
|
|
$this->registerAssets(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Executes the widget. |
134
|
|
|
* |
135
|
|
|
* @return string the result of widget execution to be outputted |
136
|
|
|
*/ |
137
|
|
|
public function run() |
138
|
|
|
{ |
139
|
|
|
$commentClass = $this->getModule()->commentModelClass; |
140
|
|
|
$commentModel = Yii::createObject([ |
141
|
|
|
'class' => $commentClass, |
142
|
|
|
'entity' => $this->entity, |
143
|
|
|
'entityId' => $this->entityId, |
144
|
|
|
]); |
145
|
|
|
$commentDataProvider = $this->getCommentDataProvider($commentClass); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return $this->render($this->commentView, [ |
148
|
|
|
'commentDataProvider' => $commentDataProvider, |
149
|
|
|
'commentModel' => $commentModel, |
150
|
|
|
'maxLevel' => $this->maxLevel, |
151
|
|
|
'encryptedEntity' => $this->encryptedEntity, |
152
|
|
|
'pjaxContainerId' => $this->pjaxContainerId, |
153
|
|
|
'formId' => $this->formId, |
154
|
|
|
'listViewConfig' => $this->listViewConfig, |
155
|
|
|
'commentWrapperId' => $this->commentWrapperId, |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get encrypted entity |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function getEncryptedEntity() |
165
|
|
|
{ |
166
|
|
|
return utf8_encode(Yii::$app->getSecurity()->encryptByKey(Json::encode([ |
167
|
|
|
'entity' => $this->entity, |
168
|
|
|
'entityId' => $this->entityId, |
169
|
|
|
'relatedTo' => $this->relatedTo, |
170
|
|
|
]), $this->getModule()->id)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Register assets. |
175
|
|
|
*/ |
176
|
|
|
protected function registerAssets() |
177
|
|
|
{ |
178
|
|
|
$view = $this->getView(); |
179
|
|
|
CommentAsset::register($view); |
180
|
|
|
$view->registerJs("jQuery('#{$this->commentWrapperId}').comment({$this->getClientOptions()});"); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected function getClientOptions() |
187
|
|
|
{ |
188
|
|
|
$this->clientOptions['pjaxContainerId'] = '#' . $this->pjaxContainerId; |
189
|
|
|
$this->clientOptions['formSelector'] = '#' . $this->formId; |
190
|
|
|
|
191
|
|
|
return Json::encode($this->clientOptions); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get comment ArrayDataProvider |
196
|
|
|
* |
197
|
|
|
* @param CommentModel $commentClass |
198
|
|
|
* |
199
|
|
|
* @return ArrayDataProvider |
200
|
|
|
*/ |
201
|
|
|
protected function getCommentDataProvider($commentClass) |
202
|
|
|
{ |
203
|
|
|
$dataProvider = new ArrayDataProvider($this->dataProviderConfig); |
204
|
|
|
if (!isset($this->dataProviderConfig['allModels'])) { |
205
|
|
|
$dataProvider->allModels = $commentClass::getTree($this->entity, $this->entityId, $this->maxLevel); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $dataProvider; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|