Completed
Push — master ( aa6fb7...16cb12 )
by Alexey
02:13
created

UserSearch   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 11
c 8
b 1
f 0
dl 0
loc 121
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A rules() 0 5 1
A scenarios() 0 6 1
A getQuery() 0 6 1
B search() 0 45 4
A processFilter() 0 13 3
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
     * Creates data provider instance with search query applied
72
     *
73
     * @param array $params
74
     *
75
     * @return ActiveDataProvider
76
     */
77
    public function search($params)
78
    {
79
        $query = $this->getQuery();
80
81
        $dataProvider = new ActiveDataProvider([
82
            'query' => $query,
83
            'sort' => [
84
                'defaultOrder' => ['id' => SORT_ASC],
85
            ],
86
        ]);
87
88
        $dataProvider->setSort([
89
            'attributes' => [
90
                'id',
91
                'username',
92
                'email',
93
                'status',
94
                'userRoleName' => [
95
                    'asc' => ['item_name' => SORT_ASC],
96
                    'desc' => ['item_name' => SORT_DESC],
97
                    'default' => SORT_ASC,
98
                    'label' => 'Role Name',
99
                ],
100
                'last_visit'
101
            ]
102
        ]);
103
104
        $this->load($params);
105
106
        if (!$this->validate()) {
107
            // uncomment the following line if you do not want to return any records when validation fails
108
            $query->where('0=1');
109
            return $dataProvider;
110
        }
111
112
        $this->processFilter($query);
113
114
        if ($this->pageSize) {
115
            $dataProvider->pagination->pageSize = $this->pageSize;
116
        }
117
118
        if (is_integer($query->count()))
119
            $dataProvider->pagination->totalCount = $query->count();
120
121
        return $dataProvider;
122
    }
123
124
    /**
125
     * @param $query \yii\db\QueryInterface
126
     */
127
    protected function processFilter($query)
128
    {
129
        // grid filtering conditions
130
        $query->andFilterWhere([
131
            'id' => $this->id,
132
            'status' => $this->status,
133
        ]);
134
135
        $query->andFilterWhere(['like', 'username', $this->username])
136
            ->andFilterWhere(['like', 'email', $this->email])
137
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
138
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
139
            ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]);
140
    }
141
}
142