b82db04a
Yarik
test
|
1
2
3
|
<?php
namespace common\modules\comment\models;
|
83b0052c
Yarik
test
|
4
|
use common\models\User;
|
4ed1f788
Yarik
test
|
5
6
|
use yii\db\ActiveQuery;
|
b82db04a
Yarik
test
|
7
8
|
/**
* Class Comment
|
2d107e9e
Yarik
test
|
9
10
|
* @property bool $guestComment
* @property integer $comment_id
|
2fd40ee7
Yarik
test
|
11
12
13
14
15
16
17
18
19
20
21
|
* @property string $text
* @property int $user_id
* @property string $user_name
* @property string $user_email
* @property int $comment_pid
* @property int $status
* @property string $date_add
* @property string $date_update
* @property string $date_delete
* @property string $model
* @property int $model_id
|
83b0052c
Yarik
test
|
22
23
|
* @property Rating $rating
* @property User $user
|
b82db04a
Yarik
test
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
* @package common\modules\comment\models
*/
class Comment extends \yii\db\ActiveRecord
implements \common\modules\comment\interfaces\CommentInterface
{
const STATUS_HIDDEN = 0;
const STATUS_DELETED = 2;
const STATUS_ACTIVE = 1;
const STATUS_PERSONAL = 3;
const SCENARIO_USER = 'user';
const SCENARIO_GUEST = 'guest';
|
2d107e9e
Yarik
test
|
38
39
40
41
42
|
/**
* @var bool
*/
public $guestComment = true;
|
b82db04a
Yarik
test
|
43
44
45
46
|
public function rules()
{
return [
[
|
2d107e9e
Yarik
test
|
47
48
49
50
51
|
[
'text',
'user_name',
'user_email',
],
|
b82db04a
Yarik
test
|
52
53
54
|
'required',
],
[
|
2d107e9e
Yarik
test
|
55
56
57
58
59
60
61
|
[
'rating',
],
'safe',
],
[
[ 'user_email' ],
|
b82db04a
Yarik
test
|
62
63
64
|
'email',
],
[
|
2d107e9e
Yarik
test
|
65
|
[ 'user_name' ],
|
b82db04a
Yarik
test
|
66
67
|
'string',
],
|
2d107e9e
Yarik
test
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
[
[
'comment_id',
'comment_pid',
],
'integer',
],
[
[ 'status' ],
'default',
'value' => 1,
],
[
[ 'comment_pid' ],
'exist',
'targetAttribute' => 'comment_id',
'filter' => [
|
2fd40ee7
Yarik
test
|
85
86
|
'model' => $this->model,
'model_id' => $this->model_id,
|
2d107e9e
Yarik
test
|
87
88
|
],
],
|
b82db04a
Yarik
test
|
89
90
91
92
93
94
|
];
}
public function scenarios()
{
return [
|
2d107e9e
Yarik
test
|
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
self::SCENARIO_GUEST => [
'user_name',
'user_email',
'text',
'comment_pid',
],
self::SCENARIO_USER => [
'text',
'comment_pid',
],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'date_add',
'updatedAtAttribute' => 'date_update',
'value' => new \yii\db\Expression('NOW()'),
],
|
b82db04a
Yarik
test
|
120
121
122
|
];
}
|
83b0052c
Yarik
test
|
123
124
125
126
127
128
129
130
131
132
133
134
135
|
public function afterSave($insert, $changedAttributes)
{
if($this->model == User::className()) {
if($user = User::findOne($this->model_id)) {
/**
* @var User $user
*/
$user->updateRating();
}
}
parent::afterSave($insert, $changedAttributes);
}
|
b82db04a
Yarik
test
|
136
137
138
139
140
141
142
143
144
145
146
|
public static function tableName()
{
return '{{%comment}}';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
2d107e9e
Yarik
test
|
147
148
|
'text' => \Yii::t('app', 'Комментарий'),
'user_name' => \Yii::t('app', 'Имя'),
|
b82db04a
Yarik
test
|
149
150
151
152
|
'user_email' => \Yii::t('app', 'Email'),
];
}
|
2d107e9e
Yarik
test
|
153
154
155
156
157
158
|
public function getGuestComment()
{
return $this->guestComment;
}
public function setGuestComment($value)
|
b82db04a
Yarik
test
|
159
|
{
|
2d107e9e
Yarik
test
|
160
|
$this->guestComment = $value;
|
b82db04a
Yarik
test
|
161
162
|
}
|
4ed1f788
Yarik
test
|
163
|
/**
|
2fd40ee7
Yarik
test
|
164
165
|
* @param string $model
* @param integer $model_id
|
4ed1f788
Yarik
test
|
166
167
168
|
*
* @return ActiveQuery
*/
|
2fd40ee7
Yarik
test
|
169
|
public function getComments($model, $model_id)
|
b82db04a
Yarik
test
|
170
|
{
|
2d107e9e
Yarik
test
|
171
172
|
return $this->find()
->where([
|
2fd40ee7
Yarik
test
|
173
174
175
|
'comment.model' => $model,
'comment.model_id' => $model_id,
'comment.status' => 1,
|
baba04c2
Yarik
test
|
176
|
])->with('rating');
|
b82db04a
Yarik
test
|
177
178
|
}
|
2d107e9e
Yarik
test
|
179
|
public function postComment()
|
b82db04a
Yarik
test
|
180
|
{
|
2d107e9e
Yarik
test
|
181
182
183
184
185
186
187
|
if($this->checkCreate()) {
if($this->insert()) {
$this->clearSafe();
return true;
} else {
return false;
}
|
b82db04a
Yarik
test
|
188
|
} else {
|
2d107e9e
Yarik
test
|
189
|
$this->addError('comment_id', 'You can`t post comment here');
|
b82db04a
Yarik
test
|
190
191
192
193
|
return false;
}
}
|
2d107e9e
Yarik
test
|
194
|
public function updateComment()
|
b82db04a
Yarik
test
|
195
|
{
|
2d107e9e
Yarik
test
|
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
if($this->checkUpdate()) {
if(empty( $this->comment_id )) {
$this->addError('comment_id', 'Comment ID not found');
return false;
} else {
if($this->update()) {
$this->clearSafe();
return true;
} else {
return false;
}
}
} else {
$this->addError('comment_id', 'You can`t update this post');
return false;
}
|
b82db04a
Yarik
test
|
212
213
|
}
|
2d107e9e
Yarik
test
|
214
|
public function deleteComment()
|
b82db04a
Yarik
test
|
215
|
{
|
2d107e9e
Yarik
test
|
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
if($this->checkDelete()) {
if(empty( $this->comment_id )) {
$this->addError('comment_id', 'Comment ID not found');
return false;
} else {
if($this->status == self::STATUS_DELETED) {
return false;
}
$this->status = self::STATUS_DELETED;
if($this->update()) {
$this->clearSafe();
return true;
} else {
return false;
}
}
} else {
$this->addError('comment_id', 'You can`t delete this post');
return false;
}
|
b82db04a
Yarik
test
|
236
237
|
}
|
2d107e9e
Yarik
test
|
238
|
public function checkCreate()
|
b82db04a
Yarik
test
|
239
|
{
|
2d107e9e
Yarik
test
|
240
|
if($this->getGuestComment()) {
|
b82db04a
Yarik
test
|
241
242
|
return true;
} else {
|
2fd40ee7
Yarik
test
|
243
244
245
246
|
return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [
'model' => $this->model,
'model_id' => $this->model_id,
]);
|
b82db04a
Yarik
test
|
247
248
249
|
}
}
|
2d107e9e
Yarik
test
|
250
251
252
253
254
|
public function checkUpdate()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
|
2fd40ee7
Yarik
test
|
255
256
257
258
259
260
261
|
return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [
'model' => $this->model,
'model_id' => $this->model_id,
]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [
'model' => $this->model,
'model_id' => $this->model_id,
]);
|
2d107e9e
Yarik
test
|
262
263
264
265
266
267
268
269
|
}
}
public function checkDelete()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
|
2fd40ee7
Yarik
test
|
270
271
272
273
274
275
276
|
return \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE, [
'model' => $this->model,
'model_id' => $this->model_id,
]) || \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE_OWN, [
'model' => $this->model,
'model_id' => $this->model_id,
]);
|
2d107e9e
Yarik
test
|
277
278
279
280
281
|
}
}
protected function clearSafe($setNew = true)
{
|
b82db04a
Yarik
test
|
282
283
284
285
286
287
288
289
|
$safe = $this->safeAttributes();
$count = count($safe);
$values = array_fill(0, $count, NULL);
$result = array_combine($safe, $values);
$this->setAttributes($result);
$this->setIsNewRecord($setNew);
}
|
2d107e9e
Yarik
test
|
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
public function getParent()
{
return $this->hasOne(self::className(), [ 'comment_id' => 'comment_pid' ]);
}
public function getAuthor()
{
// if($this->user_id != NULL) {
return $this->hasOne(\common\models\User::className(), [ 'id' => 'user_id' ]);
// } else {
// return ['firstname' => $this->user_name, 'email' => $this->user_email];
// }
}
public function checkRating()
{
|
2fd40ee7
Yarik
test
|
306
307
308
309
310
311
|
$rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([
'model' => $this->className(),
])
|
2d107e9e
Yarik
test
|
312
|
->one();
|
baba04c2
Yarik
test
|
313
|
if(!$rating instanceof \common\modules\comment\models\Rating && !$this->isNewRecord) {
|
2d107e9e
Yarik
test
|
314
|
$rating = new \common\modules\comment\models\Rating([
|
2fd40ee7
Yarik
test
|
315
316
317
|
'model' => $this->className(),
'model_id' => $this->comment_id,
'user_id' => $this->user_id,
|
2d107e9e
Yarik
test
|
318
319
320
321
322
323
324
325
|
]);
$rating->save();
}
}
public function getRating()
{
$this->checkRating();
|
2fd40ee7
Yarik
test
|
326
327
328
329
|
return $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([ 'model' => $this->className() ]);
|
2d107e9e
Yarik
test
|
330
331
332
333
|
}
public function hasRating($return = true)
{
|
2fd40ee7
Yarik
test
|
334
335
336
337
|
$rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([ 'model' => $this->className() ])
|
2d107e9e
Yarik
test
|
338
339
340
341
342
343
344
345
346
347
348
349
|
->andWhere([
'not',
[ 'value' => NULL ],
])
->one();
if($return) {
return $rating;
} else {
return $rating ? true : false;
}
}
|
83b0052c
Yarik
test
|
350
351
352
353
354
|
public function getUser()
{
return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
}
|
2d107e9e
Yarik
test
|
355
|
}
|