UserSearch   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 10
eloc 63
c 4
b 2
f 0
dl 0
loc 131
rs 10

7 Methods

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