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\Term as TermModel; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\data\ActiveDataProvider; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Term represents the model behind the search form about `common\models\Term`. |
17
|
|
|
* |
18
|
|
|
* @author Agiel K. Saputra <[email protected]> |
19
|
|
|
* @since 0.1.0 |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class Term extends TermModel |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
[['id', 'taxonomy_id', 'parent', 'count'], 'integer'], |
30
|
|
|
[['name', 'slug', 'description'], '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
|
|
|
* @param int $taxonomyId |
48
|
|
|
* @return ActiveDataProvider |
49
|
|
|
*/ |
50
|
|
|
public function search($params, $taxonomyId) |
51
|
|
|
{ |
52
|
|
|
$query = TermModel::find(); |
53
|
|
|
|
54
|
|
|
$query->andWhere(['taxonomy_id' => $taxonomyId]); |
55
|
|
|
|
56
|
|
|
$dataProvider = new ActiveDataProvider([ |
57
|
|
|
'query' => $query, |
58
|
|
|
'sort' => [ |
59
|
|
|
'defaultOrder' => [ |
60
|
|
|
'id' => SORT_DESC, |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$this->load($params); |
66
|
|
|
|
67
|
|
|
if (!$this->validate()) { |
68
|
|
|
return $dataProvider; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$query->andFilterWhere([ |
72
|
|
|
'id' => $this->id, |
73
|
|
|
'taxonomy_id' => $this->taxonomy_id, |
74
|
|
|
'parent' => $this->parent, |
75
|
|
|
'count' => $this->count, |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$query->andFilterWhere(['like', 'name', $this->name]) |
79
|
|
|
->andFilterWhere(['like', 'slug', $this->slug]) |
80
|
|
|
->andFilterWhere(['like', 'description', $this->description]); |
81
|
|
|
|
82
|
|
|
return $dataProvider; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.