1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link http://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license http://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use vistart\Models\models\BaseUserModel; |
16
|
|
|
use vistart\Models\traits\MultipleBlameableTrait as mb; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Relation features. |
20
|
|
|
* Note: Several methods associated with "inserting", "updating" and "removing" may |
21
|
|
|
* involve more DB operations, I strongly recommend those methods to be placed in |
22
|
|
|
* transaction execution, in order to ensure data consistency. |
23
|
|
|
* 若使用关系组功能,则使用此 trait 的类必须与使用 UserRelationGroupTrait 的类配合使用。 |
24
|
|
|
* @property array $groupGuids the guid array of all groups which owned by current relation. |
25
|
|
|
* @property-read array $allGroups |
26
|
|
|
* @property-read array $nonGroupMembers |
27
|
|
|
* @property-read integer $groupsCount |
28
|
|
|
* @property-read array $groupsRules |
29
|
|
|
* @property boolean $isFavorite |
30
|
|
|
* @property-read \vistart\Models\models\BaseUserRelationModel $opposite |
31
|
|
|
* @version 2.0 |
32
|
|
|
* @author vistart <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
trait UserRelationTrait |
35
|
|
|
{ |
36
|
|
|
use mb { |
37
|
|
|
mb::addBlame as addGroup; |
38
|
|
|
mb::removeBlame as removeGroup; |
39
|
|
|
mb::removeAllBlames as removeAllGroups; |
40
|
|
|
mb::getBlame as getGroup; |
41
|
|
|
mb::getBlameds as getGroupMembers; |
42
|
|
|
mb::getBlameGuids as getGroupGuids; |
43
|
|
|
mb::setBlameGuids as setGroupGuids; |
44
|
|
|
mb::getAllBlames as getAllGroups; |
45
|
|
|
mb::getNonBlameds as getNonGroupMembers; |
46
|
|
|
mb::getBlamesCount as getGroupsCount; |
47
|
|
|
mb::getMultipleBlameableAttributeRules as getGroupsRules; |
48
|
|
|
mb::getEmptyBlamesJson as getEmptyGroupJson; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string the another party of the relation. |
53
|
|
|
*/ |
54
|
|
|
public $otherGuidAttribute = 'other_guid'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $remarkAttribute = 'remark'; |
60
|
|
|
public static $relationSingle = 0; |
61
|
|
|
public static $relationMutual = 1; |
62
|
|
|
public $relationType = 1; |
63
|
|
|
public $relationTypes = [ |
64
|
|
|
0 => 'Single', |
65
|
|
|
1 => 'Mutual', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string the attribute name of which determines the relation type. |
70
|
|
|
*/ |
71
|
|
|
public $mutualTypeAttribute = 'type'; |
72
|
|
|
public static $mutualTypeNormal = 0x00; |
73
|
|
|
public static $mutualTypeSuspend = 0x01; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array Mutual types. |
77
|
|
|
*/ |
78
|
|
|
public static $mutualTypes = [ |
79
|
|
|
0x00 => 'Normal', |
80
|
|
|
0x01 => 'Suspend', |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string the attribute name of which determines the `favorite` field. |
85
|
|
|
*/ |
86
|
|
|
public $favoriteAttribute = 'favorite'; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get whether this relation is favorite or not. |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
1 |
|
public function getIsFavorite() |
93
|
|
|
{ |
94
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
95
|
1 |
|
return is_string($favoriteAttribute) ? (int) $this->$favoriteAttribute > 0 : null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set favorite. |
100
|
|
|
* @param boolean $fav |
101
|
|
|
*/ |
102
|
1 |
|
public function setIsFavorite($fav) |
103
|
|
|
{ |
104
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
105
|
1 |
|
return is_string($favoriteAttribute) ? $this->$favoriteAttribute = ($fav ? 1 : 0) : null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
7 |
|
public function rules() |
112
|
|
|
{ |
113
|
7 |
|
return array_merge(parent::rules(), $this->getUserRelationRules()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Validation rules associated with user relation. |
118
|
|
|
* @return array rules. |
119
|
|
|
*/ |
120
|
7 |
|
public function getUserRelationRules() |
121
|
|
|
{ |
122
|
7 |
|
$rules = []; |
123
|
7 |
|
if ($this->relationType == static::$relationMutual) { |
124
|
|
|
$rules = [ |
125
|
6 |
|
[[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
126
|
6 |
|
[[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
127
|
6 |
|
]; |
128
|
6 |
|
} |
129
|
7 |
|
return array_merge($rules, $this->getRemarkRules(), $this->getFavoriteRules(), $this->getGroupsRules(), $this->getOtherGuidRules()); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get remark. |
134
|
|
|
* @return string remark. |
135
|
|
|
*/ |
136
|
|
|
public function getRemark() |
137
|
|
|
{ |
138
|
|
|
$remarkAttribute = $this->remarkAttribute; |
139
|
|
|
return is_string($remarkAttribute) ? $this->$remarkAttribute : null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set remark. |
144
|
|
|
* @param string $remark |
145
|
|
|
* @return string remark. |
146
|
|
|
*/ |
147
|
|
|
public function setRemark($remark) |
148
|
|
|
{ |
149
|
|
|
$remarkAttribute = $this->remarkAttribute; |
150
|
|
|
return is_string($remarkAttribute) ? $this->$remarkAttribute = $remark : null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Validation rules associated with remark attribute. |
155
|
|
|
* @return array rules. |
156
|
|
|
*/ |
157
|
7 |
|
public function getRemarkRules() |
158
|
|
|
{ |
159
|
7 |
|
return is_string($this->remarkAttribute) ? [ |
160
|
7 |
|
[[$this->remarkAttribute], 'string'], |
161
|
7 |
|
[[$this->remarkAttribute], 'default', 'value' => ''], |
162
|
7 |
|
] : []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Validation rules associated with favorites attribute. |
167
|
|
|
* @return array rules. |
168
|
|
|
*/ |
169
|
7 |
|
public function getFavoriteRules() |
170
|
|
|
{ |
171
|
7 |
|
return is_string($this->favoriteAttribute) ? [ |
172
|
7 |
|
[[$this->favoriteAttribute], 'boolean'], |
173
|
7 |
|
[[$this->favoriteAttribute], 'default', 'value' => 0], |
174
|
7 |
|
] : []; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Validation rules associated with other guid attribute. |
179
|
|
|
* @return array rules. |
180
|
|
|
*/ |
181
|
7 |
|
public function getOtherGuidRules() |
182
|
|
|
{ |
183
|
|
|
$rules = [ |
184
|
7 |
|
[[$this->otherGuidAttribute], 'required'], |
185
|
7 |
|
[[$this->otherGuidAttribute], 'string', 'max' => 36], |
186
|
7 |
|
[[$this->otherGuidAttribute, $this->createdByAttribute], 'unique', 'targetAttribute' => [$this->otherGuidAttribute, $this->createdByAttribute]], |
|
|
|
|
187
|
7 |
|
]; |
188
|
7 |
|
return $rules; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Attach events associated with user relation. |
193
|
|
|
*/ |
194
|
7 |
|
public function initUserRelationEvents() |
195
|
|
|
{ |
196
|
7 |
|
$this->on(static::EVENT_INIT, [$this, 'onInitBlamesLimit']); |
|
|
|
|
197
|
7 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitGroups']); |
|
|
|
|
198
|
7 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitRemark']); |
|
|
|
|
199
|
7 |
|
$this->on(static::$eventMultipleBlamesChanged, [$this, 'onBlamesChanged']); |
|
|
|
|
200
|
7 |
|
$this->on(static::EVENT_AFTER_INSERT, [$this, 'onInsertRelation']); |
|
|
|
|
201
|
7 |
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onUpdateRelation']); |
|
|
|
|
202
|
7 |
|
$this->on(static::EVENT_AFTER_DELETE, [$this, 'onDeleteRelation']); |
|
|
|
|
203
|
7 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get opposite relation to self. |
207
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
208
|
|
|
*/ |
209
|
6 |
|
public function getOpposite() |
210
|
|
|
{ |
211
|
6 |
|
if ($this->isNewRecord) { |
|
|
|
|
212
|
1 |
|
return null; |
213
|
|
|
} |
214
|
6 |
|
$createdByAttribute = $this->createdByAttribute; |
215
|
6 |
|
$otherGuidAttribute = $this->otherGuidAttribute; |
216
|
6 |
|
return static::find()->opposite($this->$createdByAttribute, $this->$otherGuidAttribute); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Build a suspend mutual relation, not support single relation. |
221
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
222
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
223
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
224
|
|
|
* given if exists, or return a new relation. |
225
|
|
|
*/ |
226
|
1 |
|
public static function buildSuspendRelation($user, $other) |
227
|
|
|
{ |
228
|
1 |
|
$relation = static::buildRelation($user, $other); |
229
|
1 |
|
$btAttribute = $relation->mutualTypeAttribute; |
230
|
1 |
|
$relation->$btAttribute = static::$mutualTypeSuspend; |
231
|
1 |
|
return $relation; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Build a normal relation. |
236
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
237
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
238
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
239
|
|
|
* given if exists, or return a new relation. |
240
|
|
|
*/ |
241
|
7 |
|
public static function buildNormalRelation($user, $other) |
242
|
|
|
{ |
243
|
7 |
|
$relation = static::buildRelation($user, $other); |
244
|
7 |
|
if ($relation->relationType == static::$relationMutual) { |
245
|
6 |
|
$btAttribute = $relation->mutualTypeAttribute; |
246
|
6 |
|
$relation->$btAttribute = static::$mutualTypeNormal; |
247
|
6 |
|
} |
248
|
7 |
|
return $relation; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Build relation between initiator and recipient. |
253
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
254
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
255
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel The relation will be |
256
|
|
|
* given if exists, or return a new relation. |
257
|
|
|
*/ |
258
|
7 |
|
protected static function buildRelation($user, $other) |
259
|
|
|
{ |
260
|
7 |
|
$relation = static::find()->initiators($user)->recipients($other)->one(); |
261
|
7 |
|
if (!$relation) { |
262
|
7 |
|
$rni = static::buildNoInitModel(); |
263
|
7 |
|
$createdByAttribute = $rni->createdByAttribute; |
264
|
7 |
|
$otherGuidAttribute = $rni->otherGuidAttribute; |
265
|
7 |
|
if ($user instanceof BaseUserModel) { |
266
|
7 |
|
$user = $user->guid; |
267
|
7 |
|
} |
268
|
7 |
|
if ($other instanceof BaseUserModel) { |
269
|
7 |
|
$other = $other->guid; |
270
|
7 |
|
} |
271
|
7 |
|
$relation = new static([$createdByAttribute => $user, $otherGuidAttribute => $other]); |
|
|
|
|
272
|
7 |
|
} |
273
|
7 |
|
return $relation; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Build opposite mutual relation throughout the current relation, not support |
278
|
|
|
* single relation. The opposite relation will be given if existed. |
279
|
|
|
* @param \vistart\Models\models\BaseUserRelationModel $relation |
280
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
281
|
|
|
*/ |
282
|
6 |
|
protected static function buildOppositeRelation($relation) |
283
|
|
|
{ |
284
|
6 |
|
if ($relation->relationType == static::$relationSingle) { |
285
|
|
|
return null; |
286
|
|
|
} |
287
|
6 |
|
$createdByAttribute = $relation->createdByAttribute; |
288
|
6 |
|
$otherGuidAttribute = $relation->otherGuidAttribute; |
289
|
6 |
|
$mutualTypeAttribute = $relation->mutualTypeAttribute; |
290
|
6 |
|
$opposite = static::buildRelation($relation->$otherGuidAttribute, $relation->$createdByAttribute); |
291
|
6 |
|
$opposite->$mutualTypeAttribute = $relation->$mutualTypeAttribute; |
292
|
6 |
|
return $opposite; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Remove myself. |
297
|
|
|
* @return integer|false The number of relations removed, or false if the remove |
298
|
|
|
* is unsuccessful for some reason. Note that it is possible the number of relations |
299
|
|
|
* removed is 0, even though the remove execution is successful. |
300
|
|
|
*/ |
301
|
2 |
|
public function remove() |
302
|
|
|
{ |
303
|
2 |
|
return $this->delete(); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Remove first relation between initiator(s) and recipient(s). |
308
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
309
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
310
|
|
|
* @return integer|false The number of relations removed. |
311
|
|
|
*/ |
312
|
1 |
|
public static function removeOneRelation($user, $other) |
313
|
|
|
{ |
314
|
1 |
|
return static::find()->initiators($user)->recipients($other)->one()->delete(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Remove all relations between initiator(s) and recipient(s). |
319
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
320
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
321
|
|
|
* @return integer The number of relations removed. |
322
|
|
|
*/ |
323
|
2 |
|
public static function removeAllRelations($user, $other) |
324
|
|
|
{ |
325
|
2 |
|
$rni = static::buildNoInitModel(); |
326
|
2 |
|
$createdByAttribute = $rni->createdByAttribute; |
327
|
2 |
|
$otherGuidAttribute = $rni->otherGuidAttribute; |
328
|
2 |
|
return static::deleteAll([$createdByAttribute => $user, $otherGuidAttribute => $other]); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get first relation between initiator(s) and recipient(s). |
333
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
334
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
335
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
336
|
|
|
*/ |
337
|
1 |
|
public static function findOneRelation($user, $other) |
338
|
|
|
{ |
339
|
1 |
|
return static::find()->initiators($user)->recipients($other)->one(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get first opposite relation between initiator(s) and recipient(s). |
344
|
|
|
* @param BaseUserModel|string $user Initiator or its guid, or array of them. |
345
|
|
|
* @param BaseUserModel|string $other Recipient or its guid, or array of them. |
346
|
|
|
* @return \vistart\Models\models\BaseUserRelationModel |
347
|
|
|
*/ |
348
|
6 |
|
public static function findOneOppositeRelation($user, $other) |
349
|
|
|
{ |
350
|
6 |
|
return static::find()->initiators($other)->recipients($user)->one(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Get user's or users' all relations, or by specified groups. |
355
|
|
|
* @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
356
|
|
|
* @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
357
|
|
|
* @return array all eligible relations |
358
|
|
|
*/ |
359
|
|
|
public static function findOnesAllRelations($user, $groups = null) |
360
|
|
|
{ |
361
|
|
|
return static::find()->initiators($user)->groups($groups)->all(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Initialize groups attribute. |
366
|
|
|
* @param \yii\base\Event $event |
367
|
|
|
*/ |
368
|
7 |
|
public function onInitGroups($event) |
369
|
|
|
{ |
370
|
7 |
|
$sender = $event->sender; |
371
|
7 |
|
$sender->removeAllGroups(); |
372
|
7 |
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Initialize remark attribute. |
376
|
|
|
* @param \yii\base\Event $event |
377
|
|
|
*/ |
378
|
7 |
|
public function onInitRemark($event) |
379
|
|
|
{ |
380
|
7 |
|
$sender = $event->sender; |
381
|
7 |
|
$remarkAttribute = $sender->remarkAttribute; |
382
|
7 |
|
is_string($remarkAttribute) ? $sender->$remarkAttribute = '' : null; |
383
|
7 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* The event triggered after insert new relation. |
387
|
|
|
* The opposite relation should be inserted without triggering events |
388
|
|
|
* simultaneously after new relation inserted, |
389
|
|
|
* @param \yii\base\Event $event |
390
|
|
|
*/ |
391
|
7 |
|
public function onInsertRelation($event) |
392
|
|
|
{ |
393
|
7 |
|
$sender = $event->sender; |
394
|
7 |
|
if ($sender->relationType == static::$relationMutual) { |
395
|
6 |
|
$opposite = static::buildOppositeRelation($sender); |
396
|
6 |
|
$opposite->off(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
397
|
6 |
|
if (!$opposite->save()) { |
398
|
|
|
$opposite->recordWarnings(); |
399
|
|
|
} |
400
|
6 |
|
$opposite->on(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
401
|
6 |
|
} |
402
|
7 |
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* The event triggered after update relation. |
406
|
|
|
* The opposite relation should be updated without triggering events |
407
|
|
|
* simultaneously after existed relation removed. |
408
|
|
|
* @param \yii\base\Event $event |
409
|
|
|
*/ |
410
|
3 |
|
public function onUpdateRelation($event) |
411
|
|
|
{ |
412
|
3 |
|
$sender = $event->sender; |
413
|
3 |
|
if ($sender->relationType == static::$relationMutual) { |
414
|
3 |
|
$opposite = static::buildOppositeRelation($sender); |
415
|
3 |
|
$opposite->off(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
416
|
3 |
|
if (!$opposite->save()) { |
417
|
|
|
$opposite->recordWarnings(); |
418
|
|
|
} |
419
|
3 |
|
$opposite->on(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
420
|
3 |
|
} |
421
|
3 |
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* The event triggered after delete relation. |
425
|
|
|
* The opposite relation should be deleted without triggering events |
426
|
|
|
* simultaneously after existed relation removed. |
427
|
|
|
* @param \yii\base\Event $event |
428
|
|
|
*/ |
429
|
2 |
|
public function onDeleteRelation($event) |
430
|
|
|
{ |
431
|
2 |
|
$sender = $event->sender; |
432
|
2 |
|
if ($sender->relationType == static::$relationMutual) { |
433
|
2 |
|
$createdByAttribute = $sender->createdByAttribute; |
434
|
2 |
|
$otherGuidAttribute = $sender->otherGuidAttribute; |
435
|
2 |
|
$sender->off(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
436
|
2 |
|
static::removeAllRelations($sender->$otherGuidAttribute, $sender->$createdByAttribute); |
437
|
2 |
|
$sender->on(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
438
|
2 |
|
} |
439
|
2 |
|
} |
440
|
|
|
} |
441
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.