1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\models\search; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use modules\users\models\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class UserSearch |
12
|
|
|
* @package modules\users\models\backend |
13
|
|
|
* |
14
|
|
|
* @property string $userRoleName User Role Name |
15
|
|
|
* @property string $date_from Date From |
16
|
|
|
* @property string $date_to Date To |
17
|
|
|
* @property integer $pageSize Page Size |
18
|
|
|
*/ |
19
|
|
|
class UserSearch extends User |
20
|
|
|
{ |
21
|
|
|
public $userRoleName; |
22
|
|
|
public $date_from; |
23
|
|
|
public $date_to; |
24
|
|
|
public $pageSize; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function init() |
30
|
|
|
{ |
31
|
|
|
parent::init(); |
32
|
|
|
$this->pageSize = Yii::$app->params['user.pageSize']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function rules() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
[['id', 'status', 'pageSize'], 'integer'], |
43
|
|
|
[['username', 'email', 'role', 'userRoleName', 'date_from', 'date_to'], 'safe'], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function scenarios() |
52
|
|
|
{ |
53
|
|
|
// bypass scenarios() implementation in the parent class |
54
|
|
|
/** @scrutinizer ignore-call */ |
55
|
|
|
$scenarios = Model::scenarios(); |
56
|
|
|
return $scenarios; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \yii\db\ActiveQuery |
61
|
|
|
*/ |
62
|
|
|
protected function getQuery() |
63
|
|
|
{ |
64
|
|
|
$query = User::find(); |
65
|
|
|
// add conditions that should always apply here |
66
|
|
|
$query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id'); |
67
|
|
|
return $query; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \yii\db\ActiveQuery $query |
72
|
|
|
* @return ActiveDataProvider |
73
|
|
|
*/ |
74
|
|
|
protected function getDataProvider($query) |
75
|
|
|
{ |
76
|
|
|
return new ActiveDataProvider([ |
77
|
|
|
'query' => $query, |
78
|
|
|
'sort' => [ |
79
|
|
|
'defaultOrder' => ['id' => SORT_ASC], |
80
|
|
|
'attributes' => [ |
81
|
|
|
'id', |
82
|
|
|
'username', |
83
|
|
|
'email', |
84
|
|
|
'status', |
85
|
|
|
'userRoleName' => [ |
86
|
|
|
'asc' => ['item_name' => SORT_ASC], |
87
|
|
|
'desc' => ['item_name' => SORT_DESC], |
88
|
|
|
'default' => SORT_ASC, |
89
|
|
|
'label' => 'Role Name', |
90
|
|
|
], |
91
|
|
|
'last_visit' |
92
|
|
|
] |
93
|
|
|
], |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates data provider instance with search query applied |
99
|
|
|
* |
100
|
|
|
* @param array $params |
101
|
|
|
* |
102
|
|
|
* @return ActiveDataProvider |
103
|
|
|
*/ |
104
|
|
|
public function search($params) |
105
|
|
|
{ |
106
|
|
|
$query = $this->getQuery(); |
107
|
|
|
$dataProvider = $this->getDataProvider($query); |
108
|
|
|
|
109
|
|
|
$this->load($params); |
110
|
|
|
|
111
|
|
|
if (!$this->validate()) { |
112
|
|
|
// uncomment the following line if you do not want to return any records when validation fails |
113
|
|
|
$query->where('0=1'); |
114
|
|
|
return $dataProvider; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->processFilter($query); |
118
|
|
|
|
119
|
|
|
if ($this->pageSize) { |
120
|
|
|
$dataProvider->pagination->pageSize = $this->pageSize; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (is_integer($query->count())) |
124
|
|
|
$dataProvider->pagination->totalCount = $query->count(); |
125
|
|
|
|
126
|
|
|
return $dataProvider; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $query \yii\db\QueryInterface |
131
|
|
|
*/ |
132
|
|
|
protected function processFilter($query) |
133
|
|
|
{ |
134
|
|
|
// grid filtering conditions |
135
|
|
|
$query->andFilterWhere([ |
136
|
|
|
'id' => $this->id, |
137
|
|
|
'status' => $this->status, |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
$query->andFilterWhere(['like', 'username', $this->username]) |
141
|
|
|
->andFilterWhere(['like', 'email', $this->email]) |
142
|
|
|
->andFilterWhere(['like', 'item_name', $this->userRoleName]) |
143
|
|
|
->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null]) |
144
|
|
|
->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|