MemberSearch::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization;
14
15
use rhosocial\organization\queries\MemberQuery;
16
use rhosocial\organization\rbac\roles\DepartmentAdmin;
17
use rhosocial\organization\rbac\roles\DepartmentCreator;
18
use rhosocial\organization\rbac\roles\OrganizationAdmin;
19
use rhosocial\organization\rbac\roles\OrganizationCreator;
20
use Yii;
21
use yii\base\Model;
22
use yii\data\ActiveDataProvider;
23
24
/**
25
 * Class MemberSearch
26
 * @package rhosocial\organization
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
class MemberSearch extends Model
31
{
32
    /**
33
     * @var Organization
34
     */
35
    public $organization;
36
    public $memberClass = Member::class;
37
    public function getQuery()
38
    {
39
        $class = $this->memberClass;
40
        if (empty($class)) {
41
            return null;
42
        }
43
        return $class::find();
44
    }
45
    public $memberAlias = 'm_alias';
46
    public $memberUserAlias = 'u_alias';
47
    public $profileAlias = 'p_alias';
48
    public $id;
49
    public $first_name;
50
    public $last_name;
51
    public $nickname;
52
    public $gender;
53
    public $position;
54
    public $description;
55
    public $role;
56
    protected $roleInDb;
57
    /**
58
     * @var string
59
     */
60
    public $createdFrom;
61
    protected $createdFromInUtc;
62
63
    /**
64
     * @var string
65
     */
66
    public $createdTo;
67
    protected $createdToInUtc;
68
    /**
69
     * @var MemberQuery;
70
     */
71
    public $query;
72
73
    const ROLE_ADMIN = 'admin';
74
    const ROLE_CREATOR = 'creator';
75
76
    /**
77
     * @return array
78
     */
79
    public static function getRolesWithEmpty()
80
    {
81
        return [
82
            '' => Yii::t('user', 'All'),
83
            self::ROLE_ADMIN => Yii::t('organization', 'Administrator'),
84
            self::ROLE_CREATOR => Yii::t('organization', 'Creator'),
85
        ];
86
    }
87
88
    public function rules()
89
    {
90
        return [
91
            ['id', 'integer', 'min' => 0],
92
            [['nickname', 'first_name', 'last_name', 'position', 'description'], 'string', 'max' => 255],
93
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
94
            [['createdFrom', 'createdTo'], 'gmdate'],
95
            ['gender', 'in', 'range' => array_keys(\rhosocial\user\Profile::getGenderDescsWithEmpty())],
96
            ['role', 'in', 'range' => array_keys(static::getRolesWithEmpty())],
97
            ['role', 'judgeRoles'],
98
        ];
99
    }
100
101
    /**
102
     * Convert time attribute to UTC time.
103
     * @param string $attribute
104
     * @param array $params
105
     * @param mixed $validator
106
     */
107
    public function gmdate($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        if (isset($this->$attribute)) {
110
            $timestamp = strtotime($this->$attribute);
111
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
112
        }
113
    }
114
115
    /**
116
     * @param string $attribute
117
     * @param array $params
118
     * @param mixed $validator
119
     */
120
    public function judgeRoles($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        if (isset($this->$attribute)) {
123
            if ($this->$attribute == self::ROLE_ADMIN) {
124
                $this->roleInDb = [(new DepartmentAdmin)->name, (new OrganizationAdmin)->name];
125
            } elseif ($this->$attribute == self::ROLE_CREATOR) {
126
                $this->roleInDb = [(new DepartmentCreator)->name, (new OrganizationCreator)->name];
127
            }
128
        }
129
    }
130
131
    public function init()
132
    {
133
        if (!isset($this->query)) {
134
            $this->query = $this->prepareQuery();
135
        }
136
    }
137
138
    /**
139
     * @param MemberQuery $query
140
     * @return null|MemberQuery
141
     */
142
    protected function prepareQuery($query = null)
143
    {
144
        if (!$query) {
145
            $query = $this->getQuery();
146
        }
147
        /* @var $query MemberQuery */
148
        $class = $this->memberClass;
149
        $query = $query->from("{$class::tableName()} {$this->memberAlias}");
150
        if (isset($this->organization)) {
151
            $query = $query->organization($this->organization);
152
        }
153
        $noInitMember = new $class;
154
        /* @var $noInitMember Member */
155
        $userClass = $noInitMember->memberUserClass;
0 ignored issues
show
Unused Code introduced by
$userClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
156
        $query = $query->joinWith(["memberUser {$this->memberUserAlias}"]);
157
158
        $noInitUser = $noInitMember->getNoInitMemberUser();
159
        $profileClass = $noInitUser->profileClass;
160
        if (!empty($profileClass)) {
161
            $query = $query->joinWith(["memberUser.profile {$this->profileAlias}"]);
162
        }
163
        return $query;
164
    }
