Passed
Push — master ( 30294b...1839bf )
by Alexey
02:13
created

UserSearch::getDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
1
<?php
2
3
namespace modules\admin\models\search;
4
5
use yii\base\Model;
6
use yii\data\ActiveDataProvider;
7
use modules\admin\models\User;
8
use modules\admin\Module;
9
10
/**
11
 * Class UserSearch
12
 * @package modules\admin\models\search
13
 *
14
 * @property int $id ID
15
 * @property string $username Username
16
 * @property string $email Email
17
 * @property int|string $status Status
18
 * @property int $last_visit Last Visit
19
 * @property int $created_at Created
20
 * @property int $updated_at Updated
21
 * @property string $first_name First Name
22
 * @property string $last_name Last Name
23
 * @property int $registration_type Type Registration
24
 * @property string $role User Role Name
25
 * @property string $date_from Date From
26
 * @property string $date_to Date To
27
 */
28
class UserSearch extends Model
29
{
30
    public $id;
31
    public $username;
32
    public $email;
33
    public $status;
34
    public $last_visit;
35
    public $created_at;
36
    public $updated_at;
37
    public $registration_type;
38
    public $first_name;
39
    public $last_name;
40
    public $role;
41
    public $date_from;
42
    public $date_to;
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function rules()
48
    {
49
        return [
50
            [['id', 'status'], 'integer'],
51
            [['username', 'email', 'first_name', 'last_name', 'role', 'date_from', 'date_to'], 'safe'],
52
        ];
53
    }
54
55
    /**
56
     * @inheritdoc
57
     * @return array
58
     */
59
    public function attributeLabels()
60
    {
61
        return [
62
            'id' => 'ID',
63
            'created_at' => Module::t('users', 'Created'),
64
            'updated_at' => Module::t('users', 'Updated'),
65
            'last_visit' => Module::t('users', 'Last Visit'),
66
            'username' => Module::t('users', 'Username'),
67
            'email' => Module::t('users', 'Email'),
68
            'status' => Module::t('users', 'Status'),
69
            'first_name' => Module::t('users', 'First Name'),
70
            'last_name' => Module::t('users', 'Last Name'),
71
            'registration_type' => Module::t('users', 'Registration Type'),
72
            'role' => Module::t('users', 'Role'),
73
        ];
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getStatusesArray()
80
    {
81
        return User::getStatusesArray();
82
    }
83
84
    /**
85
     * @return object|\yii\db\ActiveQuery
86
     * @throws \yii\base\InvalidConfigException
87
     */
88
    protected function getQuery()
89
    {
90
        $query = User::find();
91
        $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%users}}.id');
92
        return $query;
93
    }
94
95
    /**
96
     * @param \yii\db\ActiveQuery $query
97
     * @return ActiveDataProvider
98
     */
99
    protected function getDataProvider($query)
100
    {
101
        return new ActiveDataProvider([
102
            'query' => $query,
103
            'sort' => [
104
                'defaultOrder' => ['id' => SORT_ASC],
105
                'attributes' => [
106
                    'id',
107
                    'username',
108
                    'email',
109
                    'status',
110
                    'role' => [
111
                        'asc' => ['item_name' => SORT_ASC],
112
                        'desc' => ['item_name' => SORT_DESC],
113
                        'default' => SORT_ASC,
114
                    ],
115
                    'last_visit'
116
                ]
117
            ],
118
        ]);
119
    }
120
121
    /**
122
     * @param $params
123
     * @return ActiveDataProvider
124
     * @throws \yii\base\InvalidConfigException
125
     */
126
    public function search($params)
127
    {
128
        $query = $this->getQuery();
129
        $dataProvider = $this->getDataProvider($query);
130
131
        $this->load($params);
132
133
        if (!$this->validate()) {
134
            $query->where('0=1');
135
            return $dataProvider;
136
        }
137
138
        $this->processFilter($query);
139
140
        return $dataProvider;
141
    }
142
143
    /**
144
     * @param $query \yii\db\QueryInterface
145
     */
146
    protected function processFilter($query)
147
    {
148
        // grid filtering conditions
149
        $query->andFilterWhere([
150
            'id' => $this->id,
151
            'status' => $this->status,
152
            'item_name' => $this->role,
153
        ]);
154
155
        $query->andFilterWhere(['like', 'username', $this->username])
156
            ->andFilterWhere(['like', 'email', $this->email])
157
            ->andFilterWhere(['like', 'first_name', $this->first_name])
158
            ->andFilterWhere(['like', 'last_name', $this->last_name])
159
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
160
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
161
    }
162
}
163