Completed
Push — master ( 70f45b...0e30cd )
by Alexey
02:42
created

UserSearch::getDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
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
        $dataProvider = new ActiveDataProvider([
77
            'query' => $query,
78
            'sort' => [
79
                'defaultOrder' => ['id' => SORT_ASC],
80
            ],
81
        ]);
82
        return $this->setSortDataProvider($dataProvider);
83
    }
84
85
    /**
86
     * @param ActiveDataProvider $dataProvider
87
     * @return mixed
88
     */
89
    protected function setSortDataProvider($dataProvider)
90
    {
91
        $dataProvider->setSort([
92
            'attributes' => [
93
                'id',
94
                'username',
95
                'email',
96
                'status',
97
                'userRoleName' => [
98
                    'asc' => ['item_name' => SORT_ASC],
99
                    'desc' => ['item_name' => SORT_DESC],
100
                    'default' => SORT_ASC,
101
                    'label' => 'Role Name',
102
                ],
103
                'last_visit'
104
            ]
105
        ]);
106
        return $dataProvider;
107
    }
108
109
    /**
110
     * Creates data provider instance with search query applied
111
     *
112
     * @param array $params
113
     *
114
     * @return ActiveDataProvider
115
     */
116
    public function search($params)
117
    {
118
        $query = $this->getQuery();
119
        $dataProvider = $this->getDataProvider($query);
120
121
        $this->load($params);
122
123
        if (!$this->validate()) {
124
            // uncomment the following line if you do not want to return any records when validation fails
125
            $query->where('0=1');
126
            return $dataProvider;
127
        }
128
129
        $this->processFilter($query);
130
131
        if ($this->pageSize) {
132
            $dataProvider->pagination->pageSize = $this->pageSize;
133
        }
134
135
        if (is_integer($query->count()))
136
            $dataProvider->pagination->totalCount = $query->count();
137
138
        return $dataProvider;
139
    }
140
141
    /**
142
     * @param $query \yii\db\QueryInterface
143
     */
144
    protected function processFilter($query)
145
    {
146
        // grid filtering conditions
147
        $query->andFilterWhere([
148
            'id' => $this->id,
149
            'status' => $this->status,
150
        ]);
151
152
        $query->andFilterWhere(['like', 'username', $this->username])
153
            ->andFilterWhere(['like', 'email', $this->email])
154
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
155
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
156
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
157
    }
158
}
159