165
166
    /**
167
     * @param $params
168
     * @return ActiveDataProvider
169
     */
170
    public function search($params)
171
    {
172
        $query = $this->query;
173
        $dataProvider = new ActiveDataProvider([
174
            'query' => $query,
175
            'pagination' => [
176
                'pageParam' => 'member-page',
177
                'defaultPageSize' => 20,
178
                'pageSizeParam' => 'member-per-page',
179
            ],
180
            'sort' => [
181
                'sortParam' => 'member-sort',
182
                'attributes' => [
183
                    'id' => [
184
                        'asc' => [$this->memberUserAlias . '.id' => SORT_ASC],
185
                        'desc' => [$this->memberUserAlias . '.id' => SORT_DESC],
186
                        'default' => SORT_ASC,
187
                        'label' => Yii::t('user', 'User ID'),
188
                    ],
189
                    'name' => [
190
                        'asc' => [$this->profileAlias . '.first_name' => SORT_ASC, $this->profileAlias . '.last_name' => SORT_ASC],
191
                        'desc' => [$this->profileAlias . '.first_name' => SORT_DESC, $this->profileAlias . '.last_name' => SORT_DESC],
192
                        'default' => SORT_DESC,
193
                        'label' => Yii::t('user', 'Name'),
194
                    ],
195
                    'position',
196
                    'role',
197
                    'created_at' => [
198
                        'asc' => [$this->memberAlias . '.created_at' => SORT_ASC],
199
                        'desc' => [$this->memberAlias . '.created_at' => SORT_DESC],
200
                        'default' => SORT_ASC,
201
                        'label' => Yii::t('user', 'Creation Time'),
202
                    ],
203
                    'updated_at' => [
204
                        'asc' => [$this->memberAlias . '.updated_at' => SORT_ASC],
205
                        'desc' => [$this->memberAlias . '.updated_at' => SORT_DESC],
206
                        'default' => SORT_ASC,
207
                        'label' => Yii::t('user', 'Last Updated Time'),
208
                    ],
209
                ],
210
            ],
211
        ]);
212
213
        if (!($this->load($params) && $this->validate())) {
214
            return $dataProvider;
215
        }
216
217
        $query = $query->andFilterWhere([
218
            'LIKE', $this->memberUserAlias . '.id', $this->id,
219
        ])->andFilterWhere([
220
            'LIKE', $this->profileAlias . '.nickname', $this->nickname,
221
        ])->andFilterWhere([
222
            'LIKE', $this->profileAlias . '.first_name', $this->first_name,
223
        ])->andFilterWhere([
224
            'LIKE', $this->profileAlias . '.last_name', $this->last_name,
225
        ])->andFilterWhere([
226
            $this->profileAlias . '.gender' => $this->gender,
227
        ])->andFilterWhere([
228
            'LIKE', $this->memberAlias . '.position', $this->position,
229
        ])->andFilterWhere([
230
            'LIKE', $this->memberAlias . '.description', $this->description,
231
        ])->andFilterWhere([
232
            $this->memberAlias . '.role' => $this->roleInDb,
233
        ])->andFilterWhere([
234
            '>=', $this->memberAlias . '.created_at', $this->createdFromInUtc,
235
        ])->andFilterWhere([
236
            '<=', $this->memberAlias . '.created_at', $this->createdToInUtc,
237
        ]);
238
        $dataProvider->query = $query;
239
        return $dataProvider;
240
    }
241
242
    /**
243
     * @return array
244
     */
245
    public function attributeLabels()
246
    {
247
        $attributeLabels = parent::attributeLabels();
248
        $attributeLabels['id'] = Yii::t('user', 'User ID');
249
        $attributeLabels['first_name'] = Yii::t('user', 'First Name');
250
        $attributeLabels['last_name'] = Yii::t('user', 'Last Name');
251
        $attributeLabels['nickname'] = Yii::t('user', 'Nickname');
252
        $attributeLabels['gender'] = Yii::t('user', 'Gender');
253
        $attributeLabels['position'] = Yii::t('organization', 'Member Position');
254
        $attributeLabels['description'] = Yii::t('organization', 'Description');
255
        $attributeLabels['role'] = Yii::t('organization', 'Role');
256
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
257
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
258
        return $attributeLabels;
259
    }
260
}
261