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\helpers\Number; |
16
|
|
|
use rhosocial\base\models\queries\BaseUserQuery; |
17
|
|
|
use yii\base\InvalidParamException; |
18
|
|
|
use yii\base\ModelEvent; |
19
|
|
|
use yii\base\NotSupportedException; |
20
|
|
|
use yii\behaviors\BlameableBehavior; |
21
|
|
|
use yii\caching\TagDependency; |
22
|
|
|
use yii\data\Pagination; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This trait is used for building blameable model. It contains following features: |
26
|
|
|
* 1.单列内容;多列内容待定; |
27
|
|
|
* 2.内容类型;具体类型应当自定义; |
28
|
|
|
* 3.内容规则;自动生成; |
29
|
|
|
* 4.归属用户 GUID; |
30
|
|
|
* 5.创建用户 GUID; |
31
|
|
|
* 6.上次更新用户 GUID; |
32
|
|
|
* 7.Confirmation features, provided by [[ConfirmationTrait]]; |
33
|
|
|
* @property-read array $blameableAttributeRules Get all rules associated with |
34
|
|
|
* blameable. |
35
|
|
|
* @property array $blameableRules Get or set all the rules associated with |
36
|
|
|
* creator, updater, content and its ID, as well as all the inherited rules. |
37
|
|
|
* @property array $blameableBehaviors Get or set all the behaviors assoriated |
38
|
|
|
* with creator and updater, as well as all the inherited behaviors. |
39
|
|
|
* @property-read array $descriptionRules Get description property rules. |
40
|
|
|
* @property-read mixed $content Content. |
41
|
|
|
* @property-read boolean $contentCanBeEdited Whether this content could be edited. |
42
|
|
|
* @property-read array $contentRules Get content rules. |
43
|
|
|
* @property BaseUserModel $user |
44
|
|
|
* @property BaseUserModel $updater |
45
|
|
|
* @version 1.0 |
46
|
|
|
* @author vistart <[email protected]> |
47
|
|
|
*/ |
48
|
|
|
trait BlameableTrait |
49
|
|
|
{ |
50
|
|
|
use ConfirmationTrait, |
51
|
|
|
SelfBlameableTrait; |
52
|
|
|
|
53
|
|
|
private $blameableLocalRules = []; |
54
|
|
|
private $blameableLocalBehaviors = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var boolean|string|array Specify the attribute(s) name of content(s). If |
58
|
|
|
* there is only one content attribute, you can assign its name. Or there |
59
|
|
|
* is multiple attributes associated with contents, you can assign their |
60
|
|
|
* names in array. If you don't want to use this feature, please assign |
61
|
|
|
* false. |
62
|
|
|
* For example: |
63
|
|
|
* ```php |
64
|
|
|
* public $contentAttribute = 'comment'; // only one field named as 'comment'. |
65
|
|
|
* ``` |
66
|
|
|
* or |
67
|
|
|
* ```php |
68
|
|
|
* public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
69
|
|
|
* ``` |
70
|
|
|
* or |
71
|
|
|
* ```php |
72
|
|
|
* public $contentAttribute = false; // no need of this feature. |
73
|
|
|
* ``` |
74
|
|
|
* If you don't need this feature, you should add rules corresponding with |
75
|
|
|
* `content` in `rules()` method of your user model by yourself. |
76
|
|
|
*/ |
77
|
|
|
public $contentAttribute = 'content'; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var array built-in validator name or validatation method name and |
81
|
|
|
* additional parameters. |
82
|
|
|
*/ |
83
|
|
|
public $contentAttributeRule = ['string', 'max' => 255]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var boolean|string Specify the field which stores the type of content. |
87
|
|
|
*/ |
88
|
|
|
public $contentTypeAttribute = false; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var boolean|array Specify the logic type of content, not data type. If |
92
|
|
|
* your content doesn't need this feature. please specify false. If the |
93
|
|
|
* $contentAttribute is specified to false, this attribute will be skipped. |
94
|
|
|
* ```php |
95
|
|
|
* public $contentTypes = [ |
96
|
|
|
* 'public', |
97
|
|
|
* 'private', |
98
|
|
|
* 'friend', |
99
|
|
|
* ]; |
100
|
|
|
* ``` |
101
|
|
|
*/ |
102
|
|
|
public $contentTypes = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var boolean|string This attribute speicfy the name of description |
106
|
|
|
* attribute. If this attribute is assigned to false, this feature will be |
107
|
|
|
* skipped. |
108
|
|
|
*/ |
109
|
|
|
public $descriptionAttribute = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var string |
113
|
|
|
*/ |
114
|
|
|
public $initDescription = ''; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var string the attribute that will receive current user ID value. This |
118
|
|
|
* attribute must be assigned. |
119
|
|
|
*/ |
120
|
|
|
public $createdByAttribute = "user_guid"; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var string the attribute that will receive current user ID value. |
124
|
|
|
* Set this property to false if you do not want to record the updater ID. |
125
|
|
|
*/ |
126
|
|
|
public $updatedByAttribute = "user_guid"; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var boolean Add combinated unique rule if assigned to true. |
130
|
|
|
*/ |
131
|
|
|
public $idCreatorCombinatedUnique = true; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @var boolean|string The name of user class which own the current entity. |
135
|
|
|
* If this attribute is assigned to false, this feature will be skipped, and |
136
|
|
|
* when you use create() method of UserTrait, it will be assigned with |
137
|
|
|
* current user class. |
138
|
|
|
*/ |
139
|
|
|
public $userClass; |
140
|
|
|
public static $cacheKeyBlameableRules = 'blameable_rules'; |
141
|
|
|
public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
142
|
|
|
public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
143
|
|
|
public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
* ------------ |
148
|
|
|
* The classical rules is like following: |
149
|
|
|
* [ |
150
|
|
|
* ['guid', 'required'], |
151
|
|
|
* ['guid', 'unique'], |
152
|
|
|
* ['guid', 'string', 'max' => 36], |
153
|
|
|
* |
154
|
|
|
* ['id', 'required'], |
155
|
|
|
* ['id', 'unique'], |
156
|
|
|
* ['id', 'string', 'max' => 4], |
157
|
|
|
* |
158
|
|
|
* ['created_at', 'safe'], |
159
|
|
|
* ['updated_at', 'safe'], |
160
|
|
|
* |
161
|
|
|
* ['ip_type', 'in', 'range' => [4, 6]], |
162
|
|
|
* ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
163
|
|
|
* ] |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
21 |
|
public function rules() |
167
|
|
|
{ |
168
|
21 |
|
return $this->getBlameableRules(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritdoc |
173
|
|
|
*/ |
174
|
21 |
|
public function behaviors() |
175
|
|
|
{ |
176
|
21 |
|
return $this->getBlameableBehaviors(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get total of contents which owned by their owner. |
181
|
|
|
* @return integer |
182
|
|
|
*/ |
183
|
1 |
|
public function countOfOwner() |
184
|
|
|
{ |
185
|
1 |
|
$createdByAttribute = $this->createdByAttribute; |
186
|
1 |
|
return static::find()->where([$createdByAttribute => $this->$createdByAttribute])->count(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get content. |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
3 |
|
public function getContent() |
194
|
|
|
{ |
195
|
3 |
|
$contentAttribute = $this->contentAttribute; |
196
|
3 |
|
if ($contentAttribute === false) { |
197
|
|
|
return null; |
198
|
|
|
} |
199
|
3 |
|
if (is_array($contentAttribute)) { |
200
|
|
|
$content = []; |
201
|
|
|
foreach ($contentAttribute as $key => $value) { |
202
|
|
|
$content[$key] = $this->$value; |
203
|
|
|
} |
204
|
|
|
return $content; |
205
|
|
|
} |
206
|
3 |
|
return $this->$contentAttribute; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Set content. |
211
|
|
|
* @param mixed $content |
212
|
|
|
*/ |
213
|
18 |
|
public function setContent($content) |
214
|
|
|
{ |
215
|
18 |
|
$contentAttribute = $this->contentAttribute; |
216
|
18 |
|
if ($contentAttribute === false) { |
217
|
|
|
return; |
218
|
|
|
} |
219
|
18 |
|
if (is_array($contentAttribute)) { |
220
|
|
|
foreach ($contentAttribute as $key => $value) { |
221
|
|
|
$this->$value = $content[$key]; |
222
|
|
|
} |
223
|
|
|
return; |
224
|
|
|
} |
225
|
18 |
|
$this->$contentAttribute = $content; |
226
|
18 |
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Determines whether content could be edited. Your should implement this |
230
|
|
|
* method by yourself. |
231
|
|
|
* @return boolean |
232
|
|
|
* @throws NotSupportedException |
233
|
|
|
*/ |
234
|
|
|
public function getContentCanBeEdited() |
235
|
|
|
{ |
236
|
|
|
if ($this->contentAttribute === false) { |
237
|
|
|
return false; |
238
|
|
|
} |
239
|
|
|
throw new NotSupportedException("This method is not implemented."); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Check it has been ever edited. |
244
|
|
|
* @return boolean Whether this content has ever been edited. |
245
|
|
|
*/ |
246
|
1 |
|
public function hasEverEdited() |
247
|
|
|
{ |
248
|
1 |
|
$createdAtAttribute = $this->createdByAttribute; |
249
|
1 |
|
$updatedAtAttribute = $this->updatedByAttribute; |
250
|
1 |
|
if (!$createdAtAttribute || !$updatedAtAttribute) { |
251
|
|
|
return false; |
252
|
|
|
} |
253
|
1 |
|
return $this->$createdAtAttribute !== $this->$updatedAtAttribute; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get blameable rules cache key. |
258
|
|
|
* @return string cache key. |
259
|
|
|
*/ |
260
|
21 |
|
public function getBlameableRulesCacheKey() |
261
|
|
|
{ |
262
|
21 |
|
return static::class . $this->cachePrefix . static::$cacheKeyBlameableRules; |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get blameable rules cache tag. |
267
|
|
|
* @return string cache tag |
268
|
|
|
*/ |
269
|
21 |
|
public function getBlameableRulesCacheTag() |
270
|
|
|
{ |
271
|
21 |
|
return static::class . $this->cachePrefix . static::$cacheTagBlameableRules; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the rules associated with content to be blamed. |
276
|
|
|
* @return array rules. |
277
|
|
|
*/ |
278
|
21 |
|
public function getBlameableRules() |
279
|
|
|
{ |
280
|
21 |
|
$cache = $this->getCache(); |
|
|
|
|
281
|
21 |
|
if ($cache) { |
282
|
21 |
|
$this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey()); |
283
|
21 |
|
} |
284
|
|
|
// 若当前规则不为空,且是数组,则认为是规则数组,直接返回。 |
285
|
21 |
|
if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) { |
286
|
12 |
|
return $this->blameableLocalRules; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// 父类规则与确认规则合并。 |
290
|
21 |
|
if ($cache) { |
291
|
21 |
|
TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]); |
|
|
|
|
292
|
21 |
|
} |
293
|
21 |
|
$rules = array_merge( |
294
|
21 |
|
parent::rules(), |
|
|
|
|
295
|
21 |
|
$this->getConfirmationRules(), |
296
|
21 |
|
$this->getBlameableAttributeRules(), |
297
|
21 |
|
$this->getDescriptionRules(), |
298
|
21 |
|
$this->getContentRules(), |
299
|
21 |
|
$this->getSelfBlameableRules() |
300
|
21 |
|
); |
301
|
21 |
|
$this->setBlameableRules($rules); |
302
|
21 |
|
return $this->blameableLocalRules; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
307
|
|
|
* and `idAttribute`-`createdByAttribute` combination unique. |
308
|
|
|
* @return array rules. |
309
|
|
|
*/ |
310
|
21 |
|
public function getBlameableAttributeRules() |
311
|
|
|
{ |
312
|
21 |
|
$rules = []; |
313
|
|
|
// 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。 |
314
|
21 |
|
if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) { |
315
|
|
|
throw new NotSupportedException('You must assign the creator.'); |
316
|
|
|
} |
317
|
21 |
|
$rules[] = [ |
318
|
21 |
|
[$this->createdByAttribute], |
319
|
21 |
|
'safe', |
320
|
|
|
]; |
321
|
|
|
|
322
|
21 |
|
if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) { |
323
|
12 |
|
$rules[] = [ |
324
|
12 |
|
[$this->updatedByAttribute], |
325
|
12 |
|
'safe', |
326
|
|
|
]; |
327
|
12 |
|
} |
328
|
|
|
|
329
|
21 |
|
if ($this->idCreatorCombinatedUnique && is_string($this->idAttribute)) { |
|
|
|
|
330
|
21 |
|
$rules ['id'] = [ |
331
|
21 |
|
[$this->idAttribute, |
|
|
|
|
332
|
21 |
|
$this->createdByAttribute], |
333
|
21 |
|
'unique', |
334
|
21 |
|
'targetAttribute' => [$this->idAttribute, |
|
|
|
|
335
|
21 |
|
$this->createdByAttribute], |
336
|
|
|
]; |
337
|
21 |
|
} |
338
|
21 |
|
return $rules; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Get the rules associated with `description` attribute. |
343
|
|
|
* @return array rules. |
344
|
|
|
*/ |
345
|
21 |
|
public function getDescriptionRules() |
346
|
|
|
{ |
347
|
21 |
|
$rules = []; |
348
|
21 |
|
if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) { |
349
|
9 |
|
$rules[] = [ |
350
|
9 |
|
[$this->descriptionAttribute], |
351
|
|
|
'string' |
352
|
9 |
|
]; |
353
|
9 |
|
$rules[] = [ |
354
|
9 |
|
[$this->descriptionAttribute], |
355
|
9 |
|
'default', |
356
|
9 |
|
'value' => $this->initDescription, |
357
|
|
|
]; |
358
|
9 |
|
} |
359
|
21 |
|
return $rules; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get the rules associated with `content` and `contentType` attributes. |
364
|
|
|
* @return array rules. |
365
|
|
|
*/ |
366
|
21 |
|
public function getContentRules() |
367
|
|
|
{ |
368
|
21 |
|
if (!$this->contentAttribute) { |
369
|
|
|
return []; |
370
|
|
|
} |
371
|
21 |
|
$rules = []; |
372
|
21 |
|
$rules[] = [$this->contentAttribute, 'required']; |
373
|
21 |
|
if ($this->contentAttributeRule) { |
374
|
21 |
|
if (is_string($this->contentAttributeRule)) { |
375
|
|
|
$this->contentAttributeRule = [$this->contentAttributeRule]; |
376
|
|
|
} |
377
|
21 |
|
if (is_array($this->contentAttributeRule)) { |
378
|
21 |
|
$rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule); |
379
|
21 |
|
} |
380
|
21 |
|
} |
381
|
|
|
|
382
|
21 |
|
if (!$this->contentTypeAttribute) { |
383
|
12 |
|
return $rules; |
384
|
|
|
} |
385
|
|
|
|
386
|
9 |
|
if (is_array($this->contentTypes) && !empty($this->contentTypes)) { |
387
|
9 |
|
$rules[] = [[ |
388
|
9 |
|
$this->contentTypeAttribute], |
389
|
9 |
|
'required']; |
390
|
9 |
|
$rules[] = [[ |
391
|
9 |
|
$this->contentTypeAttribute], |
392
|
9 |
|
'in', |
393
|
9 |
|
'range' => array_keys($this->contentTypes)]; |
394
|
9 |
|
} |
395
|
9 |
|
return $rules; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Set blameable rules. |
400
|
|
|
* @param array $rules |
401
|
|
|
*/ |
402
|
21 |
|
protected function setBlameableRules($rules = []) |
403
|
|
|
{ |
404
|
21 |
|
$this->blameableLocalRules = $rules; |
405
|
21 |
|
$cache = $this->getCache(); |
|
|
|
|
406
|
21 |
|
if ($cache) { |
407
|
21 |
|
$tagDependency = new TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]); |
408
|
21 |
|
$cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency); |
409
|
21 |
|
} |
410
|
21 |
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Get blameable behaviors cache key. |
414
|
|
|
* @return string cache key. |
415
|
|
|
*/ |
416
|
21 |
|
public function getBlameableBehaviorsCacheKey() |
417
|
|
|
{ |
418
|
21 |
|
return static::class . $this->cachePrefix . static::$cacheKeyBlameableBehaviors; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Get blameable behaviors cache tag. |
423
|
|
|
* @return string cache tag. |
424
|
|
|
*/ |
425
|
21 |
|
public function getBlameableBehaviorsCacheTag() |
426
|
|
|
{ |
427
|
21 |
|
return static::class . $this->cachePrefix . static::$cacheTagBlameableBehaviors; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Get blameable behaviors. If current behaviors array is empty, the init |
432
|
|
|
* array will be given. |
433
|
|
|
* @return array |
434
|
|
|
*/ |
435
|
21 |
|
public function getBlameableBehaviors() |
436
|
|
|
{ |
437
|
21 |
|
$cache = $this->getCache(); |
|
|
|
|
438
|
21 |
|
if ($cache) { |
439
|
21 |
|
$this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey()); |
440
|
21 |
|
} |
441
|
21 |
|
if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) { |
442
|
21 |
|
if ($cache) { |
443
|
21 |
|
TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]); |
|
|
|
|
444
|
21 |
|
} |
445
|
21 |
|
$behaviors = parent::behaviors(); |
|
|
|
|
446
|
21 |
|
$behaviors['blameable'] = [ |
447
|
21 |
|
'class' => BlameableBehavior::class, |
448
|
21 |
|
'createdByAttribute' => $this->createdByAttribute, |
449
|
21 |
|
'updatedByAttribute' => $this->updatedByAttribute, |
450
|
21 |
|
'value' => [$this, |
451
|
21 |
|
'onGetCurrentUserGuid'], |
452
|
|
|
]; |
453
|
21 |
|
$this->setBlameableBehaviors($behaviors); |
454
|
21 |
|
} |
455
|
21 |
|
return $this->blameableLocalBehaviors; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Set blameable behaviors. |
460
|
|
|
* @param array $behaviors |
461
|
|
|
*/ |
462
|
21 |
|
protected function setBlameableBehaviors($behaviors = []) |
463
|
|
|
{ |
464
|
21 |
|
$this->blameableLocalBehaviors = $behaviors; |
465
|
21 |
|
$cache = $this->getCache(); |
|
|
|
|
466
|
21 |
|
if ($cache) { |
467
|
21 |
|
$tagDependencyConfig = ['tags' => [$this->getBlameableBehaviorsCacheTag()]]; |
468
|
21 |
|
$tagDependency = new TagDependency($tagDependencyConfig); |
469
|
21 |
|
$cache->set($this->getBlameableBehaviorsCacheKey(), $behaviors, 0, $tagDependency); |
470
|
21 |
|
} |
471
|
21 |
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Set description. |
475
|
|
|
* @return string description. |
476
|
|
|
*/ |
477
|
|
|
public function getDescription() |
478
|
|
|
{ |
479
|
|
|
$descAttribute = $this->descriptionAttribute; |
480
|
|
|
return is_string($descAttribute) ? $this->$descAttribute : null; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Get description. |
485
|
|
|
* @param string $desc description. |
486
|
|
|
* @return string|null description if enabled, or null if disabled. |
487
|
|
|
*/ |
488
|
|
|
public function setDescription($desc) |
489
|
|
|
{ |
490
|
|
|
$descAttribute = $this->descriptionAttribute; |
491
|
|
|
return is_string($descAttribute) ? $this->$descAttribute = $desc : null; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Get blame who owned this blameable model. |
496
|
|
|
* NOTICE! This method will not check whether `$userClass` exists. You should |
497
|
|
|
* specify it in `init()` method. |
498
|
|
|
* @return BaseUserQuery user. |
499
|
|
|
*/ |
500
|
6 |
|
public function getUser() |
501
|
|
|
{ |
502
|
6 |
|
$userClass = $this->userClass; |
503
|
6 |
|
$model = $userClass::buildNoInitModel(); |
504
|
|
|
/* @var $model BaseUserModel */ |
505
|
6 |
|
return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->createdByAttribute]); |
|
|
|
|
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* |
510
|
|
|
* @param BaseUserModel|string $user |
511
|
|
|
* @return boolean |
512
|
|
|
*/ |
513
|
21 |
|
public function setUser($user) |
514
|
|
|
{ |
515
|
21 |
|
if ($user instanceof $this->userClass || $user instanceof \yii\web\IdentityInterface) { |
516
|
21 |
|
return $this->{$this->createdByAttribute} = $user->getGUID(); |
517
|
|
|
} |
518
|
|
|
if (is_string($user) && preg_match(Number::GUID_REGEX, $user)) { |
519
|
|
|
return $this->{$this->createdByAttribute} = Number::guid_bin($user); |
520
|
|
|
} |
521
|
|
|
if (strlen($user) == 16) { |
522
|
|
|
return $this->{$this->createdByAttribute} = $user; |
523
|
|
|
} |
524
|
|
|
return false; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Get updater who updated this blameable model recently. |
529
|
|
|
* NOTICE! This method will not check whether `$userClass` exists. You should |
530
|
|
|
* specify it in `init()` method. |
531
|
|
|
* @return BaseUserQuery user. |
532
|
|
|
*/ |
533
|
1 |
|
public function getUpdater() |
534
|
|
|
{ |
535
|
1 |
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
536
|
|
|
return null; |
537
|
|
|
} |
538
|
1 |
|
$userClass = $this->userClass; |
539
|
1 |
|
$model = $userClass::buildNoInitModel(); |
540
|
|
|
/* @var $model BaseUserModel */ |
541
|
1 |
|
return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->updatedByAttribute]); |
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* |
546
|
|
|
* @param BaseUserModel|string $user |
547
|
|
|
* @return boolean |
548
|
|
|
*/ |
549
|
|
|
public function setUpdater($user) |
550
|
|
|
{ |
551
|
|
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
552
|
|
|
return false; |
553
|
|
|
} |
554
|
|
|
if ($user instanceof $this->userClass || $user instanceof \yii\web\IdentityInterface) { |
555
|
|
|
return $this->{$this->updatedByAttribute} = $user->getGUID(); |
556
|
|
|
} |
557
|
|
|
if (is_string($user) && preg_match(Number::GUID_REGEX, $user)) { |
558
|
|
|
return $this->{$this->updatedByAttribute} = Number::guid_bin($user); |
559
|
|
|
} |
560
|
|
|
if (strlen($user) == 16) { |
561
|
|
|
return $this->{$this->updatedByAttribute} = $user; |
562
|
|
|
} |
563
|
|
|
return false; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* This event is triggered before the model update. |
568
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
569
|
|
|
* override or modify it directly, unless you know the consequences. |
570
|
|
|
* @param ModelEvent $event |
571
|
|
|
*/ |
572
|
2 |
|
public function onContentChanged($event) |
573
|
|
|
{ |
574
|
2 |
|
$sender = $event->sender; |
575
|
|
|
/* @var $sender static */ |
576
|
2 |
|
return $sender->resetConfirmation(); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Return the current user's GUID if current model doesn't specify the owner |
581
|
|
|
* yet, or return the owner's GUID if current model has been specified. |
582
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
583
|
|
|
* override or modify it directly, unless you know the consequences. |
584
|
|
|
* @param ModelEvent $event |
585
|
|
|
* @return string the GUID of current user or the owner. |
586
|
|
|
*/ |
587
|
21 |
|
public function onGetCurrentUserGuid($event) |
588
|
|
|
{ |
589
|
21 |
|
$sender = $event->sender; |
590
|
|
|
/* @var $sender static */ |
591
|
21 |
|
if (isset($sender->attributes[$sender->createdByAttribute])) { |
592
|
21 |
|
return $sender->attributes[$sender->createdByAttribute]; |
|
|
|
|
593
|
|
|
} |
594
|
|
|
$identity = \Yii::$app->user->identity; |
595
|
|
|
/* @var $identity BaseUserModel */ |
596
|
|
|
if ($identity) { |
597
|
|
|
return $identity->getGUID(); |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Initialize type of content. the first of element[index is 0] of |
603
|
|
|
* $contentTypes will be used. |
604
|
|
|
* @param ModelEvent $event |
605
|
|
|
*/ |
606
|
9 |
|
public function onInitContentType($event) |
607
|
|
|
{ |
608
|
9 |
|
$sender = $event->sender; |
609
|
|
|
/* @var $sender static */ |
610
|
9 |
|
if (!is_string($sender->contentTypeAttribute) || empty($sender->contentTypeAttribute)) { |
611
|
|
|
return; |
612
|
|
|
} |
613
|
9 |
|
$contentTypeAttribute = $sender->contentTypeAttribute; |
614
|
9 |
|
if (!isset($sender->$contentTypeAttribute) && |
615
|
9 |
|
!empty($sender->contentTypes) && |
616
|
9 |
|
is_array($sender->contentTypes)) { |
617
|
9 |
|
$sender->$contentTypeAttribute = $sender->contentTypes[0]; |
618
|
9 |
|
} |
619
|
9 |
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Initialize description property with $initDescription. |
623
|
|
|
* @param ModelEvent $event |
624
|
|
|
*/ |
625
|
9 |
|
public function onInitDescription($event) |
626
|
|
|
{ |
627
|
9 |
|
$sender = $event->sender; |
628
|
|
|
/* @var $sender static */ |
629
|
9 |
|
if (!is_string($sender->descriptionAttribute) || empty($sender->descriptionAttribute)) { |
630
|
|
|
return; |
631
|
|
|
} |
632
|
9 |
|
$descriptionAttribute = $sender->descriptionAttribute; |
633
|
9 |
|
if (empty($sender->$descriptionAttribute)) { |
634
|
9 |
|
$sender->$descriptionAttribute = $sender->initDescription; |
635
|
9 |
|
} |
636
|
9 |
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Attach events associated with blameable model. |
640
|
|
|
*/ |
641
|
21 |
|
public function initBlameableEvents() |
642
|
|
|
{ |
643
|
21 |
|
$this->on(static::$eventConfirmationChanged, [$this, "onConfirmationChanged"]); |
|
|
|
|
644
|
21 |
|
$this->on(static::$eventNewRecordCreated, [$this, "onInitConfirmation"]); |
|
|
|
|
645
|
21 |
|
$contentTypeAttribute = $this->contentTypeAttribute; |
646
|
21 |
|
if (is_string($contentTypeAttribute) && !empty($contentTypeAttribute) && !isset($this->$contentTypeAttribute)) { |
647
|
9 |
|
$this->on(static::$eventNewRecordCreated, [$this, "onInitContentType"]); |
|
|
|
|
648
|
9 |
|
} |
649
|
21 |
|
$descriptionAttribute = $this->descriptionAttribute; |
650
|
21 |
|
if (is_string($descriptionAttribute) && !empty($descriptionAttribute) && !isset($this->$descriptionAttribute)) { |
651
|
9 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitDescription']); |
|
|
|
|
652
|
9 |
|
} |
653
|
21 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, "onContentChanged"]); |
|
|
|
|
654
|
21 |
|
$this->initSelfBlameableEvents(); |
655
|
21 |
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* @inheritdoc |
659
|
|
|
*/ |
660
|
2 |
|
public function enabledFields() |
661
|
|
|
{ |
662
|
2 |
|
$fields = parent::enabledFields(); |
663
|
2 |
|
if (is_string($this->createdByAttribute) && !empty($this->createdByAttribute)) { |
664
|
2 |
|
$fields[] = $this->createdByAttribute; |
665
|
2 |
|
} |
666
|
2 |
|
if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute) && |
667
|
2 |
|
$this->createdByAttribute != $this->updatedByAttribute) { |
668
|
|
|
$fields[] = $this->updatedByAttribute; |
669
|
|
|
} |
670
|
2 |
|
if (is_string($this->contentAttribute)) { |
671
|
2 |
|
$fields[] = $this->contentAttribute; |
672
|
2 |
|
} |
673
|
2 |
|
if (is_array($this->contentAttribute)) { |
674
|
|
|
$fields = array_merge($fields, $this->contentAttribute); |
675
|
|
|
} |
676
|
2 |
|
if (is_string($this->descriptionAttribute)) { |
677
|
1 |
|
$fields[] = $this->descriptionAttribute; |
678
|
1 |
|
} |
679
|
2 |
|
if (is_string($this->confirmationAttribute)) { |
680
|
1 |
|
$fields[] = $this->confirmationAttribute; |
681
|
1 |
|
} |
682
|
2 |
|
if (is_string($this->parentAttribute)) { |
683
|
|
|
$fields[] = $this->parentAttribute; |
684
|
|
|
} |
685
|
2 |
|
return $fields; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Find all follows by specified identity. If `$identity` is null, the logged-in |
690
|
|
|
* identity will be taken. |
691
|
|
|
* @param string|integer $pageSize If it is 'all`, then will find all follows, |
692
|
|
|
* the `$currentPage` parameter will be skipped. If it is integer, it will be |
693
|
|
|
* regarded as sum of models in one page. |
694
|
|
|
* @param integer $currentPage The current page number, begun with 0. |
695
|
|
|
* @param {$this->userClass} $identity |
|
|
|
|
696
|
|
|
* @return static[] If no follows, null will be given, or return follow array. |
697
|
|
|
*/ |
698
|
1 |
|
public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
699
|
|
|
{ |
700
|
1 |
|
if ($pageSize === 'all') { |
701
|
1 |
|
return static::findByIdentity($identity)->all(); |
702
|
|
|
} |
703
|
1 |
|
return static::findByIdentity($identity)->page($pageSize, $currentPage)->all(); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* Find one follow by specified identity. If `$identity` is null, the logged-in |
708
|
|
|
* identity will be taken. If $identity doesn't has the follower, null will |
709
|
|
|
* be given. |
710
|
|
|
* @param integer $id user id. |
711
|
|
|
* @param boolean $throwException |
712
|
|
|
* @param {$this->userClass} $identity |
|
|
|
|
713
|
|
|
* @return static |
714
|
|
|
* @throws InvalidParamException |
715
|
|
|
*/ |
716
|
1 |
|
public static function findOneById($id, $throwException = true, $identity = null) |
717
|
|
|
{ |
718
|
1 |
|
$query = static::findByIdentity($identity); |
719
|
1 |
|
if (!empty($id)) { |
720
|
1 |
|
$query = $query->id($id); |
721
|
1 |
|
} |
722
|
1 |
|
$model = $query->one(); |
723
|
1 |
|
if (!$model && $throwException) { |
724
|
1 |
|
throw new InvalidParamException('Model Not Found.'); |
725
|
|
|
} |
726
|
1 |
|
return $model; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Get total of follows of specified identity. |
731
|
|
|
* @param {$this->userClass} $identity |
|
|
|
|
732
|
|
|
* @return integer total. |
733
|
|
|
*/ |
734
|
1 |
|
public static function countByIdentity($identity = null) |
735
|
|
|
{ |
736
|
1 |
|
return (int)(static::findByIdentity($identity)->count()); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* Get pagination, used for building contents page by page. |
741
|
|
|
* @param integer $limit |
742
|
|
|
* @param {$this->userClass} $identity |
|
|
|
|
743
|
|
|
* @return Pagination |
744
|
|
|
*/ |
745
|
|
|
public static function getPagination($limit = 10, $identity = null) |
746
|
|
|
{ |
747
|
|
|
$limit = (int) $limit; |
748
|
|
|
$count = static::countByIdentity($identity); |
749
|
|
|
if ($limit > $count) { |
750
|
|
|
$limit = $count; |
751
|
|
|
} |
752
|
|
|
return new Pagination(['totalCount' => $count, 'pageSize' => $limit]); |
753
|
|
|
} |
754
|
|
|
} |
755
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: