b82db04a
Yarik
test
|
1
2
3
|
<?php
namespace common\modules\comment\widgets;
|
8a551494
Yarik
test
|
4
|
use yii\data\Sort;
|
b82db04a
Yarik
test
|
5
6
7
8
9
10
11
|
use \yii\helpers\ArrayHelper;
use \yii\helpers\Html;
class CommentWidget extends \yii\base\Widget
{
/**
|
8a551494
Yarik
test
|
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
|
* 'comment_container' - apply to container of one comment. Must have data-key and data-form
* 'comment_delete' - apply to comment delete link
* 'comment_reply' - apply to comment reply link
* 'comment_author' - apply to comment author text wrapper
* 'widget_container' - apply to comment widget
* 'form_container' - apply to comment form wrapper
* 'reply_block' - apply to reply block of comment form
* 'reply_author' - apply to reply author text block inside of reply block of comment form
* 'comment_update' - apply to comment update link
* 'comment_update_submit' - apply to submit button of comment update form
*/
static $baseClass = [
'comment_container' => 'artbox_comment_container',
'comment_delete' => 'artbox_comment_delete',
'comment_reply' => 'artbox_comment_reply',
'comment_author' => 'artbox_comment_author',
'widget_container' => 'artbox_comment_widget',
'form_container' => 'artbox_comment_form',
'reply_block' => 'artbox_comment_reply_block',
'reply_author' => 'artbox_comment_reply_author',
'comment_update' => 'artbox_comment_update',
'comment_update_submit' => 'artbox_comment_update_submit',
];
/**
|
2d107e9e
Yarik
test
|
37
38
39
40
41
|
* @var null|\yii\web\View
*/
public $context = NULL;
/**
|
b82db04a
Yarik
test
|
42
43
|
* @var array Parts of widgets that can be rendered
*/
|
2d107e9e
Yarik
test
|
44
45
46
47
48
|
public $parts = [ ];
public $rating_class = NULL;
public $rating_options = [ ];
|
b82db04a
Yarik
test
|
49
50
51
52
53
54
55
56
57
|
/**
* @var string|\common\modules\comment\models\Comment
*/
public $comment_class;
/**
* @var array
*/
|
2d107e9e
Yarik
test
|
58
|
public $class_options = [ ];
|
b82db04a
Yarik
test
|
59
|
|
b5becf98
Yarik
test
|
60
61
|
public $provider_options = [ ];
|
b82db04a
Yarik
test
|
62
63
64
65
66
67
|
/**
* @var bool Wheather to display comment list
*/
public $display_comment_list = true;
/**
|
2d107e9e
Yarik
test
|
68
|
* @var bool Whether to display comment form
|
b82db04a
Yarik
test
|
69
70
71
|
*/
public $display_comment_form = true;
|
2d107e9e
Yarik
test
|
72
73
74
|
/**
* @var bool Whether to display success text
*/
|
b82db04a
Yarik
test
|
75
76
|
public $display_comment_success = true;
|
2d107e9e
Yarik
test
|
77
78
79
|
/**
* @var bool Whether to allow one user post multiple comments
*/
|
b82db04a
Yarik
test
|
80
|
public $allow_multiple = true;
|
b5becf98
Yarik
test
|
81
|
|
8a551494
Yarik
test
|
82
|
public $allow_reply = true;
|
b82db04a
Yarik
test
|
83
|
|
2d107e9e
Yarik
test
|
84
85
86
|
/**
* @var array Options sent to list part
*/
|
b82db04a
Yarik
test
|
87
|
public $list_options = [
|
2d107e9e
Yarik
test
|
88
89
|
'tag' => 'div',
'view' => 'list-comment',
|
b82db04a
Yarik
test
|
90
91
92
|
'class' => 'test-class',
];
|
2d107e9e
Yarik
test
|
93
94
95
|
/**
* @var array Options sent to success part
*/
|
b82db04a
Yarik
test
|
96
|
public $success_options = [
|
2d107e9e
Yarik
test
|
97
98
99
|
'tag' => 'div',
'content' => NULL,
'class' => 'test-class-success',
|
b82db04a
Yarik
test
|
100
101
|
];
|
2d107e9e
Yarik
test
|
102
103
104
|
/**
* @var array Options sent to form part
*/
|
b82db04a
Yarik
test
|
105
|
public $form_options = [
|
2d107e9e
Yarik
test
|
106
107
|
'tag' => 'div',
'view' => 'form-comment',
|
b82db04a
Yarik
test
|
108
109
110
|
'class' => 'test-class-form',
];
|
2d107e9e
Yarik
test
|
111
112
113
|
/**
* @var bool Indicates whether any successful action happened
*/
|
b82db04a
Yarik
test
|
114
115
|
protected $isSuccess = false;
|
2d107e9e
Yarik
test
|
116
117
118
|
public $success_text = 'Comment successfully added';
/**
|
2fd40ee7
Yarik
test
|
119
|
* @var string $model Model, to which comments attached
|
2d107e9e
Yarik
test
|
120
|
*/
|
2fd40ee7
Yarik
test
|
121
122
123
124
125
126
|
public $model;
/**
* @var integer $model_id Model id, to which comments attached
*/
public $model_id;
|
b82db04a
Yarik
test
|
127
|
|
2d107e9e
Yarik
test
|
128
129
130
131
|
/**
* @var string Template of the widget. You may use <code>{success}, {form}, {list}</code>
* to render particular parts. You are also able to use common HTML here.
*/
|
b82db04a
Yarik
test
|
132
133
|
public $template = "{success}\n{form}\n{list}";
|
2d107e9e
Yarik
test
|
134
135
136
137
|
/**
* @var array Widget options
*/
public $options = [ ];
|
b82db04a
Yarik
test
|
138
139
|
/**
|
2d107e9e
Yarik
test
|
140
|
* @var \yii\data\DataProviderInterface Data provider of comments
|
b82db04a
Yarik
test
|
141
142
143
144
145
146
147
148
149
|
*/
public $dataProvider;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
|
2d107e9e
Yarik
test
|
150
|
\common\modules\comment\assets\CommentAsset::register($this->view);
|
b82db04a
Yarik
test
|
151
152
|
if(is_string($this->comment_class)) {
$this->comment_class = new $this->comment_class($this->class_options);
|
2d107e9e
Yarik
test
|
153
154
155
156
157
158
159
|
} else {
throw new \yii\base\InvalidConfigException(__CLASS__ . '->comment_class must be defined as object full class name string.');
}
if(!empty( $this->rating_class ) && is_string($this->rating_class)) {
$this->rating_class = new $this->rating_class($this->rating_options);
} elseif(!empty( $this->rating_class )) {
throw new \yii\base\InvalidConfigException(__CLASS__ . '->rating_class must be defined as object full class name string.');
|
b82db04a
Yarik
test
|
160
|
}
|
2fd40ee7
Yarik
test
|
161
162
|
$this->comment_class->model = $this->model;
$this->comment_class->model_id = $this->model_id;
|
b82db04a
Yarik
test
|
163
|
$this->createDataProvider();
|
2d107e9e
Yarik
test
|
164
|
$this->process();
|
b82db04a
Yarik
test
|
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
ob_start();
}
/**
* @inheritdoc
* @return string
*/
public function run()
{
$content = ob_get_clean();
$this->createParts();
return $this->renderWidget();
}
|
2d107e9e
Yarik
test
|
179
180
|
public function createParts()
{
|
b82db04a
Yarik
test
|
181
182
|
if($this->display_comment_success && $this->isSuccess) {
$tag = ArrayHelper::remove($this->success_options, 'tag', 'div');
|
2d107e9e
Yarik
test
|
183
|
if(is_callable($this->success_options[ 'content' ])) {
|
b82db04a
Yarik
test
|
184
|
$result = call_user_func(ArrayHelper::remove($this->success_options, 'content'), $this->success_text);
|
2d107e9e
Yarik
test
|
185
|
} elseif($this->success_options[ 'content' ] != NULL) {
|
b82db04a
Yarik
test
|
186
187
188
189
|
$result = Html::encode(ArrayHelper::remove($this->success_options, 'content', $this->success_text));
} else {
$result = Html::encode($this->success_text);
}
|
2d107e9e
Yarik
test
|
190
191
|
$this->parts[ 'success' ] = Html::tag($tag, $result, $this->success_options);
unset( $tag, $result );
|
b82db04a
Yarik
test
|
192
193
194
195
196
|
}
if($this->display_comment_list) {
$tag = ArrayHelper::remove($this->list_options, 'tag', 'div');
$view = ArrayHelper::remove($this->list_options, 'view');
|
2d107e9e
Yarik
test
|
197
|
$this->parts[ 'list' ] = Html::tag($tag, $this->renderItems($view), $this->list_options);
|
b82db04a
Yarik
test
|
198
199
|
}
|
38a6e1dd
Yarik
test
|
200
|
if($this->display_comment_form && $this->comment_class->checkCreate()) {
|
b82db04a
Yarik
test
|
201
202
|
$tag = ArrayHelper::remove($this->form_options, 'tag', 'div');
$view = ArrayHelper::remove($this->form_options, 'view');
|
b5becf98
Yarik
test
|
203
204
|
if(!empty( $this->form_options[ 'class' ] )) {
$this->form_options[ 'class' ] .= ' ' . self::$baseClass[ 'form_container' ];
|
8a551494
Yarik
test
|
205
|
} else {
|
b5becf98
Yarik
test
|
206
|
$this->form_options[ 'class' ] = self::$baseClass[ 'form_container' ];
|
8a551494
Yarik
test
|
207
|
}
|
2d107e9e
Yarik
test
|
208
|
$this->parts[ 'form' ] = Html::tag($tag, $this->renderForm($view), $this->form_options);
|
b82db04a
Yarik
test
|
209
210
211
212
213
214
|
}
}
public function createDataProvider()
{
$this->dataProvider = new \yii\data\ActiveDataProvider([
|
b5becf98
Yarik
test
|
215
216
|
'query' => ArrayHelper::remove($this->provider_options, 'query', $this->comment_class->getComments($this->model, $this->model_id)),
'sort' => ArrayHelper::remove($this->provider_options, 'sort', new Sort([
|
8a551494
Yarik
test
|
217
218
219
|
'defaultOrder' => [
'date_add' => SORT_DESC,
],
|
b5becf98
Yarik
test
|
220
|
])),
|
ed4ee1f2
Yarik
test
|
221
|
'pagination' => ArrayHelper::remove($this->provider_options, 'pagination', [
|
b82db04a
Yarik
test
|
222
|
'pageSize' => 10,
|
b5becf98
Yarik
test
|
223
|
]),
|
b82db04a
Yarik
test
|
224
225
226
|
]);
}
|
2d107e9e
Yarik
test
|
227
228
229
|
public function renderItems($view)
{
if(empty( $view )) {
|
b82db04a
Yarik
test
|
230
231
|
throw new \yii\base\InvalidConfigException("list_options[view] must be set");
}
|
8a551494
Yarik
test
|
232
233
234
235
|
return $this->render($view, [
'dataProvider' => $this->dataProvider,
'commentClass' => $this->comment_class,
]);
|
b82db04a
Yarik
test
|
236
237
|
}
|
2d107e9e
Yarik
test
|
238
239
240
|
public function renderForm($view)
{
if(empty( $view )) {
|
b82db04a
Yarik
test
|
241
242
|
throw new \yii\base\InvalidConfigException("form_options[view] must be set");
}
|
8a551494
Yarik
test
|
243
|
if($this->comment_class->guestComment || !empty( \Yii::$app->user->identity )) {
|
4ff3ca88
Yarik
test
|
244
245
246
247
248
249
250
251
252
|
return $this->render($view, [
'model' => $this->comment_class,
'rating' => $this->rating_class,
'user' => \Yii::$app->user->identity,
'dataProvider' => $this->dataProvider,
]);
} else {
return '';
}
|
b82db04a
Yarik
test
|
253
254
|
}
|
2d107e9e
Yarik
test
|
255
256
|
public function renderWidget()
{
|
b82db04a
Yarik
test
|
257
258
259
260
261
262
263
|
$template = $this->template;
$parts = $this->parts;
$options = $this->options;
$template = preg_replace('/{success}/', ArrayHelper::remove($parts, 'success', ''), $template);
$template = preg_replace('/{list}/', ArrayHelper::remove($parts, 'list', ''), $template);
$template = preg_replace('/{form}/', ArrayHelper::remove($parts, 'form', ''), $template);
$tag = ArrayHelper::remove($options, 'tag', 'div');
|
b5becf98
Yarik
test
|
264
265
|
if(!empty( $options[ 'class' ] )) {
$options[ 'class' ] .= ' ' . self::$baseClass[ 'widget_container' ];
|
8a551494
Yarik
test
|
266
|
} else {
|
b5becf98
Yarik
test
|
267
|
$options[ 'class' ] = self::$baseClass[ 'widget_container' ];
|
8a551494
Yarik
test
|
268
|
}
|
b82db04a
Yarik
test
|
269
270
271
|
return Html::tag($tag, $template, $options);
}
|
2d107e9e
Yarik
test
|
272
|
public function process()
|
b82db04a
Yarik
test
|
273
274
|
{
$data = \Yii::$app->request->post();
|
2d107e9e
Yarik
test
|
275
|
if($this->comment_class->load($data) && $this->comment_class->postComment()) {
|
47bd5ade
Yarik
test
|
276
277
278
279
280
|
if(is_object($this->rating_class)) {
$this->comment_class->checkRating();
if($this->comment_class->rating->load($data) && $this->comment_class->rating->save()) {
$this->isSuccess = true;
}
|
0eb4e7fc
Yarik
test
|
281
282
|
} else {
$this->isSuccess = true;
|
2d107e9e
Yarik
test
|
283
284
|
}
}
|
b82db04a
Yarik
test
|
285
|
}
|
8a551494
Yarik
test
|
286
|
|
b82db04a
Yarik
test
|
287
|
}
|