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 common\models\search; |
9
|
|
|
|
10
|
|
|
use common\models\PostComment as PostCommentModel; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\data\ActiveDataProvider; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PostComment represents the model behind the search form about `common\models\PostComment`. |
17
|
|
|
* |
18
|
|
|
* @author Agiel K. Saputra <[email protected]> |
19
|
|
|
* @since 0.1.0 |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class PostComment extends PostCommentModel |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
[['id', 'post_id', 'parent', 'user_id'], 'integer'], |
30
|
|
|
[['author', 'email', 'url', 'ip', 'date', 'content', 'status', 'agent', 'post_title'], 'safe'], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function scenarios() |
38
|
|
|
{ |
39
|
|
|
// bypass scenarios() implementation in the parent class |
40
|
|
|
return Model::scenarios(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates data provider instance with search query applied |
45
|
|
|
* |
46
|
|
|
* @param array $params |
47
|
|
|
* |
48
|
|
|
* @param int $posttype Post type ID |
49
|
|
|
* @param int|null $post Post ID |
50
|
|
|
* @return ActiveDataProvider |
51
|
|
|
*/ |
52
|
|
|
public function search($params, $posttype, $post = null) |
53
|
|
|
{ |
54
|
|
|
$query = PostCommentModel::find(); |
55
|
|
|
|
56
|
|
|
$query->innerJoinWith([ |
57
|
|
|
'commentPost' => function ($query) { |
58
|
|
|
/* @var $query \yii\db\ActiveQuery */ |
59
|
|
|
return $query->from(['post' => Post::tableName()]); |
60
|
|
|
}, |
61
|
|
|
])->from(['postComment' => PostComment::tableName()]); |
62
|
|
|
|
63
|
|
|
$query->andWhere(['post.type' => $posttype]); |
64
|
|
|
|
65
|
|
|
if ($post) { |
|
|
|
|
66
|
|
|
$query->andWhere(['post.id' => $post]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$dataProvider = new ActiveDataProvider([ |
70
|
|
|
'query' => $query, |
71
|
|
|
'sort' => [ |
72
|
|
|
'defaultOrder' => [ |
73
|
|
|
'id' => SORT_DESC, |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$this->load($params); |
79
|
|
|
|
80
|
|
|
if (!$this->validate()) { |
81
|
|
|
return $dataProvider; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$query->andFilterWhere([ |
85
|
|
|
'id' => $this->id, |
86
|
|
|
'post_id' => $this->post_id, |
87
|
|
|
'parent' => $this->parent, |
88
|
|
|
'user_id' => $this->user_id, |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$query->andFilterWhere(['like', 'postComment.author', $this->author]) |
92
|
|
|
->andFilterWhere(['like', 'email', $this->email]) |
93
|
|
|
->andFilterWhere(['like', 'url', $this->url]) |
94
|
|
|
->andFilterWhere(['like', 'ip', $this->ip]) |
95
|
|
|
->andFilterWhere(['like', 'postComment.content', $this->content]) |
96
|
|
|
->andFilterWhere(['like', 'postComment.status', $this->status]) |
97
|
|
|
->andFilterWhere(['like', 'agent', $this->agent]) |
98
|
|
|
->andFilterWhere(['like', 'postComment.date', $this->date]) |
99
|
|
|
->andFilterWhere(['like', 'post.title', $this->post_title]); |
100
|
|
|
|
101
|
|
|
return $dataProvider; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.