|
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
|
36 |
|
public function rules() |
|
167
|
|
|
{ |
|
168
|
36 |
|
return $this->getBlameableRules(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @inheritdoc |
|
173
|
|
|
*/ |
|
174
|
36 |
|
public function behaviors() |
|
175
|
|
|
{ |
|
176
|
36 |
|
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
|
19 |
|
public function setContent($content) |
|
214
|
|
|
{ |
|
215
|
19 |
|
$contentAttribute = $this->contentAttribute; |
|
216
|
19 |
|
if ($contentAttribute === false) { |
|
217
|
|
|
return; |
|
218
|
|
|
} |
|
219
|
19 |
|
if (is_array($contentAttribute)) { |
|
220
|
|
|
foreach ($contentAttribute as $key => $value) { |
|
221
|
|
|
$this->$value = $content[$key]; |
|
222
|
|
|
} |
|
223
|
|
|
return; |
|
224
|
|
|
} |
|
225
|
19 |
|
$this->$contentAttribute = $content; |
|
226
|
19 |
|
} |
|
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
|
36 |
|
public function getBlameableRulesCacheKey() |
|
261
|
|
|
{ |
|
262
|
36 |
|
return static::class . $this->cachePrefix . static::$cacheKeyBlameableRules; |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Get blameable rules cache tag. |
|
267
|
|
|
* @return string cache tag |
|
268
|
|
|
*/ |
|
269
|
36 |
|
public function getBlameableRulesCacheTag() |
|
270
|
|
|
{ |
|
271
|
36 |
|
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
|
36 |
|
public function getBlameableRules() |
|
279
|
|
|
{ |
|
280
|
36 |
|
$cache = $this->getCache(); |
|
|
|
|
|
|
281
|
36 |
|
if ($cache) { |
|
282
|
36 |
|
$this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey()); |
|
283
|
|
|
} |
|
284
|
|
|
// 若当前规则不为空,且是数组,则认为是规则数组,直接返回。 |
|
285
|
36 |
|
if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) { |
|
286
|
12 |
|
return $this->blameableLocalRules; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
// 父类规则与确认规则合并。 |
|
290
|
36 |
|
if ($cache) { |
|
291
|
36 |
|
TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]); |
|
|
|
|
|
|
292
|
|
|
} |
|
293
|
36 |
|
$rules = array_merge( |
|
294
|
36 |
|
parent::rules(), |
|
|
|
|
|
|
295
|
36 |
|
$this->getConfirmationRules(), |
|
296
|
36 |
|
$this->getBlameableAttributeRules(), |
|
297
|
36 |
|
$this->getDescriptionRules(), |
|
298
|
36 |
|
$this->getContentRules(), |
|
299
|
36 |
|
$this->getSelfBlameableRules() |
|
300
|
|
|
); |
|
301
|
36 |
|
$this->setBlameableRules($rules); |
|
302
|
36 |
|
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
|
36 |
|
public function getBlameableAttributeRules() |
|
311
|
|
|
{ |
|
312
|
36 |
|
$rules = []; |
|
313
|
|
|
// 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。 |
|
314
|
36 |
|
if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) { |
|
315
|
|
|
throw new NotSupportedException('You must assign the creator.'); |
|
316
|
|
|
} |
|
317
|
36 |
|
$rules[] = [ |
|
318
|
36 |
|
[$this->createdByAttribute], |
|
319
|
36 |
|
'safe', |
|
320
|
|
|
]; |
|
321
|
|
|
|
|
322
|
36 |
|
if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) { |
|
323
|
26 |
|
$rules[] = [ |
|
324
|
26 |
|
[$this->updatedByAttribute], |
|
325
|
26 |
|
'safe', |
|
326
|
|
|
]; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
36 |
|
if ($this->idCreatorCombinatedUnique && is_string($this->idAttribute)) { |
|
|
|
|
|
|
330
|
36 |
|
$rules ['id'] = [ |
|
331
|
36 |
|
[$this->idAttribute, |
|
|
|
|
|
|
332
|
36 |
|
$this->createdByAttribute], |
|
333
|
36 |
|
'unique', |
|
334
|
36 |
|
'targetAttribute' => [$this->idAttribute, |
|
|
|
|
|
|
335
|
36 |
|
$this->createdByAttribute], |
|
336
|
|
|
]; |
|
337
|
|
|
} |
|
338
|
36 |
|
return $rules; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Get the rules associated with `description` attribute. |
|
343
|
|
|
* @return array rules. |
|
344
|
|
|
*/ |
|
345
|
36 |
|
public function getDescriptionRules() |
|
346
|
|
|
{ |
|
347
|
36 |
|
$rules = []; |
|
348
|
36 |
|
if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) { |
|
349
|
10 |
|
$rules[] = [ |
|
350
|
10 |
|
[$this->descriptionAttribute], |
|
351
|
10 |
|
'string' |
|
352
|
|
|
]; |
|
353
|
10 |
|
$rules[] = [ |
|
354
|
10 |
|
[$this->descriptionAttribute], |
|
355
|
10 |
|
'default', |
|
356
|
10 |
|
'value' => $this->initDescription, |
|
357
|
|
|
]; |
|
358
|
|
|
} |
|
359
|
36 |
|
return $rules; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Get the rules associated with `content` and `contentType` attributes. |
|
364
|
|
|
* @return array rules. |
|
365
|
|
|
*/ |
|
366
|
36 |
|
public function getContentRules() |
|
367
|
|
|
{ |
|
368
|
36 |
|
if (!$this->contentAttribute) { |
|
369
|
|
|
return []; |
|
370
|
|
|
} |
|
371
|
36 |
|
$rules = []; |
|
372
|
36 |
|
$rules[] = [$this->contentAttribute, 'required']; |
|
373
|
36 |
|
if ($this->contentAttributeRule) { |
|
374
|
36 |
|
if (is_string($this->contentAttributeRule)) { |
|
375
|
|
|
$this->contentAttributeRule = [$this->contentAttributeRule]; |
|
376
|
|
|
} |
|
377
|
36 |
|
if (is_array($this->contentAttributeRule)) { |
|
378
|
36 |
|
$rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule); |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
36 |
|
if (!$this->contentTypeAttribute) { |
|
383
|
26 |
|
return $rules; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
10 |
|
if (is_array($this->contentTypes) && !empty($this->contentTypes)) { |
|
387
|
10 |
|
$rules[] = [[ |
|
388
|
10 |
|
$this->contentTypeAttribute], |
|
389
|
10 |
|
'required']; |
|
390
|
10 |
|
$rules[] = [[ |
|
391
|
10 |
|
$this->contentTypeAttribute], |
|
392
|
10 |
|
'in', |
|
393
|
10 |
|
'range' => array_keys($this->contentTypes)]; |
|
394
|
|
|
} |
|
395
|
10 |
|
return $rules; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Set blameable rules. |
|
400
|
|
|
* @param array $rules |
|
401
|
|
|
*/ |
|
402
|
36 |
|
protected function setBlameableRules($rules = []) |
|
403
|
|
|
{ |
|
404
|
36 |
|
$this->blameableLocalRules = $rules; |
|
405
|
36 |
|
$cache = $this->getCache(); |
|
|
|
|
|
|
406
|
36 |
|
if ($cache) { |
|
407
|
36 |
|
$tagDependency = new TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]); |
|
408
|
36 |
|
$cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency); |
|
409
|
|
|
} |
|
410
|
36 |
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Get blameable behaviors cache key. |
|
414
|
|
|
* @return string cache key. |
|
415
|
|
|
*/ |
|
416
|
36 |
|
public function getBlameableBehaviorsCacheKey() |
|
417
|
|
|
{ |
|
418
|
36 |
|
return static::class . $this->cachePrefix . static::$cacheKeyBlameableBehaviors; |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Get blameable behaviors cache tag. |
|
423
|
|
|
* @return string cache tag. |
|
424
|
|
|
*/ |
|
425
|
36 |
|
public function getBlameableBehaviorsCacheTag() |
|
426
|
|
|
{ |
|
427
|
36 |
|
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
|
36 |
|
public function getBlameableBehaviors() |
|
436
|
|
|
{ |
|
437
|
36 |
|
$cache = $this->getCache(); |
|
|
|
|
|
|
438
|
36 |
|
if ($cache) { |
|
439
|
36 |
|
$this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey()); |
|
440
|
|
|
} |
|
441
|
36 |
|
if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) { |
|
442
|
36 |
|
if ($cache) { |
|
443
|
36 |
|
TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]); |
|
|
|
|
|
|
444
|
|
|
} |
|
445
|
36 |
|
$behaviors = parent::behaviors(); |
|
|
|
|
|
|
446
|
36 |
|
$behaviors['blameable'] = [ |
|
447
|
36 |
|
'class' => BlameableBehavior::class, |
|
448
|
36 |
|
'createdByAttribute' => $this->createdByAttribute, |
|
449
|
36 |
|
'updatedByAttribute' => $this->updatedByAttribute, |
|
450
|
36 |
|
'value' => [$this, |
|
451
|
36 |
|
'onGetCurrentUserGuid'], |
|
452
|
|
|
]; |
|
453
|
36 |
|
$this->setBlameableBehaviors($behaviors); |
|
454
|
|
|
} |
|
455
|
36 |
|
return $this->blameableLocalBehaviors; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Set blameable behaviors. |
|
460
|
|
|
* @param array $behaviors |
|
461
|
|
|
*/ |
|
462
|
36 |
|
protected function setBlameableBehaviors($behaviors = []) |
|
463
|
|
|
{ |
|
464
|
36 |
|
$this->blameableLocalBehaviors = $behaviors; |
|
465
|
36 |
|
$cache = $this->getCache(); |
|
|
|
|
|
|
466
|
36 |
|
if ($cache) { |
|
467
|
36 |
|
$tagDependencyConfig = ['tags' => [$this->getBlameableBehaviorsCacheTag()]]; |
|
468
|
36 |
|
$tagDependency = new TagDependency($tagDependencyConfig); |
|
469
|
36 |
|
$cache->set($this->getBlameableBehaviorsCacheKey(), $behaviors, 0, $tagDependency); |
|
470
|
|
|
} |
|
471
|
36 |
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* Set description. |
|
475
|
|
|
* @return string description. |
|
476
|
|
|
*/ |
|
477
|
1 |
|
public function getDescription() |
|
478
|
|
|
{ |
|
479
|
1 |
|
$descAttribute = $this->descriptionAttribute; |
|
480
|
1 |
|
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
|
1 |
|
public function setDescription($desc) |
|
489
|
|
|
{ |
|
490
|
1 |
|
$descAttribute = $this->descriptionAttribute; |
|
491
|
1 |
|
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
|
24 |
|
public function setUser($user) |
|
514
|
|
|
{ |
|
515
|
24 |
|
if ($user instanceof $this->userClass || $user instanceof \yii\web\IdentityInterface) { |
|
516
|
24 |
|
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
|
24 |
|
public function onGetCurrentUserGuid($event) |
|
588
|
|
|
{ |
|
589
|
24 |
|
$sender = $event->sender; |
|
590
|
|
|
/* @var $sender static */ |
|
591
|
24 |
|
if (isset($sender->attributes[$sender->createdByAttribute])) { |
|
592
|
24 |
|
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
|
10 |
|
public function onInitContentType($event) |
|
607
|
|
|
{ |
|
608
|
10 |
|
$sender = $event->sender; |
|
609
|
|
|
/* @var $sender static */ |
|
610
|
10 |
|
if (!is_string($sender->contentTypeAttribute) || empty($sender->contentTypeAttribute)) { |
|
611
|
|
|
return; |
|
612
|
|
|
} |
|
613
|
10 |
|
$contentTypeAttribute = $sender->contentTypeAttribute; |
|
614
|
10 |
|
if (!isset($sender->$contentTypeAttribute) && |
|
615
|
10 |
|
!empty($sender->contentTypes) && |
|
616
|
10 |
|
is_array($sender->contentTypes)) { |
|
617
|
10 |
|
$sender->$contentTypeAttribute = $sender->contentTypes[0]; |
|
618
|
|
|
} |
|
619
|
10 |
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Initialize description property with $initDescription. |
|
623
|
|
|
* @param ModelEvent $event |
|
624
|
|
|
*/ |
|
625
|
10 |
|
public function onInitDescription($event) |
|
626
|
|
|
{ |
|
627
|
10 |
|
$sender = $event->sender; |
|
628
|
|
|
/* @var $sender static */ |
|
629
|
10 |
|
if (!is_string($sender->descriptionAttribute) || empty($sender->descriptionAttribute)) { |
|
630
|
|
|
return; |
|
631
|
|
|
} |
|
632
|
10 |
|
$descriptionAttribute = $sender->descriptionAttribute; |
|
633
|
10 |
|
if (empty($sender->$descriptionAttribute)) { |
|
634
|
10 |
|
$sender->$descriptionAttribute = $sender->initDescription; |
|
635
|
|
|
} |
|
636
|
10 |
|
} |
|
637
|
|
|
|
|
638
|
|
|
/** |
|
639
|
|
|
* Attach events associated with blameable model. |
|
640
|
|
|
*/ |
|
641
|
36 |
|
public function initBlameableEvents() |
|
642
|
|
|
{ |
|
643
|
36 |
|
$this->on(static::$eventConfirmationChanged, [$this, "onConfirmationChanged"]); |
|
|
|
|
|
|
644
|
36 |
|
$this->on(static::$eventNewRecordCreated, [$this, "onInitConfirmation"]); |
|
|
|
|
|
|
645
|
36 |
|
$contentTypeAttribute = $this->contentTypeAttribute; |
|
646
|
36 |
|
if (is_string($contentTypeAttribute) && !empty($contentTypeAttribute) && !isset($this->$contentTypeAttribute)) { |
|
647
|
10 |
|
$this->on(static::$eventNewRecordCreated, [$this, "onInitContentType"]); |
|
|
|
|
|
|
648
|
|
|
} |
|
649
|
36 |
|
$descriptionAttribute = $this->descriptionAttribute; |
|
650
|
36 |
|
if (is_string($descriptionAttribute) && !empty($descriptionAttribute) && !isset($this->$descriptionAttribute)) { |
|
651
|
10 |
|
$this->on(static::$eventNewRecordCreated, [$this, 'onInitDescription']); |
|
|
|
|
|
|
652
|
|
|
} |
|
653
|
36 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, "onContentChanged"]); |
|
|
|
|
|
|
654
|
36 |
|
$this->initSelfBlameableEvents(); |
|
655
|
36 |
|
} |
|
656
|
|
|
|
|
657
|
|
|
/** |
|
658
|
|
|
* @inheritdoc |
|
659
|
|
|
*/ |
|
660
|
16 |
|
public function enabledFields() |
|
661
|
|
|
{ |
|
662
|
16 |
|
$fields = parent::enabledFields(); |
|
663
|
16 |
|
if (is_string($this->createdByAttribute) && !empty($this->createdByAttribute)) { |
|
664
|
16 |
|
$fields[] = $this->createdByAttribute; |
|
665
|
|
|
} |
|
666
|
16 |
|
if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute) && |
|
667
|
16 |
|
$this->createdByAttribute != $this->updatedByAttribute) { |
|
668
|
|
|
$fields[] = $this->updatedByAttribute; |
|
669
|
|
|
} |
|
670
|
16 |
|
if (is_string($this->contentAttribute)) { |
|
671
|
16 |
|
$fields[] = $this->contentAttribute; |
|
672
|
|
|
} |
|
673
|
16 |
|
if (is_array($this->contentAttribute)) { |
|
674
|
|
|
$fields = array_merge($fields, $this->contentAttribute); |
|
675
|
|
|
} |
|
676
|
16 |
|
if (is_string($this->descriptionAttribute)) { |
|
677
|
1 |
|
$fields[] = $this->descriptionAttribute; |
|
678
|
|
|
} |
|
679
|
16 |
|
if (is_string($this->confirmationAttribute)) { |
|
680
|
1 |
|
$fields[] = $this->confirmationAttribute; |
|
681
|
|
|
} |
|
682
|
16 |
|
if (is_string($this->parentAttribute)) { |
|
683
|
|
|
$fields[] = $this->parentAttribute; |
|
684
|
|
|
} |
|
685
|
16 |
|
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
|
|
|
} |
|
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: