CommentWidget.php
5.71 KB
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
<?php
namespace common\modules\comment\widgets;
use \yii\helpers\ArrayHelper;
use \yii\helpers\Html;
class CommentWidget extends \yii\base\Widget
{
/**
* @var array Parts of widgets that can be rendered
*/
public $parts = [];
/**
* @var string|\common\modules\comment\models\Comment
*/
public $comment_class;
/**
* @var array
*/
public $class_options = [];
/**
* @var bool Wheather to display comment list
*/
public $display_comment_list = true;
/**
* @var bool Wheather to display comment form
*/
public $display_comment_form = true;
public $display_comment_success = true;
public $allow_multiple = true;
public $success_text = "Comment posted successfully";
public $list_options = [
'tag' => 'div',
'view' => 'list-comment',
'class' => 'test-class',
];
public $success_options = [
'tag' => 'div',
'content' => null,
'class' => 'test-class-success',
];
public $form_options = [
'tag' => 'div',
'view' => 'form-comment',
'class' => 'test-class-form',
];
protected $isSuccess = false;
public $entity;
public $template = "{success}\n{form}\n{list}";
public $options = [];
/**
* @var \yii\data\DataProviderInterface
*/
public $dataProvider;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if(is_string($this->comment_class)) {
$this->comment_class = new $this->comment_class($this->class_options);
} elseif(!is_object($this->comment_class)) {
throw new \yii\base\InvalidConfigException(__CLASS__.'->comment_class must be defined as string or object.');
}
$this->comment_class->entity = $this->entity;
$this->createDataProvider();
if($this->comment_class->checkCreate($this->entity)) {
$this->handleCreate();
}
ob_start();
}
/**
* @inheritdoc
* @return string
*/
public function run()
{
$content = ob_get_clean();
$this->createParts();
return $this->renderWidget();
}
public function createParts() {
if($this->display_comment_success && $this->isSuccess) {
$tag = ArrayHelper::remove($this->success_options, 'tag', 'div');
if(is_callable($this->success_options['content'])) {
$result = call_user_func(ArrayHelper::remove($this->success_options, 'content'), $this->success_text);
} elseif($this->success_options['content'] != NULL) {
$result = Html::encode(ArrayHelper::remove($this->success_options, 'content', $this->success_text));
} else {
$result = Html::encode($this->success_text);
}
$this->parts['success'] = Html::tag($tag, $result, $this->success_options);
unset($tag, $result);
}
if($this->display_comment_list) {
$tag = ArrayHelper::remove($this->list_options, 'tag', 'div');
$view = ArrayHelper::remove($this->list_options, 'view');
$this->parts['list'] = Html::tag($tag, $this->renderItems($view), $this->list_options);
}
if($this->display_comment_form) {
$tag = ArrayHelper::remove($this->form_options, 'tag', 'div');
$view = ArrayHelper::remove($this->form_options, 'view');
$this->parts['form'] = Html::tag($tag, $this->renderForm($view), $this->list_options);
}
}
public function createDataProvider()
{
$this->dataProvider = new \yii\data\ActiveDataProvider([
'query' => $this->comment_class->getComments($this->entity),
'pagination' => [
'pageSize' => 10,
],
]);
}
public function renderItems($view) {
if(empty($view)) {
throw new \yii\base\InvalidConfigException("list_options[view] must be set");
}
return $this->render($view, ['dataProvider' => $this->dataProvider]);
}
public function renderForm($view) {
if(empty($view)) {
throw new \yii\base\InvalidConfigException("form_options[view] must be set");
}
return $this->render($view, [
'model' => $this->comment_class,
'user' => \Yii::$app->user->identity,
'dataProvider' => $this->dataProvider,
]);
}
public function renderWidget() {
$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');
return Html::tag($tag, $template, $options);
}
public function handleCreate()
{
$data = \Yii::$app->request->post();
if($this->comment_class->postComment($data)) {
$this->isSuccess = true;
};
}
}