2261f70a
Yarik
test
|
1
2
3
4
5
|
<?php
namespace common\modules\comment\models;
use common\models\Currency;
use common\models\File;
|
32ed90fd
Yarik
test
|
6
|
use common\models\Project;
|
be662d1f
Yarik
test
|
7
|
use common\models\User;
|
2261f70a
Yarik
test
|
8
|
use yii\db\ActiveQuery;
|
be662d1f
Yarik
test
|
9
|
use yii\db\ActiveRecord;
|
2261f70a
Yarik
test
|
10
11
12
13
|
use yii\web\UploadedFile;
/**
* Class Comment
|
be662d1f
Yarik
test
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
* @property bool $guestComment
* @property integer $comment_id
* @property string $text
* @property int $user_id
* @property int $status
* @property string $date_add
* @property string $date_update
* @property string $date_delete
* @property string $model
* @property int $model_id
* @property string $files
* @property float $budget_from
* @property float $budget_to
* @property int $term_from
* @property int $term_to
|
38a6e1dd
Yarik
test
|
29
|
* @property int $state
|
be662d1f
Yarik
test
|
30
|
* @property Currency $currency
|
32ed90fd
Yarik
test
|
31
|
* @property Project $project
|
2261f70a
Yarik
test
|
32
33
34
35
36
37
38
39
40
41
42
43
|
* @package common\modules\comment\models
*/
class CommentProject 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 STATUS_ANONYMOUS = 4;
|
38a6e1dd
Yarik
test
|
44
45
46
47
48
49
|
const STATE_NEW = 1;
const STATE_CANDIDATE = 2;
const STATE_PERFORMER = 3;
const STATE_DENY = 4;
const STATE_TRASH = 5;
|
2261f70a
Yarik
test
|
50
51
|
const SCENARIO_USER = 'user';
const SCENARIO_GUEST = 'guest';
|
38a6e1dd
Yarik
test
|
52
53
|
const SCENARIO_STATE = 'state';
const SCENARIO_OWNER = 'owner';
|
2261f70a
Yarik
test
|
54
55
56
57
58
|
/**
* @var bool
*/
public $guestComment = false;
|
be662d1f
Yarik
test
|
59
|
|
2261f70a
Yarik
test
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
public $file;
public function rules()
{
return [
[
[
'text',
'budget_from',
'budget_to',
'term_from',
'term_to',
'budget_currency',
],
'required',
],
[
[
'budget_currency',
],
'integer',
],
[
[
'budget_from',
'budget_to',
'term_from',
'term_to',
],
'integer',
'min' => 0,
],
[
[
'budget_currency',
],
'default',
'value' => 3,
],
[
[ 'budget_currency' ],
'exist',
|
be662d1f
Yarik
test
|
102
|
'targetClass' => Currency::className(),
|
2261f70a
Yarik
test
|
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
'targetAttribute' => 'currency_id',
],
[
[
'files',
],
'string',
],
[
[
'file',
],
'safe',
],
[
[ 'status' ],
'default',
'value' => 1,
],
|
38a6e1dd
Yarik
test
|
122
123
124
125
126
|
[
[ 'state' ],
'integer',
'max' => 4,
'min' => 1,
|
32ed90fd
Yarik
test
|
127
|
'on' => self::SCENARIO_STATE,
|
38a6e1dd
Yarik
test
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
],
[
[ 'state' ],
'required',
'on' => self::SCENARIO_STATE,
],
[
[ 'state' ],
'required',
'on' => self::SCENARIO_OWNER,
],
[
[ 'state' ],
'in',
|
32ed90fd
Yarik
test
|
142
143
144
145
146
|
'range' => [
1,
5,
],
'on' => self::SCENARIO_OWNER,
|
38a6e1dd
Yarik
test
|
147
|
],
|
2261f70a
Yarik
test
|
148
149
150
151
152
153
154
155
156
157
|
];
}
public function scenarios()
{
return [
self::SCENARIO_USER => [
'text',
'budget_from',
'budget_to',
|
25bd78f8
Yarik
test
|
158
|
'budget_currency',
|
2261f70a
Yarik
test
|
159
160
161
162
|
'term_from',
'term_to',
'file',
],
|
be662d1f
Yarik
test
|
163
|
self::SCENARIO_GUEST => [
|
2261f70a
Yarik
test
|
164
165
|
],
|
38a6e1dd
Yarik
test
|
166
167
168
|
self::SCENARIO_STATE => [
'state',
],
|
32ed90fd
Yarik
test
|
169
170
171
|
self::SCENARIO_OWNER => [
'state',
],
|
2261f70a
Yarik
test
|
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'date_add',
'updatedAtAttribute' => 'date_update',
'value' => new \yii\db\Expression('NOW()'),
],
];
}
public static function tableName()
{
return '{{%comment_project}}';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
be662d1f
Yarik
test
|
201
|
'text' => \Yii::t('app', 'Текст ответа'),
|
2261f70a
Yarik
test
|
202
|
'budget_from' => \Yii::t('app', 'от'),
|
be662d1f
Yarik
test
|
203
204
205
|
'budget_to' => \Yii::t('app', 'до'),
'term_from' => \Yii::t('app', 'от'),
'term_to' => \Yii::t('app', 'до'),
|
2261f70a
Yarik
test
|
206
207
208
209
210
211
212
213
|
];
}
public function getGuestComment()
{
return $this->guestComment;
}
|
be662d1f
Yarik
test
|
214
215
216
217
|
// public function setGuestComment($value)
// {
// $this->guestComment = $value;
// }
|
2261f70a
Yarik
test
|
218
219
220
221
222
223
224
|
/**
* @param string $model
* @param integer $model_id
*
* @return ActiveQuery
*/
|
b5becf98
Yarik
test
|
225
|
public static function getComments($model, $model_id)
|
2261f70a
Yarik
test
|
226
|
{
|
25bd78f8
Yarik
test
|
227
228
229
230
231
232
233
|
/**
* @var User $user
*/
$user = \Yii::$app->user->identity;
if(!empty($user)) {
$project = Project::findOne($model_id);
if(!empty($project) && $user->id == $project->user_id) {
|
b5becf98
Yarik
test
|
234
|
return self::find()
|
25bd78f8
Yarik
test
|
235
236
237
238
239
240
241
242
|
->where([
'comment_project.model' => $model,
'comment_project.model_id' => $model_id,
'comment_project.status' => [self::STATUS_ANONYMOUS, self::STATUS_ACTIVE, self::STATUS_PERSONAL],
])
->with('currency', 'user', 'user.userInfo', 'user.companyInfo', 'user.comments');
}
}
|
b5becf98
Yarik
test
|
243
|
$query = self::find()
|
25bd78f8
Yarik
test
|
244
245
246
247
248
249
250
251
252
253
254
|
->where([
'comment_project.model' => $model,
'comment_project.model_id' => $model_id,
])
->with('currency', 'user', 'user.userInfo', 'user.companyInfo', 'user.comments');
if(!empty($user)) {
$query->andWhere(['or', ['comment_project.status' => self::STATUS_ACTIVE], ['comment_project.status' => self::STATUS_ANONYMOUS, 'comment_project.user_id' => $user->id]]);
} else {
$query->andWhere(['comment_project.status' => 1]);
}
return $query;
|
2261f70a
Yarik
test
|
255
256
257
258
259
|
}
public function postComment()
{
if($this->checkCreate()) {
|
be662d1f
Yarik
test
|
260
|
if(!empty( \Yii::$app->request->post($this->formName())[ 'anonymous' ] )) {
|
2261f70a
Yarik
test
|
261
262
263
|
$this->status = self::STATUS_ANONYMOUS;
}
$this->file = UploadedFile::getInstances($this, 'file');
|
be662d1f
Yarik
test
|
264
265
266
267
268
|
if(!empty( $this->file )) {
$file_id = [ ];
if(is_array($this->file)) {
foreach($this->file as $file) {
if($file instanceof UploadedFile) {
|
2261f70a
Yarik
test
|
269
270
271
272
273
|
$file_model = new File();
$file_id[] = $file_model->saveFile($file);
}
}
} else {
|
be662d1f
Yarik
test
|
274
|
if($this->file instanceof UploadedFile) {
|
2261f70a
Yarik
test
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
$file_model = new File();
$file_id[] = $file_model->saveFile($this->file);
}
}
$this->files = json_encode($file_id);
}
if($this->insert()) {
$this->clearSafe();
return true;
} else {
return false;
}
} else {
$this->addError('comment_id', 'You can`t post comment here');
return false;
}
}
public function updateComment()
{
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;
}
}
public function deleteComment()
{
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;
}
}
public function checkCreate()
{
if($this->getGuestComment()) {
return true;
} else {
return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [
'model' => $this->model,
'model_id' => $this->model_id,
|
d0f3b99f
Yarik
test
|
345
|
'comment_model' => $this->className(),
|
2261f70a
Yarik
test
|
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
]);
}
}
public function checkUpdate()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
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,
]);
}
}
public function checkDelete()
{
if($this->scenario == self::SCENARIO_GUEST) {
return false;
} else {
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,
]);
}
}
protected function clearSafe($setNew = true)
{
$safe = $this->safeAttributes();
$count = count($safe);
$values = array_fill(0, $count, NULL);
$result = array_combine($safe, $values);
$this->setAttributes($result);
$this->setIsNewRecord($setNew);
}
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];
// }
}
|
be662d1f
Yarik
test
|
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/**
* @return ActiveQuery
*/
public function getCurrency()
{
return $this->hasOne(Currency::className(), [ 'currency_id' => 'budget_currency' ]);
}
/**
* @return File[]
*/
public function getFilesList()
{
$files = json_decode($this->files);
if(!empty( $files )) {
return File::findAll($files);
} else {
return [ ];
}
}
/**
* @return ActiveRecord
* @throws \TypeError
*/
public function getOwner()
{
$model = new $this->model();
if($model instanceof ActiveRecord) {
return $model->findOne($this->model_id);
} else {
throw new \TypeError('Model must extends Active Record Class');
}
}
|
32ed90fd
Yarik
test
|
434
435
436
437
438
|
public function getProject()
{
return $this->hasOne(Project::className(), [ 'project_id' => 'model_id' ]);
}
|
be662d1f
Yarik
test
|
439
440
441
442
443
|
/**
* @return User
*/
public function getUser()
{
|
38a6e1dd
Yarik
test
|
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
}
public function changeState()
{
if($this->isAttributeChanged('state')) {
if($this->save()) {
return true;
} else {
return false;
}
} else {
return true;
}
|
be662d1f
Yarik
test
|
458
459
|
}
|
2261f70a
Yarik
test
|
460
|
}
|