Passed
Push — master ( ab3dad...a17f18 )
by Alexey
03:05
created

UserSearch::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * Creates data provider instance with search query applied
61
     *
62
     * @param array $params
63
     *
64
     * @return ActiveDataProvider
65
     */
66
    public function search($params)
67
    {
68
        $query = User::find();
69
70
        // add conditions that should always apply here
71
        $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id');
72
73
        $dataProvider = new ActiveDataProvider([
74
            'query' => $query,
75
            'sort' => [
76
                'defaultOrder' => ['id' => SORT_ASC],
77
            ],
78
        ]);
79
80
        $dataProvider->setSort([
81
            'attributes' => [
82
                'id',
83
                'username',
84
                'email',
85
                'status',
86
                'userRoleName' => [
87
                    'asc' => ['item_name' => SORT_ASC],
88
                    'desc' => ['item_name' => SORT_DESC],
89
                    'default' => SORT_ASC,
90
                    'label' => 'Role Name',
91
                ],
92
                'last_visit'
93
            ]
94
        ]);
95
96
        $this->load($params);
97
98
        if (!$this->validate()) {
99
            // uncomment the following line if you do not want to return any records when validation fails
100
            $query->where('0=1');
101
            return $dataProvider;
102
        }
103
104
        $this->processFilter($query);
105
106
        if ($this->pageSize) {
107
            $dataProvider->pagination->pageSize = $this->pageSize;
108
        }
109
110
        if (is_integer($query->count()))
111
            $dataProvider->pagination->totalCount = $query->count();
112
113
        return $dataProvider;
114
    }
115
116
    /**
117
     * @param $query \yii\db\QueryInterface
118
     */
119
    protected function processFilter($query)
120
    {
121
        // grid filtering conditions
122
        $query->andFilterWhere([
123
            'id' => $this->id,
124
            'status' => $this->status,
125
        ]);
126
127
        $query->andFilterWhere(['like', 'username', $this->username])
128
            ->andFilterWhere(['like', 'email', $this->email])
129
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
130
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
131
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
132
    }
133
}
134