Completed
Push — master ( 16cb12...70f45b )
by Alexey
02:27
created

UserSearch::getDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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