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