Completed
Push — master ( c933cb...bae5b1 )
by Alexey
03:50
created

UserSearch::setPageSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
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
25
    /**
26
     * @inheritdoc
27
     * @return array
28
     */
29
    public function rules()
30
    {
31
        return [
32
            [['id', 'status'], 'integer'],
33
            [['username', 'email', 'role', 'userRoleName', 'date_from', 'date_to'], 'safe'],
34
        ];
35
    }
36
37
    /**
38
     * @inheritdoc
39
     * @return array
40
     */
41
    public function scenarios()
42
    {
43
        // bypass scenarios() implementation in the parent class
44
        /** @scrutinizer ignore-call */
45
        $scenarios = Model::scenarios();
46
        return $scenarios;
47
    }
48
49
    /**
50
     * @return object|\yii\db\ActiveQuery
51
     * @throws \yii\base\InvalidConfigException
52
     */
53
    protected function getQuery()
54
    {
55
        $query = User::find();
56
        // add conditions that should always apply here
57
        $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id');
58
        return $query;
59
    }
60
61
    /**
62
     * @param \yii\db\ActiveQuery $query
63
     * @return ActiveDataProvider
64
     */
65
    protected function getDataProvider($query)
66
    {
67
        return new ActiveDataProvider([
68
            'query' => $query,
69
            'sort' => [
70
                'defaultOrder' => ['id' => SORT_ASC],
71
                'attributes' => [
72
                    'id',
73
                    'username',
74
                    'email',
75
                    'status',
76
                    'userRoleName' => [
77
                        'asc' => ['item_name' => SORT_ASC],
78
                        'desc' => ['item_name' => SORT_DESC],
79
                        'default' => SORT_ASC,
80
                        'label' => 'Role Name',
81
                    ],
82
                    'last_visit'
83
                ]
84
            ],
85
        ]);
86
    }
87
88
    /**
89
     * @param array $params
90
     * @return mixed|ActiveDataProvider
91
     * @throws \yii\base\InvalidConfigException
92
     */
93
    public function search($params)
94
    {
95
        $query = $this->getQuery();
96
        $dataProvider = $this->getDataProvider($query);
97
98
        $this->load($params);
99
100
        if (!$this->validate()) {
101
            // uncomment the following line if you do not want to return any records when validation fails
102
            $query->where('0=1');
103
            return $dataProvider;
104
        }
105
106
        $this->processFilter($query);
107
        $dataProvider = $this->setTotalCount($query, $dataProvider);
108
        return $dataProvider;
109
    }
110
111
    /**
112
     * @param $query \yii\db\QueryInterface
113
     */
114
    protected function processFilter($query)
115
    {
116
        // grid filtering conditions
117
        $query->andFilterWhere([
118
            'id' => $this->id,
119
            'status' => $this->status,
120
        ]);
121
122
        $query->andFilterWhere(['like', 'username', $this->username])
123
            ->andFilterWhere(['like', 'email', $this->email])
124
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
125
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
126
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
127
    }
128
129
    /**
130
     * @param \yii\db\ActiveQuery $query
131
     * @param ActiveDataProvider $dataProvider
132
     * @return mixed
133
     */
134
    protected function setTotalCount($query, $dataProvider)
135
    {
136
        if (is_integer($query->count()))
137
            $dataProvider->pagination->totalCount = $query->count();
138
        return $dataProvider;
139
    }
140
}
141