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
|
|
|
/** |
16
|
|
|
* This trait is used for building blameable query class for blameable model, |
17
|
|
|
* which would be attached three conditions. |
18
|
|
|
* For example: |
19
|
|
|
* ```php |
20
|
|
|
* class BlameableQuery { |
21
|
|
|
* use BlameableQueryTrait; |
22
|
|
|
* } |
23
|
|
|
* ``` |
24
|
|
|
* |
25
|
|
|
* @version 2.0 |
26
|
|
|
* @author vistart <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait BlameableQueryTrait |
29
|
|
|
{ |
30
|
|
|
use QueryTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Specify confirmation. |
34
|
|
|
* @param boolean $isConfirmed |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function confirmed($isConfirmed = true) |
38
|
|
|
{ |
39
|
|
|
$model = $this->noInitModel; |
|
|
|
|
40
|
|
|
if (!is_string($model->confirmationAttribute)) { |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
return $this->andWhere([$model->confirmationAttribute => $isConfirmed]); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Specify content. |
48
|
|
|
* @param mixed $content |
49
|
|
|
* @param false|string $like false, 'like', 'or like', 'not like', 'or not like'. |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
3 |
|
public function content($content, $like = false) |
53
|
|
|
{ |
54
|
3 |
|
$model = $this->noInitModel; |
55
|
3 |
|
return $this->likeCondition($content, $model->contentAttribute, $like); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Specify parent. |
60
|
|
|
* @param array|string $guid parent guid or array of them. non-parent if |
61
|
|
|
* empty. If you don't want to specify parent, please do not access this |
62
|
|
|
* method. |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function parentGuid($guid) |
66
|
|
|
{ |
67
|
|
|
$model = $this->noInitModel; |
68
|
|
|
if (!is_string($model->parentAttribute)) { |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
if (empty($guid)) { |
72
|
|
|
return $this->andWhere([$model->parentAttribute => '']); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
return $this->andWhere([$model->parentAttribute => $guid]); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Specify creator(s). |
79
|
|
|
* @param string|array $guid |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
17 |
|
public function createdBy($guid) |
83
|
|
|
{ |
84
|
17 |
|
$model = $this->noInitModel; |
85
|
17 |
|
if (!is_string($model->createdByAttribute)) { |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
17 |
|
if ($guid instanceof \vistart\Models\models\BaseUserModel) { |
89
|
8 |
|
$guid = $guid->guid; |
90
|
8 |
|
} |
91
|
17 |
|
return $this->andWhere([$model->createdByAttribute => $guid]); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Specify last updater(s). |
96
|
|
|
* @param string|array $guid |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function updatedBy($guid) |
100
|
|
|
{ |
101
|
|
|
$model = $this->noInitModel; |
102
|
|
|
if (!is_string($model->updatedByAttribute)) { |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
if ($guid instanceof \vistart\Models\models\BaseUserModel) { |
106
|
|
|
$guid = $guid->guid; |
107
|
|
|
} |
108
|
|
|
return $this->andWhere([$model->updatedByAttribute => $guid]); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Attach current identity to createdBy condition. |
113
|
|
|
* @param \vistart\Models\models\BaseUserModel $identity |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
9 |
|
public function byIdentity($identity = null) |
117
|
|
|
{ |
118
|
9 |
|
if (!$identity) { |
119
|
|
|
$identity = \Yii::$app->user->identity; |
120
|
|
|
} |
121
|
9 |
|
if (!$identity || !$identity->canGetProperty('guid')) { |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
9 |
|
return $this->createdBy($identity->guid); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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: