1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\models\search; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
use app\models\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* UserSearch represents the model behind the search form about `app\models\User`. |
12
|
|
|
*/ |
13
|
|
|
class UserSearch extends User |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public function rules() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
[ |
22
|
|
|
['username', 'email', 'ip', 'role'], 'string' |
23
|
|
|
], |
24
|
|
|
|
25
|
|
|
['date_create', 'date', 'format' => 'yyyy-mm-dd'], |
26
|
|
|
|
27
|
|
|
['status', 'integer'], |
28
|
|
|
['status', 'in', 'range' => array_keys(User::getStatuses())], |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Search by request criteria. |
34
|
|
|
* |
35
|
|
|
* @param array|null Filter params. |
36
|
|
|
* @return ActiveDataProvider Data provider. |
37
|
|
|
*/ |
38
|
|
|
public function search($params) |
39
|
|
|
{ |
40
|
|
|
$query = User::find()->with('roles'); |
41
|
|
|
|
42
|
|
|
$dataProvider = new ActiveDataProvider([ |
43
|
|
|
'query' => $query, |
44
|
|
|
'sort' => [ |
45
|
|
|
'defaultOrder' => [ |
46
|
|
|
'date_create' => SORT_DESC, |
47
|
|
|
] |
48
|
|
|
], |
49
|
|
|
'pagination' => [ |
50
|
|
|
'pageSize' => 50, |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$dataProvider->getPagination()->setPageSize(Yii::$app->request->get('pageSize'), true); |
55
|
|
|
|
56
|
|
|
if (!($this->load($params) && $this->validate())) { |
57
|
|
|
return $dataProvider; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$query->andFilterWhere([ |
61
|
|
|
'ip' => !empty($this->ip) ? ip2long($this->ip) : null, |
62
|
|
|
'status' => $this->status, |
63
|
|
|
'role' => $this->role, |
64
|
|
|
'DATE(date_create)' => $this->date_create |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$query->andFilterWhere(['like', 'user.username', $this->username]); |
68
|
|
|
$query->andFilterWhere(['like', 'user.email', $this->email]); |
69
|
|
|
|
70
|
|
|
return $dataProvider; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|