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 common\models\MediaComment as Comment; |
11
|
|
|
use yii\data\Pagination; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MediaComment |
15
|
|
|
* |
16
|
|
|
* @author Agiel K. Saputra <[email protected]> |
17
|
|
|
* @since 0.1.0 |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class MediaComment extends BaseComment |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Set comment and pagination. |
23
|
|
|
* Select all comments from database and create pagination. |
24
|
|
|
* Get child of current comment. |
25
|
|
|
*/ |
26
|
|
|
protected function setComments() |
27
|
|
|
{ |
28
|
|
|
/* @var $models \common\models\BaseComment */ |
29
|
|
|
$comments = []; |
30
|
|
|
$query = Comment::find() |
31
|
|
|
->select(['id', 'author', 'email', 'url', 'date', 'content']) |
32
|
|
|
->andWhere(['parent' => 0, 'media_id' => $this->model->id, 'status' => 'approved']) |
33
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
34
|
|
|
->orderBy(['date' => $this->commentOrder]); |
35
|
|
|
|
36
|
|
|
$countQuery = clone $query; |
37
|
|
|
|
38
|
|
|
$pages = new Pagination([ |
39
|
|
|
'totalCount' => $countQuery->count(), |
40
|
|
|
'pageSize' => $this->pageSize, |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->pages = $pages; |
44
|
|
|
|
45
|
|
|
$models = $query |
46
|
|
|
->offset($pages->offset) |
47
|
|
|
->limit($pages->limit) |
48
|
|
|
->all(); |
49
|
|
|
|
50
|
|
|
/* @var $model \common\models\PostComment */ |
51
|
|
|
foreach ($models as $model) { |
52
|
|
|
$comments[$model->id] = $model; |
53
|
|
|
$comments[$model->id]['child'] = $this->getChildren($model->id); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->comments = $comments; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get comment children based on comment ID. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return array|null |
65
|
|
|
*/ |
66
|
|
|
protected function getChildren($id) |
67
|
|
|
{ |
68
|
|
|
$comments = []; |
69
|
|
|
|
70
|
|
|
$models = Comment::find() |
71
|
|
|
->select(['id', 'author', 'email', 'url', 'date', 'content']) |
72
|
|
|
->andWhere(['parent' => $id, 'media_id' => $this->model->id, 'status' => 'approved']) |
73
|
|
|
->andWhere(['<=', 'date', date('Y-m-d H:i:s')]) |
74
|
|
|
->orderBy(['date' => $this->commentOrder]) |
75
|
|
|
->all(); |
76
|
|
|
|
77
|
|
|
if (empty($models)) { |
78
|
|
|
$comments = null; |
79
|
|
|
} else { |
80
|
|
|
/* @var $model \common\models\PostComment */ |
81
|
|
|
foreach ($models as $model) { |
82
|
|
|
$comments[$model->id] = $model; |
83
|
|
|
$comments[$model->id]['child'] = $this->getChildren($model->id); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $comments; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.