1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ogheo\comments\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use ogheo\comments\helpers\CommentsHelper; |
7
|
|
|
use ogheo\comments\Module as CommentsModule; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CommentsQuery This is the ActiveQuery class for [[Comments]]. |
11
|
|
|
* |
12
|
|
|
* @property mixed loadParams |
13
|
|
|
* |
14
|
|
|
* @package ogheo\comments\models |
15
|
|
|
*/ |
16
|
|
|
class CommentsQuery extends \yii\db\ActiveQuery |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $_params = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getLoadParams() |
27
|
|
|
{ |
28
|
|
|
return $this->_params; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $params |
33
|
|
|
*/ |
34
|
|
|
public function setLoadParams($params) |
35
|
|
|
{ |
36
|
|
|
$this->_params = $params; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Select by model |
41
|
|
|
* @param $params |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function byModel($params) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$commentClass = CommentsModule::getInstance()->commentModelClass; |
47
|
|
|
|
48
|
|
|
return $this->andWhere([ |
49
|
|
|
'model' => $params['model'], |
50
|
|
|
'model_key' => $params['model_key'], |
51
|
|
|
'language' => Yii::$app->language, |
52
|
|
|
'status' => $commentClass::STATUS_PUBLISHED, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Select by url |
58
|
|
|
* @param $params |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function byUrl($params) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$commentClass = CommentsModule::getInstance()->commentModelClass; |
64
|
|
|
|
65
|
|
|
return $this->andWhere([ |
66
|
|
|
'url' => $params['url'], |
67
|
|
|
'language' => Yii::$app->language, |
68
|
|
|
'status' => $commentClass::STATUS_PUBLISHED, |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Select without children |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function withoutChildren() |
77
|
|
|
{ |
78
|
|
|
return $this->andWhere([ |
79
|
|
|
'main_parent_id' => null |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
* @return Comments|array|null |
86
|
|
|
*/ |
87
|
|
|
public function one($db = null) |
88
|
|
|
{ |
89
|
|
|
return parent::one($db); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
* @param null $db |
95
|
|
|
* @return array|\yii\db\ActiveRecord[] |
96
|
|
|
*/ |
97
|
|
|
public function all($db = null) |
98
|
|
|
{ |
99
|
|
|
$result = parent::all($db); |
100
|
|
|
|
101
|
|
|
if (!isset($this->loadParams['loadComments']) || $this->loadParams['loadComments'] !== true) { |
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$ids = []; |
106
|
|
|
foreach ($result as $model) { |
107
|
|
|
$ids[] = $model->id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$parentsId = implode(',', $ids); |
111
|
|
|
if (!empty($parentsId)) { |
112
|
|
|
$commentClass = CommentsModule::getInstance()->commentModelClass; |
113
|
|
|
$nestedComments = $commentClass::find()->where("main_parent_id IN ($parentsId)")->orderBy([ |
114
|
|
|
'created_at' => isset($this->loadParams['nestedOrder']) ? $this->loadParams['nestedOrder'] : null, |
115
|
|
|
])->all(); |
116
|
|
|
|
117
|
|
|
if (!empty($nestedComments)) { |
118
|
|
|
$mergedComments = array_merge($result, $nestedComments); |
119
|
|
|
$result = CommentsHelper::buildCommentsTree($mergedComments); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.