Completed
Push — master ( a17f18...4a9aa1 )
by Alexey
02:56
created

UserSearch::processDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
use yii\db\ActiveRecord;
10
11
/**
12
 * Class UserSearch
13
 * @package modules\users\models\backend
14
 *
15
 * @property string $userRoleName User Role Name
16
 * @property string $date_from Date From
17
 * @property string $date_to Date To
18
 * @property integer $pageSize Page Size
19
 */
20
class UserSearch extends User
21
{
22
    public $userRoleName;
23
    public $date_from;
24
    public $date_to;
25
    public $pageSize;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function init()
31
    {
32
        parent::init();
33
        $this->pageSize = Yii::$app->params['user.pageSize'];
34
    }
35
36
    /**
37
     * @inheritdoc
38
     * @return array
39
     */
40
    public function rules()
41
    {
42
        return [
43
            [['id', 'status', 'pageSize'], 'integer'],
44
            [['username', 'email', 'role', 'userRoleName', 'date_from', 'date_to'], 'safe'],
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     * @return array
51
     */
52
    public function scenarios()
53
    {
54
        // bypass scenarios() implementation in the parent class
55
        /** @scrutinizer ignore-call */
56
        $scenarios = Model::scenarios();
57
        return $scenarios;
58
    }
59
60
    /**
61
     * Creates data provider instance with search query applied
62
     *
63
     * @param array $params
64
     *
65
     * @return ActiveDataProvider
66
     */
67
    public function search($params)
68
    {
69
        $query = User::find();
70
71
        // add conditions that should always apply here
72
        $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id');
73
74
        $dataProvider = $this->processDataProvider($query);
75
76
        $this->load($params);
77
78
        if (!$this->validate()) {
79
            // uncomment the following line if you do not want to return any records when validation fails
80
            $query->where('0=1');
81
            return $dataProvider;
82
        }
83
84
        $this->processFilter($query);
85
86
        if ($this->pageSize) {
87
            $dataProvider->pagination->pageSize = $this->pageSize;
88
        }
89
90
        if (is_integer($query->count()))
91
            $dataProvider->pagination->totalCount = $query->count();
92
93
        return $dataProvider;
94
    }
95
96
    /**
97
     * @param $query \yii\db\QueryInterface
98
     * @return ActiveDataProvider
99
     */
100
    protected function processDataProvider($query)
101
    {
102
        $dataProvider = new ActiveDataProvider([
103
            'query' => $query,
104
            'sort' => [
105
                'defaultOrder' => ['id' => SORT_ASC],
106
            ],
107
        ]);
108
109
        $dataProvider->setSort([
110
            'attributes' => [
111
                'id',
112
                'username',
113
                'email',
114
                'status',
115
                'userRoleName' => [
116
                    'asc' => ['item_name' => SORT_ASC],
117
                    'desc' => ['item_name' => SORT_DESC],
118
                    'default' => SORT_ASC,
119
                    'label' => 'Role Name',
120
                ],
121
                'last_visit'
122
            ]
123
        ]);
124
125
        return $dataProvider;
126
    }
127
128
    /**
129
     * @param $query \yii\db\QueryInterface
130
     */
131
    protected function processFilter($query)
132
    {
133
        // grid filtering conditions
134
        $query->andFilterWhere([
135
            'id' => $this->id,
136
            'status' => $this->status,
137
        ]);
138
139
        $query->andFilterWhere(['like', 'username', $this->username])
140
            ->andFilterWhere(['like', 'email', $this->email])
141
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
142
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
143
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
144
    }
145
}
146