1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace frontend\widgets\comment; |
9
|
|
|
|
10
|
|
|
use cebe\gravatar\Gravatar; |
11
|
|
|
use common\models\Option; |
12
|
|
|
use yii\base\Widget; |
13
|
|
|
use yii\helpers\ArrayHelper; |
14
|
|
|
use yii\helpers\Html; |
15
|
|
|
use yii\widgets\LinkPager; |
16
|
|
|
use yii\widgets\Pjax; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class BaseComment |
20
|
|
|
* |
21
|
|
|
* @author Agiel K. Saputra <[email protected]> |
22
|
|
|
* @since 0.1.0 |
23
|
|
|
*/ |
24
|
|
|
abstract class BaseComment extends Widget |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $tag = 'ul'; |
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
public $model; |
34
|
|
|
/** |
35
|
|
|
* @var |
36
|
|
|
*/ |
37
|
|
|
public $enableThreadComments; |
38
|
|
|
/** |
39
|
|
|
* @var |
40
|
|
|
*/ |
41
|
|
|
public $maxDepth; |
42
|
|
|
/** |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
public $commentOrder; |
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
public $avatarSize = 48; |
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public $options = ['class' => 'comments']; |
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
public $childOptions = ['class' => 'child']; |
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
public $itemOptions = ['class' => 'media']; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var |
65
|
|
|
*/ |
66
|
|
|
protected $pageSize; |
67
|
|
|
/** |
68
|
|
|
* @var |
69
|
|
|
*/ |
70
|
|
|
protected $pages; |
71
|
|
|
/** |
72
|
|
|
* @var |
73
|
|
|
*/ |
74
|
|
|
protected $comments; |
75
|
|
|
/** |
76
|
|
|
* @var |
77
|
|
|
*/ |
78
|
|
|
protected $tagItem; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function init() |
84
|
|
|
{ |
85
|
|
|
switch ($this->tag) { |
86
|
|
|
case 'div': |
87
|
|
|
$this->tagItem = 'div'; |
88
|
|
|
break; |
89
|
|
|
case 'ul': |
90
|
|
|
case 'li': |
91
|
|
|
$this->tagItem = 'li'; |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$this->pageSize) { |
96
|
|
|
$this->pageSize = Option::get('comments_per_page'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!$this->maxDepth) { |
100
|
|
|
$this->maxDepth = Option::get('thread_comments_depth'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!$this->commentOrder) { |
104
|
|
|
$this->commentOrder = Option::get('comment_order') === 'asc' ? SORT_ASC : SORT_DESC; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!$this->enableThreadComments) { |
108
|
|
|
$this->enableThreadComments = Option::get('thread_comments'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
Html::addCssClass($this->itemOptions, 'comment'); |
112
|
|
|
$this->setComments(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function run() |
119
|
|
|
{ |
120
|
|
|
if ($this->comments) { |
121
|
|
|
Pjax::begin(); |
122
|
|
|
echo Html::beginTag($this->tag, ArrayHelper::merge(['id' => $this->id], $this->options)); |
123
|
|
|
$this->renderComments($this->comments); |
124
|
|
|
echo Html::endTag($this->tag); |
125
|
|
|
echo Html::beginTag('nav', ['class' => 'comment-pagination']); |
126
|
|
|
echo LinkPager::widget([ |
127
|
|
|
'pagination' => $this->pages, |
128
|
|
|
'activePageCssClass' => 'active', |
129
|
|
|
'disabledPageCssClass' => 'disabled', |
130
|
|
|
'options' => [ |
131
|
|
|
'class' => 'pagination', |
132
|
|
|
], |
133
|
|
|
]); |
134
|
|
|
echo Html::endTag('nav'); |
135
|
|
|
Pjax::end(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \common\models\BaseComment $comment |
142
|
|
|
* @param int $depth |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
|
|
protected function displayComment($comment, $depth = 0) |
146
|
|
|
{ |
147
|
|
|
echo Html::beginTag('div', [ |
148
|
|
|
'id' => 'comment-' . $comment->id, |
149
|
|
|
'class' => $comment->child ? 'parent depth-' . $depth : 'depth-' . $depth, |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
if (Option::get('show_avatars')) { |
153
|
|
|
?> |
154
|
|
|
<div class="media-left avatar"> |
155
|
|
|
<?= Gravatar::widget([ |
156
|
|
|
'email' => $comment->email, |
157
|
|
|
'options' => [ |
158
|
|
|
'alt' => $comment->author, |
159
|
|
|
'class' => 'avatar', |
160
|
|
|
'width' => $this->avatarSize, |
161
|
|
|
'height' => $this->avatarSize, |
162
|
|
|
], |
163
|
|
|
'defaultImage' => Option::get('avatar_default'), |
164
|
|
|
'rating' => Option::get('avatar_rating'), |
165
|
|
|
'size' => $this->avatarSize, |
166
|
|
|
]) ?> |
167
|
|
|
|
168
|
|
|
</div> |
169
|
|
|
<?php |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
?> |
173
|
|
|
<div class="media-body comment-body"> |
174
|
|
|
<p class="meta"> |
175
|
|
|
<strong class="author vcard"> |
176
|
|
|
<span class="fn"> |
177
|
|
|
<?= $comment->author ? $comment->author : \Yii::t('writesdown', 'Anonymous') ?> |
178
|
|
|
|
179
|
|
|
</span> |
180
|
|
|
</strong> |
181
|
|
|
- |
182
|
|
|
<time class="date published" datetime="<?= \Yii::$app |
183
|
|
|
->formatter |
184
|
|
|
->asDatetime($comment->date) ?>"> |
185
|
|
|
<?= \Yii::$app->formatter->asDate($comment->date) ?> |
186
|
|
|
|
187
|
|
|
</time> |
188
|
|
|
|
189
|
|
|
<?php if ($depth < $this->maxDepth && $this->enableThreadComments) { |
190
|
|
|
echo Html::a(\Yii::t('writesdown', 'Reply'), '#', [ |
191
|
|
|
'class' => 'comment-reply-link', |
192
|
|
|
'data-id' => $comment->id, |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
?> |
196
|
|
|
|
197
|
|
|
</p> |
198
|
|
|
<div class="comment-content"> |
199
|
|
|
<?= $comment->content ?> |
200
|
|
|
|
201
|
|
|
</div> |
202
|
|
|
</div> |
203
|
|
|
<?php |
204
|
|
|
echo Html::endTag('div'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $comments |
209
|
|
|
* @param int $depth |
210
|
|
|
*/ |
211
|
|
|
protected function renderComments($comments, $depth = 0) |
212
|
|
|
{ |
213
|
|
|
foreach ($comments as $comment) { |
214
|
|
|
echo Html::beginTag($this->tagItem, $this->itemOptions); |
215
|
|
|
$this->displayComment($comment, $depth); |
216
|
|
|
if ($comment->child) { |
217
|
|
|
$depth++; |
218
|
|
|
if ($depth <= $this->maxDepth && $this->enableThreadComments) { |
219
|
|
|
echo Html::beginTag($this->tag, $this->childOptions); |
220
|
|
|
$this->renderComments($comment->child, $depth); |
221
|
|
|
echo Html::endTag($this->tag); |
222
|
|
|
} |
223
|
|
|
$depth--; |
224
|
|
|
} |
225
|
|
|
echo Html::endTag($this->tagItem); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get comment children. |
231
|
|
|
* |
232
|
|
|
* @param int $id |
233
|
|
|
*/ |
234
|
|
|
protected function getChildren($id) |
235
|
|
|
{ |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set comment model and pagination. |
240
|
|
|
*/ |
241
|
|
|
protected function setComments() |
242
|
|
|
{ |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|