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
|
|
|
public static function getRolesWithEmpty() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'' => Yii::t('user', 'All'), |
80
|
|
|
self::ROLE_ADMIN => Yii::t('organization', 'Administrator'), |
81
|
|
|
self::ROLE_CREATOR => Yii::t('organization', 'Creator'), |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function rules() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
['id', 'integer', 'min' => 0], |
89
|
|
|
[['nickname', 'first_name', 'last_name', 'position', 'description'], 'string', 'max' => 255], |
90
|
|
|
[['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'], |
91
|
|
|
[['createdFrom', 'createdTo'], 'gmdate'], |
92
|
|
|
['gender', 'in', 'range' => array_keys(\rhosocial\user\Profile::getGenderDescsWithEmpty())], |
93
|
|
|
['role', 'in', 'range' => array_keys(static::getRolesWithEmpty())], |
94
|
|
|
['role', 'judgeRoles'], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Convert time attribute to UTC time. |
100
|
|
|
* @param string $attribute |
101
|
|
|
* @param array $params |
102
|
|
|
* @param mixed $validator |
103
|
|
|
*/ |
104
|
|
|
public function gmdate($attribute, $params, $validator) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (isset($this->$attribute)) { |
107
|
|
|
$timestamp = strtotime($this->$attribute); |
108
|
|
|
$this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $attribute |
114
|
|
|
* @param array $params |
115
|
|
|
* @param mixed $validator |
116
|
|
|
*/ |
117
|
|
|
public function judgeRoles($attribute, $params, $validator) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (isset($this->$attribute)) { |
120
|
|
|
if ($this->$attribute == self::ROLE_ADMIN) { |
121
|
|
|
$this->roleInDb = [(new DepartmentAdmin)->name, (new OrganizationAdmin)->name]; |
122
|
|
|
} elseif ($this->$attribute == self::ROLE_CREATOR) { |
123
|
|
|
$this->roleInDb = [(new DepartmentCreator)->name, (new OrganizationCreator)->name]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function init() |
129
|
|
|
{ |
130
|
|
|
if (!isset($this->query)) { |
131
|
|
|
$this->query = $this->prepareQuery(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param MemberQuery $query |
137
|
|
|
* @return null|MemberQuery |
138
|
|
|
*/ |
139
|
|
|
protected function prepareQuery($query = null) |
140
|
|
|
{ |
141
|
|
|
if (!$query) { |
142
|
|
|
$query = $this->getQuery(); |
143
|
|
|
} |
144
|
|
|
/* @var $query MemberQuery */ |
145
|
|
|
$class = $this->memberClass; |
146
|
|
|
$query = $query->from("{$class::tableName()} {$this->memberAlias}"); |
147
|
|
|
if (isset($this->organization)) { |
148
|
|
|
$query = $query->organization($this->organization); |
149
|
|
|
} |
150
|
|
|
$noInitMember = new $class; |
151
|
|
|
/* @var $noInitMember Member */ |
152
|
|
|
$userClass = $noInitMember->memberUserClass; |
|
|
|
|
153
|
|
|
$query = $query->joinWith(["memberUser {$this->memberUserAlias}"]); |
154
|
|
|
|
155
|
|
|
$noInitUser = $noInitMember->getNoInitMemberUser(); |
156
|
|
|
$profileClass = $noInitUser->profileClass; |
157
|
|
|
if (!empty($profileClass)) { |
158
|
|
|
$query = $query->joinWith(["memberUser.profile {$this->profileAlias}"]); |
159
|
|
|
} |
160
|
|
|
return $query; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function search($params) |
164
|
|
|
{ |
165
|
|
|
$query = $this->query; |
166
|
|
|
$dataProvider = new ActiveDataProvider([ |
167
|
|
|
'query' => $query, |
168
|
|
|
'pagination' => [ |
169
|
|
|
'pageParam' => 'member-page', |
170
|
|
|
'defaultPageSize' => 20, |
171
|
|
|
'pageSizeParam' => 'member-per-page', |
172
|
|
|
], |
173
|
|
|
'sort' => [ |
174
|
|
|
'sortParam' => 'member-sort', |
175
|
|
|
'attributes' => [ |
176
|
|
|
'id' => [ |
177
|
|
|
'asc' => [$this->memberUserAlias . '.id' => SORT_ASC], |
178
|
|
|
'desc' => [$this->memberUserAlias . '.id' => SORT_DESC], |
179
|
|
|
'default' => SORT_ASC, |
180
|
|
|
'label' => Yii::t('user', 'User ID'), |
181
|
|
|
], |
182
|
|
|
'name' => [ |
183
|
|
|
'asc' => [$this->profileAlias . '.first_name' => SORT_ASC, $this->profileAlias . '.last_name' => SORT_ASC], |
184
|
|
|
'desc' => [$this->profileAlias . '.first_name' => SORT_DESC, $this->profileAlias . '.last_name' => SORT_DESC], |
185
|
|
|
'default' => SORT_DESC, |
186
|
|
|
'label' => Yii::t('user', 'Name'), |
187
|
|
|
], |
188
|
|
|
'position', |
189
|
|
|
'role', |
190
|
|
|
'created_at' => [ |
191
|
|
|
'asc' => [$this->memberAlias . '.created_at' => SORT_ASC], |
192
|
|
|
'desc' => [$this->memberAlias . '.created_at' => SORT_DESC], |
193
|
|
|
'default' => SORT_ASC, |
194
|
|
|
'label' => Yii::t('user', 'Creation Time'), |
195
|
|
|
], |
196
|
|
|
'updated_at' => [ |
197
|
|
|
'asc' => [$this->memberAlias . '.updated_at' => SORT_ASC], |
198
|
|
|
'desc' => [$this->memberAlias . '.updated_at' => SORT_DESC], |
199
|
|
|
'default' => SORT_ASC, |
200
|
|
|
'label' => Yii::t('user', 'Last Updated Time'), |
201
|
|
|
], |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
]); |
205
|
|
|
|
206
|
|
|
if (!($this->load($params) && $this->validate())) { |
207
|
|
|
return $dataProvider; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$query = $query->andFilterWhere([ |
211
|
|
|
'LIKE', $this->memberUserAlias . '.id', $this->id, |
212
|
|
|
])->andFilterWhere([ |
213
|
|
|
'LIKE', $this->profileAlias . '.nickname', $this->nickname, |
214
|
|
|
])->andFilterWhere([ |
215
|
|
|
'LIKE', $this->profileAlias . '.first_name', $this->first_name, |
216
|
|
|
])->andFilterWhere([ |
217
|
|
|
'LIKE', $this->profileAlias . '.last_name', $this->last_name, |
218
|
|
|
])->andFilterWhere([ |
219
|
|
|
$this->profileAlias . '.gender' => $this->gender, |
220
|
|
|
])->andFilterWhere([ |
221
|
|
|
'LIKE', $this->memberAlias . '.position', $this->position, |
222
|
|
|
])->andFilterWhere([ |
223
|
|
|
'LIKE', $this->memberAlias . '.description', $this->description, |
224
|
|
|
])->andFilterWhere([ |
225
|
|
|
$this->memberAlias . '.role' => $this->roleInDb, |
226
|
|
|
])->andFilterWhere([ |
227
|
|
|
'>=', $this->memberAlias . '.created_at', $this->createdFromInUtc, |
228
|
|
|
])->andFilterWhere([ |
229
|
|
|
'<=', $this->memberAlias . '.created_at', $this->createdToInUtc, |
230
|
|
|
]); |
231
|
|
|
$dataProvider->query = $query; |
232
|
|
|
return $dataProvider; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
public function attributeLabels() |
239
|
|
|
{ |
240
|
|
|
$attributeLabels = parent::attributeLabels(); |
241
|
|
|
$attributeLabels['id'] = Yii::t('user', 'User ID'); |
242
|
|
|
$attributeLabels['first_name'] = Yii::t('user', 'First Name'); |
243
|
|
|
$attributeLabels['last_name'] = Yii::t('user', 'Last Name'); |
244
|
|
|
$attributeLabels['nickname'] = Yii::t('user', 'Nickname'); |
245
|
|
|
$attributeLabels['gender'] = Yii::t('user', 'Gender'); |
246
|
|
|
$attributeLabels['position'] = Yii::t('organization', 'Member Position'); |
247
|
|
|
$attributeLabels['description'] = Yii::t('organization', 'Description'); |
248
|
|
|
$attributeLabels['role'] = Yii::t('organization', 'Role'); |
249
|
|
|
$attributeLabels['createdFrom'] = Yii::t('user', 'From'); |
250
|
|
|
$attributeLabels['createdTo'] = Yii::t('user', 'To'); |
251
|
|
|
return $attributeLabels; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.