|
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\base\models\traits; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
|
16
|
|
|
use rhosocial\base\models\traits\MultipleBlameableTrait as mb; |
|
17
|
|
|
use yii\base\ModelEvent; |
|
18
|
|
|
use yii\base\InvalidConfigException; |
|
19
|
|
|
use yii\base\InvalidValueException; |
|
20
|
|
|
use yii\db\Connection; |
|
21
|
|
|
use yii\db\IntegrityException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Relation features. |
|
25
|
|
|
* This trait should be used in user relation model which is extended from |
|
26
|
|
|
* [[BaseBlameableModel]], and is specified `$userClass` property. And the user |
|
27
|
|
|
* class should be extended from [[BaseUserModel]], or any other classes used |
|
28
|
|
|
* [[UserTrait]]. |
|
29
|
|
|
* Notice: Several methods associated with "inserting", "updating" and "removing" may |
|
30
|
|
|
* involve more DB operations, I strongly recommend those methods to be placed in |
|
31
|
|
|
* transaction execution, in order to ensure data consistency. |
|
32
|
|
|
* If you want to use group feature, the class used [[UserRelationGroupTrait]] |
|
33
|
|
|
* must be used coordinately. |
|
34
|
|
|
* @property array $groupGuids the guid array of all groups which owned by current relation. |
|
35
|
|
|
* @property-read array $favoriteRules |
|
36
|
|
|
* @property boolean $isFavorite |
|
37
|
|
|
* @property-read static $opposite |
|
38
|
|
|
* @property-read array $otherGuidRules |
|
39
|
|
|
* @property string $remark |
|
40
|
|
|
* @property-read array $remarkRules |
|
41
|
|
|
* @property-read array $userRelationRules |
|
42
|
|
|
* @property-read mixed $group |
|
43
|
|
|
* @property-read array $groupMembers |
|
44
|
|
|
* @property array $groupGuids |
|
45
|
|
|
* @property-read array $allGroups |
|
46
|
|
|
* @property-read array $nonGroupMembers |
|
47
|
|
|
* @property-read integer $groupsCount |
|
48
|
|
|
* @property-read array $groupsRules |
|
49
|
|
|
* @version 1.0 |
|
50
|
|
|
* @author vistart <[email protected]> |
|
51
|
|
|
*/ |
|
52
|
|
|
trait UserRelationTrait |
|
53
|
|
|
{ |
|
54
|
|
|
use mb, |
|
55
|
|
|
MutualTrait { |
|
56
|
|
|
mb::addBlame as addGroup; |
|
57
|
|
|
mb::createBlame as createGroup; |
|
58
|
|
|
mb::addOrCreateBlame as addOrCreateGroup; |
|
59
|
|
|
mb::removeBlame as removeGroup; |
|
60
|
|
|
mb::removeAllBlames as removeAllGroups; |
|
61
|
|
|
mb::getBlame as getGroup; |
|
62
|
|
|
mb::getOrCreateBlame as getOrCreateGroup; |
|
63
|
|
|
mb::getBlameds as getGroupMembers; |
|
64
|
|
|
mb::getBlameGuids as getGroupGuids; |
|
65
|
|
|
mb::setBlameGuids as setGroupGuids; |
|
66
|
|
|
mb::getAllBlames as getAllGroups; |
|
67
|
|
|
mb::getNonBlameds as getNonGroupMembers; |
|
68
|
|
|
mb::getBlamesCount as getGroupsCount; |
|
69
|
|
|
mb::getMultipleBlameableAttributeRules as getGroupsRules; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
public $remarkAttribute = 'remark'; |
|
76
|
|
|
public static $relationSingle = 0; |
|
77
|
|
|
public static $relationMutual = 1; |
|
78
|
|
|
public $relationType = 1; |
|
79
|
|
|
public static $relationTypes = [ |
|
80
|
|
|
0 => 'Single', |
|
81
|
|
|
1 => 'Mutual', |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var string the attribute name of which determines the relation type. |
|
86
|
|
|
*/ |
|
87
|
|
|
public $mutualTypeAttribute = 'type'; |
|
88
|
|
|
public static $mutualTypeNormal = 0x00; |
|
89
|
|
|
public static $mutualTypeSuspend = 0x01; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var array Mutual types. |
|
93
|
|
|
*/ |
|
94
|
|
|
public static $mutualTypes = [ |
|
95
|
|
|
0x00 => 'Normal', |
|
96
|
|
|
0x01 => 'Suspend', |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var string the attribute name of which determines the `favorite` field. |
|
101
|
|
|
*/ |
|
102
|
|
|
public $favoriteAttribute = 'favorite'; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Permit to build self relation. |
|
106
|
|
|
* @var boolean |
|
107
|
|
|
*/ |
|
108
|
|
|
public $relationSelf = false; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get whether this relation is favorite or not. |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function getIsFavorite() |
|
115
|
|
|
{ |
|
116
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
|
117
|
1 |
|
return (is_string($favoriteAttribute) && !empty($favoriteAttribute)) ? (int) $this->$favoriteAttribute > 0 : null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set favorite. |
|
122
|
|
|
* @param boolean $fav |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function setIsFavorite($fav) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$favoriteAttribute = $this->favoriteAttribute; |
|
127
|
1 |
|
return (is_string($favoriteAttribute) && !empty($favoriteAttribute)) ? $this->$favoriteAttribute = ($fav ? 1 : 0) : null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritdoc |
|
132
|
|
|
*/ |
|
133
|
24 |
|
public function rules() |
|
134
|
|
|
{ |
|
135
|
24 |
|
return array_merge(parent::rules(), $this->getUserRelationRules()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Validation rules associated with user relation. |
|
140
|
|
|
* @return array rules. |
|
141
|
|
|
*/ |
|
142
|
24 |
|
public function getUserRelationRules() |
|
143
|
|
|
{ |
|
144
|
24 |
|
$rules = []; |
|
145
|
24 |
|
if ($this->relationType == static::$relationMutual) { |
|
146
|
|
|
$rules = [ |
|
147
|
6 |
|
[[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
|
148
|
6 |
|
[[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
|
149
|
|
|
]; |
|
150
|
|
|
} |
|
151
|
24 |
|
return array_merge($rules, $this->getRemarkRules(), $this->getFavoriteRules(), $this->getGroupsRules(), $this->getOtherGuidRules()); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get remark. |
|
156
|
|
|
* @return string remark. |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getRemark() |
|
159
|
|
|
{ |
|
160
|
1 |
|
$remarkAttribute = $this->remarkAttribute; |
|
161
|
1 |
|
return (is_string($remarkAttribute) && !empty($remarkAttribute)) ? $this->$remarkAttribute : null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set remark. |
|
166
|
|
|
* @param string $remark |
|
167
|
|
|
* @return string remark. |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function setRemark($remark) |
|
170
|
|
|
{ |
|
171
|
1 |
|
$remarkAttribute = $this->remarkAttribute; |
|
172
|
1 |
|
return (is_string($remarkAttribute) && !empty($remarkAttribute)) ? $this->$remarkAttribute = $remark : null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Validation rules associated with remark attribute. |
|
177
|
|
|
* @return array rules. |
|
178
|
|
|
*/ |
|
179
|
24 |
|
public function getRemarkRules() |
|
180
|
|
|
{ |
|
181
|
24 |
|
return is_string($this->remarkAttribute) ? [ |
|
182
|
24 |
|
[[$this->remarkAttribute], 'string'], |
|
183
|
24 |
|
[[$this->remarkAttribute], 'default', 'value' => ''], |
|
184
|
24 |
|
] : []; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Validation rules associated with favorites attribute. |
|
189
|
|
|
* @return array rules. |
|
190
|
|
|
*/ |
|
191
|
24 |
|
public function getFavoriteRules() |
|
192
|
|
|
{ |
|
193
|
24 |
|
return is_string($this->favoriteAttribute) ? [ |
|
194
|
24 |
|
[[$this->favoriteAttribute], 'boolean'], |
|
195
|
24 |
|
[[$this->favoriteAttribute], 'default', 'value' => 0], |
|
196
|
24 |
|
] : []; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Validation rules associated with other guid attribute. |
|
201
|
|
|
* @return array rules. |
|
202
|
|
|
*/ |
|
203
|
24 |
|
public function getOtherGuidRules() |
|
204
|
|
|
{ |
|
205
|
24 |
|
$rules = array_merge($this->getMutualRules(), [ |
|
206
|
24 |
|
[[$this->otherGuidAttribute, $this->createdByAttribute], 'unique', 'targetAttribute' => [$this->otherGuidAttribute, $this->createdByAttribute]], |
|
|
|
|
|
|
207
|
|
|
]); |
|
208
|
24 |
|
return $rules; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Attach events associated with user relation. |
|
213
|
|
|
*/ |
|
214
|
27 |
|
public function initUserRelationEvents() |
|
215
|
|
|
{ |
|
216
|
27 |
|
$this->on(static::EVENT_INIT, [$this, 'onInitBlamesLimit']); |
|
|
|
|
|
|
217
|
27 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitGroups']); |
|
|
|
|
|
|
218
|
27 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitRemark']); |
|
|
|
|
|
|
219
|
27 |
|
$this->on(static::$eventMultipleBlamesChanged, [$this, 'onBlamesChanged']); |
|
|
|
|
|
|
220
|
27 |
|
$this->on(static::EVENT_AFTER_INSERT, [$this, 'onInsertRelation']); |
|
|
|
|
|
|
221
|
27 |
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onUpdateRelation']); |
|
|
|
|
|
|
222
|
27 |
|
$this->on(static::EVENT_AFTER_DELETE, [$this, 'onDeleteRelation']); |
|
|
|
|
|
|
223
|
27 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get opposite relation against self. |
|
227
|
|
|
* @return static |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function getOpposite() |
|
230
|
|
|
{ |
|
231
|
1 |
|
if ($this->isNewRecord) { |
|
|
|
|
|
|
232
|
1 |
|
return null; |
|
233
|
|
|
} |
|
234
|
1 |
|
return static::find()->opposite($this->initiator, $this->recipient); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Check whether the initiator is followed by recipient. |
|
239
|
|
|
* @param BaseUserModel $initiator |
|
240
|
|
|
* @param BaseUserModel $recipient |
|
241
|
|
|
* @return boolean |
|
242
|
|
|
*/ |
|
243
|
4 |
|
public static function isFollowed($initiator, $recipient) |
|
244
|
|
|
{ |
|
245
|
4 |
|
return static::find()->initiators($recipient)->recipients($initiator)->exists(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Check whether the initiator is following recipient. |
|
250
|
|
|
* @param BaseUserModel $initiator |
|
251
|
|
|
* @param BaseUserModel $recipient |
|
252
|
|
|
* @return boolean |
|
253
|
|
|
*/ |
|
254
|
4 |
|
public static function isFollowing($initiator, $recipient) |
|
255
|
|
|
{ |
|
256
|
4 |
|
return static::find()->initiators($initiator)->recipients($recipient)->exists(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Check whether the initiator is following and followed by recipient mutually (Single Relation). |
|
261
|
|
|
* Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
|
262
|
|
|
* @param BaseUserModel $initiator |
|
263
|
|
|
* @param BaseUserModel $recipient |
|
264
|
|
|
* @return boolean |
|
265
|
|
|
*/ |
|
266
|
3 |
|
public static function isMutual($initiator, $recipient) |
|
267
|
|
|
{ |
|
268
|
3 |
|
return static::isFollowed($initiator, $recipient) && static::isFollowing($initiator, $recipient); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Check whether the initiator is following and followed by recipient mutually (Single Relation). |
|
273
|
|
|
* Or check whether the initiator and recipient are friend if the mutual type is normal. |
|
274
|
|
|
* @param BaseUserModel $initiator |
|
275
|
|
|
* @param BaseUserModel $recipient |
|
276
|
|
|
* @return boolean |
|
277
|
|
|
*/ |
|
278
|
6 |
|
public static function isFriend($initiator, $recipient) |
|
279
|
|
|
{ |
|
280
|
6 |
|
$query = static::find(); |
|
281
|
6 |
|
$model = $query->noInitModel; |
|
282
|
|
|
/* @var $model static */ |
|
283
|
6 |
|
if ($model->relationType == static::$relationSingle) { |
|
284
|
2 |
|
return static::isMutual($initiator, $recipient); |
|
285
|
|
|
} |
|
286
|
4 |
|
if ($model->relationType == static::$relationMutual) { |
|
287
|
4 |
|
$relation = static::find()->initiators($initiator)->recipients($recipient)->andWhere([$model->mutualTypeAttribute => static::$mutualTypeNormal])->exists(); |
|
288
|
4 |
|
$inverse = static::find()->recipients($initiator)->initiators($recipient)->andWhere([$model->mutualTypeAttribute => static::$mutualTypeNormal])->exists(); |
|
289
|
4 |
|
return $relation && $inverse; |
|
290
|
|
|
} |
|
291
|
|
|
return false; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Build new or return existed suspend mutual relation, or return null if |
|
296
|
|
|
* current type is not mutual. |
|
297
|
|
|
* @see buildRelation() |
|
298
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
|
299
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
|
300
|
|
|
* @return static The relation will be |
|
301
|
|
|
* given if exists, or return a new relation. |
|
302
|
|
|
*/ |
|
303
|
9 |
|
public static function buildSuspendRelation($user, $other) |
|
304
|
|
|
{ |
|
305
|
9 |
|
$relation = static::buildRelation($user, $other); |
|
306
|
9 |
|
if (!$relation || $relation->relationType != static::$relationMutual) { |
|
307
|
1 |
|
return null; |
|
308
|
|
|
} |
|
309
|
8 |
|
$relation->setMutualType(static::$mutualTypeSuspend); |
|
310
|
8 |
|
return $relation; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Build new or return existed normal relation. |
|
315
|
|
|
* The status of mutual relation will be changed to normal if it is not. |
|
316
|
|
|
* @see buildRelation() |
|
317
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
|
318
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
|
319
|
|
|
* @return static The relation will be |
|
320
|
|
|
* given if exists, or return a new relation. |
|
321
|
|
|
*/ |
|
322
|
27 |
|
public static function buildNormalRelation($user, $other) |
|
323
|
|
|
{ |
|
324
|
27 |
|
$relation = static::buildRelation($user, $other); |
|
325
|
27 |
|
if (!$relation) { |
|
326
|
1 |
|
return null; |
|
327
|
|
|
} |
|
328
|
27 |
|
if ($relation->relationType == static::$relationMutual) { |
|
329
|
8 |
|
$relation->setMutualType(static::$mutualTypeNormal); |
|
330
|
|
|
} |
|
331
|
27 |
|
return $relation; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Transform relation from suspend to normal. |
|
336
|
|
|
* Note: You should ensure the relation model is not new one. |
|
337
|
|
|
* @param static $relation |
|
|
|
|
|
|
338
|
|
|
* @return boolean |
|
339
|
|
|
*/ |
|
340
|
1 |
|
public static function transformSuspendToNormal($relation) |
|
341
|
|
|
{ |
|
342
|
1 |
|
if (!$relation || !($relation instanceof static) || $relation->getIsNewRecord() || $relation->relationType != static::$relationMutual) { |
|
|
|
|
|
|
343
|
|
|
return false; |
|
344
|
|
|
} |
|
345
|
1 |
|
$relation = static::buildNormalRelation($relation->initiator, $relation->recipient); |
|
346
|
1 |
|
return $relation->save(); |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Revert relation from normal to suspend. |
|
351
|
|
|
* Note: You should ensure the relation model is not new one. |
|
352
|
|
|
* @param static $relation |
|
|
|
|
|
|
353
|
|
|
* @return boolean |
|
354
|
|
|
*/ |
|
355
|
1 |
|
public static function revertNormalToSuspend($relation) |
|
356
|
|
|
{ |
|
357
|
1 |
|
if (!$relation || !($relation instanceof static) || $relation->getIsNewRecord() || $relation->relationType != static::$relationMutual) { |
|
|
|
|
|
|
358
|
|
|
return false; |
|
359
|
|
|
} |
|
360
|
1 |
|
$relation = static::buildSuspendRelation($relation->initiator, $relation->recipient); |
|
361
|
1 |
|
return $relation->save(); |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Build new or return existed relation between initiator and recipient. |
|
366
|
|
|
* If relation between initiator and recipient is not found, new relation will |
|
367
|
|
|
* be built. If initiator and recipient are the same one and it is not allowed |
|
368
|
|
|
* to build self relation, null will be given. |
|
369
|
|
|
* If you want to know whether the relation exists, you can check the return |
|
370
|
|
|
* value of `getIsNewRecord()` method. |
|
371
|
|
|
* @param BaseUserModel|string $user Initiator or its GUID. |
|
372
|
|
|
* @param BaseUserModel|string $other Recipient or its GUID. |
|
373
|
|
|
* @return static The relation will be |
|
374
|
|
|
* given if exists, or return a new relation. Or return null if not allowed |
|
375
|
|
|
* to build self relation, |
|
376
|
|
|
*/ |
|
377
|
27 |
|
protected static function buildRelation($user, $other) |
|
378
|
|
|
{ |
|
379
|
27 |
|
$relationQuery = static::find()->initiators($user)->recipients($other); |
|
380
|
27 |
|
$noInit = $relationQuery->noInitModel; |
|
381
|
27 |
|
$relation = $relationQuery->one(); |
|
382
|
27 |
|
if (!$relation) { |
|
383
|
27 |
|
$createdByAttribute = $noInit->createdByAttribute; |
|
384
|
27 |
|
$otherGuidAttribute = $noInit->otherGuidAttribute; |
|
385
|
27 |
|
$userClass = $noInit->userClass; |
|
386
|
27 |
|
if ($user instanceof BaseUserModel) { |
|
387
|
27 |
|
$userClass = $userClass ? : $user->className(); |
|
388
|
27 |
|
$user = $user->getGUID(); |
|
389
|
|
|
} |
|
390
|
27 |
|
if ($other instanceof BaseUserModel) { |
|
391
|
27 |
|
$other = $other->getGUID(); |
|
392
|
|
|
} |
|
393
|
27 |
|
if (!$noInit->relationSelf && $user == $other) { |
|
394
|
1 |
|
return null; |
|
395
|
|
|
} |
|
396
|
27 |
|
$relation = new static([$createdByAttribute => $user, $otherGuidAttribute => $other, 'userClass' => $userClass]); |
|
|
|
|
|
|
397
|
|
|
} |
|
398
|
27 |
|
return $relation; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Build opposite relation throughout the current relation. The opposite |
|
403
|
|
|
* relation will be given if existed. |
|
404
|
|
|
* @param static $relation |
|
|
|
|
|
|
405
|
|
|
* @return static |
|
406
|
|
|
*/ |
|
407
|
6 |
|
protected static function buildOppositeRelation($relation) |
|
408
|
|
|
{ |
|
409
|
6 |
|
if (!$relation) { |
|
410
|
|
|
return null; |
|
411
|
|
|
} |
|
412
|
6 |
|
$createdByAttribute = $relation->createdByAttribute; |
|
413
|
6 |
|
$otherGuidAttribute = $relation->otherGuidAttribute; |
|
414
|
6 |
|
$opposite = static::buildRelation($relation->$otherGuidAttribute, $relation->$createdByAttribute); |
|
415
|
6 |
|
if ($relation->relationType == static::$relationSingle) { |
|
416
|
|
|
$opposite->relationType = static::$relationSingle; |
|
417
|
6 |
|
} elseif ($relation->relationType == static::$relationMutual) { |
|
418
|
6 |
|
$opposite->setMutualType($relation->getMutualType()); |
|
419
|
|
|
} |
|
420
|
6 |
|
return $opposite; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* Get mutual type. |
|
425
|
|
|
* @return integer |
|
426
|
|
|
*/ |
|
427
|
6 |
|
public function getMutualType() |
|
428
|
|
|
{ |
|
429
|
6 |
|
$btAttribute = $this->mutualTypeAttribute; |
|
430
|
6 |
|
if (is_string($btAttribute) && !empty($btAttribute)) { |
|
431
|
6 |
|
return $this->$btAttribute; |
|
432
|
|
|
} |
|
433
|
|
|
return static::$mutualTypeNormal; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Set mutual type. |
|
438
|
|
|
* @param integer $type |
|
439
|
|
|
* @return integer |
|
440
|
|
|
*/ |
|
441
|
8 |
|
protected function setMutualType($type) |
|
442
|
|
|
{ |
|
443
|
8 |
|
if (!array_key_exists($type, static::$mutualTypes)) { |
|
444
|
|
|
$type = static::$mutualTypeNormal; |
|
445
|
|
|
} |
|
446
|
8 |
|
$btAttribute = $this->mutualTypeAttribute; |
|
447
|
8 |
|
if (is_string($btAttribute) && !empty($btAttribute)) { |
|
448
|
8 |
|
return $this->$btAttribute = $type; |
|
449
|
|
|
} |
|
450
|
|
|
return static::$mutualTypeNormal; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Insert relation, the process is placed in a transaction. |
|
455
|
|
|
* Note: This feature only support relational databases and skip all errors. |
|
456
|
|
|
* If you don't want to use transaction or database doesn't support it, |
|
457
|
|
|
* please use `save()` directly. |
|
458
|
|
|
* @param static $relation |
|
|
|
|
|
|
459
|
|
|
* @param Connection $db |
|
460
|
|
|
* @return boolean |
|
461
|
|
|
* @throws InvalidValueException |
|
462
|
|
|
* @throws InvalidConfigException |
|
463
|
|
|
* @throws IntegrityException |
|
464
|
|
|
*/ |
|
465
|
|
|
public static function insertRelation($relation, Connection $db = null) |
|
466
|
|
|
{ |
|
467
|
|
|
if (!$relation || !($relation instanceof static)) { |
|
468
|
|
|
return false; |
|
469
|
|
|
} |
|
470
|
|
|
if (!$relation->getIsNewRecord()) { |
|
|
|
|
|
|
471
|
|
|
throw new InvalidValueException('This relation is not new one.'); |
|
472
|
|
|
} |
|
473
|
|
|
if (!$db && isset(\Yii::$app->db) && \Yii::$app->db instanceof Connection) { |
|
474
|
|
|
$db = \Yii::$app->db; |
|
475
|
|
|
} |
|
476
|
|
|
if (!$db) { |
|
477
|
|
|
throw new InvalidConfigException('Invalid database connection.'); |
|
478
|
|
|
} |
|
479
|
|
|
/* @var $db Connection */ |
|
480
|
|
|
$transaction = $db->beginTransaction(); |
|
481
|
|
|
try { |
|
482
|
|
|
if (!$relation->save()) { |
|
|
|
|
|
|
483
|
|
|
throw new IntegrityException('Relation insert failed.'); |
|
484
|
|
|
} |
|
485
|
|
|
$transaction->commit(); |
|
486
|
|
|
} catch (\Exception $ex) { |
|
487
|
|
|
$transaction->rollBack(); |
|
488
|
|
|
return false; |
|
489
|
|
|
} |
|
490
|
|
|
return true; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Remove myself. |
|
495
|
|
|
* @return integer|false The number of relations removed, or false if the remove |
|
496
|
|
|
* is unsuccessful for some reason. Note that it is possible the number of relations |
|
497
|
|
|
* removed is 0, even though the remove execution is successful. |
|
498
|
|
|
*/ |
|
499
|
27 |
|
public function remove() |
|
500
|
|
|
{ |
|
501
|
27 |
|
return $this->delete(); |
|
|
|
|
|
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Remove first relation between initiator(s) and recipient(s). |
|
506
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
|
507
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
|
508
|
|
|
* @return integer|false The number of relations removed. |
|
509
|
|
|
*/ |
|
510
|
1 |
|
public static function removeOneRelation($user, $other) |
|
511
|
|
|
{ |
|
512
|
1 |
|
return static::find()->initiators($user)->recipients($other)->one()->remove(); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Remove all relations between initiator(s) and recipient(s). |
|
517
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
|
518
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
|
519
|
|
|
* @return integer The number of relations removed. |
|
520
|
|
|
*/ |
|
521
|
9 |
|
public static function removeAllRelations($user, $other) |
|
522
|
|
|
{ |
|
523
|
9 |
|
$rni = static::buildNoInitModel(); |
|
524
|
9 |
|
$createdByAttribute = $rni->createdByAttribute; |
|
525
|
9 |
|
$otherGuidAttribute = $rni->otherGuidAttribute; |
|
526
|
9 |
|
return static::deleteAll([$createdByAttribute => BaseUserModel::compositeGUIDs($user), $otherGuidAttribute => BaseUserModel::compositeGUIDs($other)]); |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Get first relation between initiator(s) and recipient(s). |
|
531
|
|
|
* @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
|
532
|
|
|
* @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
|
533
|
|
|
* @return static |
|
534
|
|
|
*/ |
|
535
|
4 |
|
public static function findOneRelation($user, $other) |
|
536
|
|
|
{ |
|
537
|
4 |
|
return static::find()->initiators($user)->recipients($other)->one(); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
/** |
|
541
|
|
|
* Get first opposite relation between initiator(s) and recipient(s). |
|
542
|
|
|
* @param BaseUserModel|string $user Initiator or its guid, or array of them. |
|
543
|
|
|
* @param BaseUserModel|string $other Recipient or its guid, or array of them. |
|
544
|
|
|
* @return static |
|
545
|
|
|
*/ |
|
546
|
1 |
|
public static function findOneOppositeRelation($user, $other) |
|
547
|
|
|
{ |
|
548
|
1 |
|
return static::find()->initiators($other)->recipients($user)->one(); |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* Get user's or users' all relations, or by specified groups. |
|
553
|
|
|
* @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
|
554
|
|
|
* @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
|
555
|
|
|
* or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
|
556
|
|
|
* @return array all eligible relations |
|
557
|
|
|
*/ |
|
558
|
1 |
|
public static function findOnesAllRelations($user, $groups = null) |
|
559
|
|
|
{ |
|
560
|
1 |
|
return static::find()->initiators($user)->groups($groups)->all(); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Initialize groups attribute. |
|
565
|
|
|
* @param ModelEvent $event |
|
566
|
|
|
*/ |
|
567
|
27 |
|
public function onInitGroups($event) |
|
568
|
|
|
{ |
|
569
|
27 |
|
$sender = $event->sender; |
|
570
|
27 |
|
$sender->removeAllGroups(); |
|
571
|
27 |
|
} |
|
572
|
|
|
|
|
573
|
|
|
/** |
|
574
|
|
|
* Initialize remark attribute. |
|
575
|
|
|
* @param ModelEvent $event |
|
576
|
|
|
*/ |
|
577
|
27 |
|
public function onInitRemark($event) |
|
578
|
|
|
{ |
|
579
|
27 |
|
$sender = $event->sender; |
|
580
|
27 |
|
$remarkAttribute = $sender->remarkAttribute; |
|
581
|
27 |
|
is_string($remarkAttribute) ? $sender->$remarkAttribute = '' : null; |
|
582
|
27 |
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* The event triggered after insert new relation. |
|
586
|
|
|
* The opposite relation should be inserted without triggering events |
|
587
|
|
|
* simultaneously after new relation inserted, |
|
588
|
|
|
* @param ModelEvent $event |
|
589
|
|
|
* @throws IntegrityException throw if insert failed. |
|
590
|
|
|
*/ |
|
591
|
24 |
|
public function onInsertRelation($event) |
|
592
|
|
|
{ |
|
593
|
24 |
|
$sender = $event->sender; |
|
594
|
24 |
|
if ($sender->relationType == static::$relationMutual) { |
|
595
|
6 |
|
$opposite = static::buildOppositeRelation($sender); |
|
596
|
6 |
|
$opposite->off(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
|
|
|
|
|
|
597
|
6 |
|
if (!$opposite->save()) { |
|
|
|
|
|
|
598
|
|
|
$opposite->recordWarnings(); |
|
|
|
|
|
|
599
|
|
|
throw new IntegrityException('Reverse relation insert failed.'); |
|
600
|
|
|
} |
|
601
|
6 |
|
$opposite->on(static::EVENT_AFTER_INSERT, [$opposite, 'onInsertRelation']); |
|
|
|
|
|
|
602
|
|
|
} |
|
603
|
24 |
|
} |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* The event triggered after update relation. |
|
607
|
|
|
* The opposite relation should be updated without triggering events |
|
608
|
|
|
* simultaneously after existed relation removed. |
|
609
|
|
|
* @param ModelEvent $event |
|
610
|
|
|
* @throw IntegrityException throw if update failed. |
|
611
|
|
|
*/ |
|
612
|
4 |
|
public function onUpdateRelation($event) |
|
613
|
|
|
{ |
|
614
|
4 |
|
$sender = $event->sender; |
|
615
|
4 |
|
if ($sender->relationType == static::$relationMutual) { |
|
616
|
2 |
|
$opposite = static::buildOppositeRelation($sender); |
|
617
|
2 |
|
$opposite->off(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
|
|
|
|
|
|
618
|
2 |
|
if (!$opposite->save()) { |
|
|
|
|
|
|
619
|
|
|
$opposite->recordWarnings(); |
|
|
|
|
|
|
620
|
|
|
throw new IntegrityException('Reverse relation update failed.'); |
|
621
|
|
|
} |
|
622
|
2 |
|
$opposite->on(static::EVENT_AFTER_UPDATE, [$opposite, 'onUpdateRelation']); |
|
|
|
|
|
|
623
|
|
|
} |
|
624
|
4 |
|
} |
|
625
|
|
|
|
|
626
|
|
|
/** |
|
627
|
|
|
* The event triggered after delete relation. |
|
628
|
|
|
* The opposite relation should be deleted without triggering events |
|
629
|
|
|
* simultaneously after existed relation removed. |
|
630
|
|
|
* @param ModelEvent $event |
|
631
|
|
|
*/ |
|
632
|
27 |
|
public function onDeleteRelation($event) |
|
633
|
|
|
{ |
|
634
|
27 |
|
$sender = $event->sender; |
|
635
|
27 |
|
if ($sender->relationType == static::$relationMutual) { |
|
636
|
8 |
|
$createdByAttribute = $sender->createdByAttribute; |
|
637
|
8 |
|
$otherGuidAttribute = $sender->otherGuidAttribute; |
|
638
|
8 |
|
$sender->off(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
|
639
|
8 |
|
static::removeAllRelations($sender->$otherGuidAttribute, $sender->$createdByAttribute); |
|
640
|
8 |
|
$sender->on(static::EVENT_AFTER_DELETE, [$sender, 'onDeleteRelation']); |
|
641
|
|
|
} |
|
642
|
27 |
|
} |
|
643
|
|
|
} |
|
644
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.