b82db04a
Yarik
test
|
1
2
3
|
<?php
namespace common\modules\comment\models;
|
83b0052c
Yarik
test
|
4
|
use common\models\User;
|
4ed1f788
Yarik
test
|
5
|
use yii\db\ActiveQuery;
|
0c0cdc9d
Yarik
test
|
6
|
use yii\helpers\Url;
|
4ed1f788
Yarik
test
|
7
|
|
b82db04a
Yarik
test
|
8
9
|
/**
* Class Comment
|
2d107e9e
Yarik
test
|
10
11
|
* @property bool $guestComment
* @property integer $comment_id
|
2fd40ee7
Yarik
test
|
12
13
14
15
16
17
18
19
20
21
22
|
* @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
|
23
24
|
* @property Rating $rating
* @property User $user
|
8a551494
Yarik
test
|
25
|
* @property User $author
|
b82db04a
Yarik
test
|
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
* @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
|
40
41
42
43
44
|
/**
* @var bool
*/
public $guestComment = true;
|
0c0cdc9d
Yarik
test
|
45
46
|
public $buttons = [ ];
|
b82db04a
Yarik
test
|
47
48
49
50
|
public function rules()
{
return [
[
|
2d107e9e
Yarik
test
|
51
52
53
54
55
|
[
'text',
'user_name',
'user_email',
],
|
b82db04a
Yarik
test
|
56
57
58
|
'required',
],
[
|
2d107e9e
Yarik
test
|
59
60
61
62
63
64
65
|
[
'rating',
],
'safe',
],
[
[ 'user_email' ],
|
b82db04a
Yarik
test
|
66
67
68
|
'email',
],
[
|
2d107e9e
Yarik
test
|
69
|
[ 'user_name' ],
|
b82db04a
Yarik
test
|
70
71
|
'string',
],
|
2d107e9e
Yarik
test
|
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
[
[
'comment_id',
'comment_pid',
],
'integer',
],
[
[ 'status' ],
'default',
'value' => 1,
],
[
[ 'comment_pid' ],
'exist',
'targetAttribute' => 'comment_id',
'filter' => [
|
2fd40ee7
Yarik
test
|
89
90
|
'model' => $this->model,
'model_id' => $this->model_id,
|
2d107e9e
Yarik
test
|
91
92
|
],
],
|
b82db04a
Yarik
test
|
93
94
95
96
97
98
|
];
}
public function scenarios()
{
return [
|
2d107e9e
Yarik
test
|
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
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
|
124
125
126
|
];
}
|
83b0052c
Yarik
test
|
127
128
129
130
131
132
133
134
135
136
137
138
139
|
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);
}
|
fdc1c9de
Yarik
test
|
140
141
142
|
/**
* @inheritdoc
*/
|
b82db04a
Yarik
test
|
143
144
145
146
147
148
149
150
151
152
153
|
public static function tableName()
{
return '{{%comment}}';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
f52b6809
Yarik
Commit
|
154
|
'text' => \Yii::t('app', 'Мнение'),
|
2d107e9e
Yarik
test
|
155
|
'user_name' => \Yii::t('app', 'Имя'),
|
b82db04a
Yarik
test
|
156
157
158
159
|
'user_email' => \Yii::t('app', 'Email'),
];
}
|
2d107e9e
Yarik
test
|
160
161
162
163
164
165
|
public function getGuestComment()
{
return $this->guestComment;
}
public function setGuestComment($value)
|
b82db04a
Yarik
test
|
166
|
{
|
2d107e9e
Yarik
test
|
167
|
$this->guestComment = $value;
|
b82db04a
Yarik
test
|
168
169
|
}
|
4ed1f788
Yarik
test
|
170
|
/**
|
2fd40ee7
Yarik
test
|
171
172
|
* @param string $model
* @param integer $model_id
|
4ed1f788
Yarik
test
|
173
174
175
|
*
* @return ActiveQuery
*/
|
b5becf98
Yarik
test
|
176
|
public static function getComments($model, $model_id)
|
b82db04a
Yarik
test
|
177
|
{
|
b5becf98
Yarik
test
|
178
|
return self::find()
|
8eab7c9f
Yarik
test
|
179
180
181
182
183
184
|
->where([
'comment.model' => $model,
'comment.model_id' => $model_id,
'comment.status' => 1,
])
->with('rating');
|
b82db04a
Yarik
test
|
185
186
|
}
|
2d107e9e
Yarik
test
|
187
|
public function postComment()
|
b82db04a
Yarik
test
|
188
|
{
|
2d107e9e
Yarik
test
|
189
|
if($this->checkCreate()) {
|
8eab7c9f
Yarik
test
|
190
191
192
193
|
if(!empty($this->comment_pid) && !$this->checkReply()) {
$this->addError('comment_id', 'You can`t reply to this message');
return false;
}
|
2d107e9e
Yarik
test
|
194
195
196
197
198
199
|
if($this->insert()) {
$this->clearSafe();
return true;
} else {
return false;
}
|
b82db04a
Yarik
test
|
200
|
}
|
8eab7c9f
Yarik
test
|
201
202
|
$this->addError('comment_id', 'You can`t post comment here');
return false;
|
b82db04a
Yarik
test
|
203
204
|
}
|
2d107e9e
Yarik
test
|
205
|
public function updateComment()
|
b82db04a
Yarik
test
|
206
|
{
|
2d107e9e
Yarik
test
|
207
208
209
210
211
|
if($this->checkUpdate()) {
if(empty( $this->comment_id )) {
$this->addError('comment_id', 'Comment ID not found');
return false;
} else {
|
282b6e56
Yarik
Commit
|
212
|
if($this->update() !== false) {
|
8a551494
Yarik
test
|
213
|
// $this->clearSafe(); Clears safe attributes after AJAX update
|
2d107e9e
Yarik
test
|
214
215
216
217
218
219
220
221
222
|
return true;
} else {
return false;
}
}
} else {
$this->addError('comment_id', 'You can`t update this post');
return false;
}
|
b82db04a
Yarik
test
|
223
224
|
}
|
2d107e9e
Yarik
test
|
225
|
public function deleteComment()
|
b82db04a
Yarik
test
|
226
|
{
|
2d107e9e
Yarik
test
|
227
228
229
230
231
|
if($this->checkDelete()) {
if(empty( $this->comment_id )) {
$this->addError('comment_id', 'Comment ID not found');
return false;
} else {
|
0c0cdc9d
Yarik
test
|
232
233
234
235
236
237
238
|
if($this->user_id == \Yii::$app->user->id) {
if($this->delete()) {
return true;
} else {
$this->addError('comment_id', 'Can\'t delete post.');
return false;
}
|
2d107e9e
Yarik
test
|
239
|
} else {
|
0c0cdc9d
Yarik
test
|
240
241
242
243
244
245
246
247
248
249
|
if($this->status == self::STATUS_DELETED) {
return false;
}
$this->status = self::STATUS_DELETED;
if($this->update()) {
$this->clearSafe();
return true;
} else {
return false;
}
|
2d107e9e
Yarik
test
|
250
251
252
253
254
255
|
}
}
} else {
$this->addError('comment_id', 'You can`t delete this post');
return false;
}
|
b82db04a
Yarik
test
|
256
257
|
}
|
2d107e9e
Yarik
test
|
258
|
public function checkCreate()
|
b82db04a
Yarik
test
|
259
|
{
|
2d107e9e
Yarik
test
|
260
|
if($this->getGuestComment()) {
|
b82db04a
Yarik
test
|
261
262
|
return true;
} else {
|
2fd40ee7
Yarik
test
|
263
|
return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [
|
b5becf98
Yarik
test
|
264
265
266
|
'model' => $this->model,
'model_id' => $this->model_id,
'comment_model' => $this,
|
2fd40ee7
Yarik
test
|
267
|
]);
|
b82db04a
Yarik
test
|
268
269
270
|
}
}
|
2d107e9e
Yarik
test
|
271
272
273
274
275
|
public function checkUpdate()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
|
2fd40ee7
Yarik
test
|
276
277
278
|
return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [
'model' => $this->model,
'model_id' => $this->model_id,
|
8a551494
Yarik
test
|
279
|
'comment' => $this,
|
2fd40ee7
Yarik
test
|
280
281
282
|
]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [
'model' => $this->model,
'model_id' => $this->model_id,
|
8a551494
Yarik
test
|
283
|
'comment' => $this,
|
2fd40ee7
Yarik
test
|
284
|
]);
|
2d107e9e
Yarik
test
|
285
286
287
288
289
290
291
292
|
}
}
public function checkDelete()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
|
8a551494
Yarik
test
|
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
return ( \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE, [
'model' => $this->model,
'model_id' => $this->model_id,
'comment' => $this,
]) || \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE_OWN, [
'model' => $this->model,
'model_id' => $this->model_id,
'comment' => $this,
]) );
}
}
public function checkReply()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
return $this->allowReply;
|
2d107e9e
Yarik
test
|
311
312
313
314
315
|
}
}
protected function clearSafe($setNew = true)
{
|
b82db04a
Yarik
test
|
316
317
318
319
320
321
322
323
|
$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
|
324
325
326
327
328
|
public function getParent()
{
return $this->hasOne(self::className(), [ 'comment_id' => 'comment_pid' ]);
}
|
8a551494
Yarik
test
|
329
330
331
332
333
334
335
336
337
|
public function getAuthorName()
{
if(!empty( $this->author )) {
return $this->author->name;
} else {
return $this->user_name;
}
}
|
25bd78f8
Yarik
test
|
338
|
public function getAuthor($guestMark = '')
|
2d107e9e
Yarik
test
|
339
|
{
|
b5becf98
Yarik
test
|
340
|
if(!empty( $this->user )) {
|
25bd78f8
Yarik
test
|
341
342
343
|
return $this->user->name;
} else {
$name = $this->user_name;
|
b5becf98
Yarik
test
|
344
|
if(!empty( $guestMark )) {
|
25bd78f8
Yarik
test
|
345
346
347
348
|
$name .= $guestMark;
}
return $name;
}
|
2d107e9e
Yarik
test
|
349
350
351
352
|
}
public function checkRating()
{
|
2fd40ee7
Yarik
test
|
353
354
355
356
357
358
|
$rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([
'model' => $this->className(),
])
|
2d107e9e
Yarik
test
|
359
|
->one();
|
0c0cdc9d
Yarik
test
|
360
|
if(!$rating instanceof \common\modules\comment\models\Rating && !empty( $this->primaryKey )) {
|
2d107e9e
Yarik
test
|
361
|
$rating = new \common\modules\comment\models\Rating([
|
2fd40ee7
Yarik
test
|
362
363
364
|
'model' => $this->className(),
'model_id' => $this->comment_id,
'user_id' => $this->user_id,
|
2d107e9e
Yarik
test
|
365
366
367
368
369
370
371
372
|
]);
$rating->save();
}
}
public function getRating()
{
$this->checkRating();
|
2fd40ee7
Yarik
test
|
373
374
375
376
|
return $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([ 'model' => $this->className() ]);
|
2d107e9e
Yarik
test
|
377
378
379
380
|
}
public function hasRating($return = true)
{
|
2fd40ee7
Yarik
test
|
381
382
383
384
|
$rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [
'model_id' => 'comment_id',
])
->andWhere([ 'model' => $this->className() ])
|
2d107e9e
Yarik
test
|
385
386
387
388
389
390
391
392
393
394
395
396
|
->andWhere([
'not',
[ 'value' => NULL ],
])
->one();
if($return) {
return $rating;
} else {
return $rating ? true : false;
}
}
|
83b0052c
Yarik
test
|
397
398
399
400
401
|
public function getUser()
{
return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
}
|
b5becf98
Yarik
test
|
402
403
404
405
406
407
408
|
public function buildButtons(
$buttons = [
'delete',
'update',
'reply',
]
) {
|
25bd78f8
Yarik
test
|
409
410
411
412
413
414
415
|
if(in_array('delete', $buttons)) {
if($this->checkDelete()) {
$this->buttons[ 'delete' ] = Url::to([
'artbox-comment/delete',
'comment_id' => $this->comment_id,
]);
}
|
0c0cdc9d
Yarik
test
|
416
|
}
|
25bd78f8
Yarik
test
|
417
418
419
420
421
422
423
|
if(in_array('update', $buttons)) {
if($this->checkUpdate()) {
$this->buttons[ 'update' ] = Url::to([
'artbox-comment/update',
'comment_id' => $this->comment_id,
]);
}
|
8a551494
Yarik
test
|
424
|
}
|
25bd78f8
Yarik
test
|
425
426
427
428
429
430
431
|
if(in_array('reply', $buttons)) {
if($this->checkReply()) {
$this->buttons[ 'reply' ] = Url::to([
'artbox-comment/reply',
'comment_id' => $this->comment_id,
]);
}
|
8a551494
Yarik
test
|
432
433
434
435
436
437
|
}
}
public function getAllowReply()
{
return $this->hasAttribute('comment_pid');
|
0c0cdc9d
Yarik
test
|
438
439
|
}
|
2d107e9e
Yarik
test
|
440
|
}
|