User::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace common\models\search;
9
10
use common\models\User as UserModel;
11
use Yii;
12
use yii\base\Model;
13
use yii\data\ActiveDataProvider;
14
15
/**
16
 * User represents the model behind the search form about `common\models\User`.
17
 *
18
 * @author Agiel K. Saputra <[email protected]>
19
 * @since 0.1.0
20
 */
21
class User extends UserModel
22
{
23
    /**
24
     * @inheritdoc
25
     */
26
    public function rules()
27
    {
28
        return [
29
            [['id', 'status'], 'integer'],
30
            [
31
                [
32
                    'username',
33
                    'email',
34
                    'full_name',
35
                    'display_name',
36
                    'password_hash',
37
                    'password_reset_token',
38
                    'auth_key',
39
                    'created_at',
40
                    'updated_at',
41
                    'login_at',
42
                ],
43
                'safe',
44
            ],
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function scenarios()
52
    {
53
        // bypass scenarios() implementation in the parent class
54
        return Model::scenarios();
55
    }
56
57
    /**
58
     * Creates data provider instance with search query applied
59
     *
60
     * @param array $params
61
     * @return ActiveDataProvider
62
     */
63
    public function search($params)
64
    {
65
        $query = UserModel::find();
66
67
        $query->andWhere(['<>', 'id', Yii::$app->user->id]);
68
69
        $dataProvider = new ActiveDataProvider([
70
            'query' => $query,
71
            'sort' => [
72
                'defaultOrder' => [
73
                    'id' => SORT_DESC,
74
                ],
75
            ],
76
        ]);
77
78
        $this->load($params);
79
80
        if (!$this->validate()) {
81
            return $dataProvider;
82
        }
83
84
        $query->andFilterWhere([
85
            'id' => $this->id,
86
            'status' => $this->status,
87
        ]);
88
89
        $query->andFilterWhere(['like', 'username', $this->username])
90
            ->andFilterWhere(['like', 'email', $this->email])
91
            ->andFilterWhere(['like', 'full_name', $this->full_name])
92
            ->andFilterWhere(['like', 'display_name', $this->display_name])
93
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
94
            ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
95
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
96
            ->andFilterWhere(['like', 'created_at', $this->created_at])
97
            ->andFilterWhere(['like', 'updated_at', $this->updated_at])
98
            ->andFilterWhere(['like', 'login_at', $this->login_at]);
99
100
        return $dataProvider;
101
    }
102
}
103