|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Comment.php |
|
4
|
|
|
* @author Revin Roman |
|
5
|
|
|
* @link https://rmrevin.ru |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace rmrevin\yii\module\Comments\models; |
|
9
|
|
|
|
|
10
|
|
|
use rmrevin\yii\module\Comments; |
|
11
|
|
|
use yii\behaviors\BlameableBehavior; |
|
12
|
|
|
use yii\behaviors\TimestampBehavior; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Comment |
|
16
|
|
|
* @package rmrevin\yii\module\Comments\models |
|
17
|
|
|
* |
|
18
|
|
|
* @property integer $id |
|
19
|
|
|
* @property string $entity |
|
20
|
|
|
* @property string $from |
|
21
|
|
|
* @property string $text |
|
22
|
|
|
* @property integer $deleted |
|
23
|
|
|
* @property integer $created_by |
|
24
|
|
|
* @property integer $updated_by |
|
25
|
|
|
* @property integer $created_at |
|
26
|
|
|
* @property integer $updated_at |
|
27
|
|
|
* |
|
28
|
|
|
* @property \yii\db\ActiveRecord $author |
|
29
|
|
|
* @property \yii\db\ActiveRecord $lastUpdateAuthor |
|
30
|
|
|
* |
|
31
|
|
|
* @method queries\CommentQuery hasMany(string $class, array $link) see BaseActiveRecord::hasMany() for more info |
|
32
|
|
|
* @method queries\CommentQuery hasOne(string $class, array $link) see BaseActiveRecord::hasOne() for more info |
|
33
|
|
|
*/ |
|
34
|
|
|
class Comment extends \yii\db\ActiveRecord |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function behaviors() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
'blameable' => BlameableBehavior::className(), |
|
44
|
|
|
'timestamp' => TimestampBehavior::className(), |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function rules() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
[['text'], 'required'], |
|
55
|
|
|
[['from', 'text'], 'string'], |
|
56
|
|
|
[['created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'], |
|
57
|
|
|
[['deleted'], 'boolean'], |
|
58
|
|
|
[['deleted'], 'default', 'value' => self::NOT_DELETED], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function attributeLabels() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'id' => \Yii::t('app', 'ID'), |
|
69
|
|
|
'entity' => \Yii::t('app', 'Entity'), |
|
70
|
|
|
'from' => \Yii::t('app', 'Comment author'), |
|
71
|
|
|
'text' => \Yii::t('app', 'Text'), |
|
72
|
|
|
'created_by' => \Yii::t('app', 'Created by'), |
|
73
|
|
|
'updated_by' => \Yii::t('app', 'Updated by'), |
|
74
|
|
|
'created_at' => \Yii::t('app', 'Created at'), |
|
75
|
|
|
'updated_at' => \Yii::t('app', 'Updated at'), |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function isEdited() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->created_at !== $this->updated_at; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isDeleted() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->deleted === self::DELETED; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function canCreate() |
|
99
|
|
|
{ |
|
100
|
|
|
return Comments\Module::instance()->useRbac === true |
|
101
|
|
|
? \Yii::$app->getUser()->can(Comments\Permission::CREATE) |
|
|
|
|
|
|
102
|
|
|
: true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function canUpdate() |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$User = \Yii::$app->getUser(); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return Comments\Module::instance()->useRbac === true |
|
113
|
|
|
? (\Yii::$app->getUser()->can(Comments\Permission::UPDATE) || \Yii::$app->getUser()->can(Comments\Permission::UPDATE_OWN, ['Comment' => $this])) |
|
114
|
|
|
: ($User->isGuest ? false : ($this->created_by === $User->id)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
View Code Duplication |
public function canDelete() |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
$User = \Yii::$app->getUser(); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
return Comments\Module::instance()->useRbac === true |
|
125
|
|
|
? (\Yii::$app->getUser()->can(Comments\Permission::DELETE) || \Yii::$app->getUser()->can(Comments\Permission::DELETE_OWN, ['Comment' => $this])) |
|
126
|
|
|
: ($User->isGuest ? false : ($this->created_by === $User->id)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return queries\CommentQuery |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getAuthor() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'created_by']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return queries\CommentQuery |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getLastUpdateAuthor() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->hasOne(Comments\Module::instance()->userIdentityClass, ['id' => 'updated_by']); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return queries\CommentQuery |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function find() |
|
149
|
|
|
{ |
|
150
|
|
|
return \Yii::createObject( |
|
151
|
|
|
Comments\Module::instance()->model('commentQuery'), |
|
152
|
|
|
[get_called_class()] |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritdoc |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function tableName() |
|
160
|
|
|
{ |
|
161
|
|
|
return '{{%comment}}'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
const NOT_DELETED = 0; |
|
165
|
|
|
const DELETED = 1; |
|
166
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: