a2cde075
Yarik
first commit
|
1
2
3
|
<?php
namespace artweb\artbox\comment\models;
|
a2cde075
Yarik
first commit
|
4
5
|
use artweb\artbox\comment\behaviors\ParentBehavior;
use artweb\artbox\comment\models\interfaces\CommentInterface;
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
6
|
use artweb\artbox\ecommerce\models\Product;
|
a2cde075
Yarik
first commit
|
7
8
9
10
11
|
use yii\behaviors\AttributeBehavior;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
12
13
|
use yii\helpers\Html;
use yii\helpers\VarDumper;
|
a2cde075
Yarik
first commit
|
14
15
16
17
|
/**
* Class CommentModel
*
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
* @property int $artbox_comment_id
* @property string $text
* @property int $user_id
* @property string $username
* @property string $email
* @property int $created_at
* @property int $updated_at
* @property int $deleted_at
* @property int $status
* @property int $artbox_comment_pid
* @property int $related_id
* @property string $ip
* @property string $info
* @property string $entity
* @property int $entity_id
|
a2cde075
Yarik
first commit
|
33
|
* @package artweb\artbox\comment\models
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
34
35
|
* @property ActiveRecord $entityModel
* @property string $link
|
a2cde075
Yarik
first commit
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
*/
class CommentModel extends ActiveRecord implements CommentInterface
{
const STATUS_ACTIVE = 1;
const STATUS_HIDDEN = 0;
const STATUS_DELETED = 2;
const SCENARIO_USER = 'user';
const SCENARIO_GUEST = 'guest';
public $encryptedEntity;
public $entityId;
|
a2cde075
Yarik
first commit
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[ self::SCENARIO_USER ] = [
'text',
'entity',
'entity_id',
'artbox_comment_pid',
'status',
];
$scenarios[ self::SCENARIO_GUEST ] = [
'text',
'entity',
'entity_id',
'username',
'email',
'status',
];
return $scenarios;
}
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
71
|
|
a2cde075
Yarik
first commit
|
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
public static function tableName()
{
return '{{%artbox_comment}}';
}
public function rules()
{
return [
[
[
'text',
'entity',
'entity_id',
],
'required',
],
[
[
'username',
'email',
],
'required',
'on' => self::SCENARIO_GUEST,
],
[
[
'text',
'entity',
'username',
],
'string',
],
[
[
'email',
],
'email',
],
[
[
'entity_id',
'artbox_comment_pid',
],
'integer',
],
[
[ 'status' ],
'default',
'value' => 0,
],
[
[ 'artbox_comment_pid' ],
'exist',
'targetAttribute' => 'artbox_comment_id',
'skipOnError' => true,
],
];
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
],
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'user_id',
'updatedByAttribute' => false,
],
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'ip',
],
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
147
|
'value' => function($event) {
|
a2cde075
Yarik
first commit
|
148
149
150
151
152
153
|
return \Yii::$app->request->userIP;
},
],
[
'class' => ParentBehavior::className(),
],
|
a2cde075
Yarik
first commit
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
201
202
203
204
|
];
}
public function attributeLabels()
{
return [
'artbox_comment_id' => \Yii::t('artbox-comment', 'ID'),
'text' => \Yii::t('artbox-comment', 'Text'),
'user_id' => \Yii::t('artbox-comment', 'User'),
'username' => \Yii::t('artbox-comment', 'Username'),
'email' => 'Email',
'date_add' => \Yii::t('artbox-comment', 'Date add'),
'updated_at' => \Yii::t('artbox-comment', 'Date update'),
'deleted_at' => \Yii::t('artbox-comment', 'Date delete'),
'status' => \Yii::t('artbox-comment', 'Status'),
'artbox_comment_pid' => \Yii::t('artbox-comment', 'Comment parent'),
'related_id' => \Yii::t('artbox-comment', 'Comment related'),
'ip' => 'IP',
'entity' => \Yii::t('artbox-comment', 'Entity'),
'info' => \Yii::t('artbox-comment', 'Info'),
'entity_id' => \Yii::t('artbox-comment', 'Entity ID'),
];
}
public function setEntity(string $entity)
{
$this->entity = $entity;
}
public function getEntity(): string
{
return $this->entity;
}
public static function getTree(string $entity, int $entityId): ActiveDataProvider
{
return new ActiveDataProvider(
[
'query' => self::find()
->with(
[
'children',
'user',
'children.user',
]
)
->where(
[
'entity' => $entity,
'entity_id' => $entityId,
'status' => 1,
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
205
|
'artbox_comment_pid' => NULL,
|
a2cde075
Yarik
first commit
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
]
),
'pagination' => [
'pageSize' => 20,
],
'sort' => [
'defaultOrder' => [
'created_at' => SORT_DESC,
],
],
]
);
}
public function deleteComment(): bool
{
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
222
|
if (\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) {
|
a2cde075
Yarik
first commit
|
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
if ($this->delete()) {
return true;
}
}
return false;
}
public function setEntityId(int $entityId)
{
$this->entityId = $entityId;
}
public function getEntityId(): int
{
return $this->entityId;
}
public function getChildren()
{
return $this->hasMany(self::className(), [ 'artbox_comment_pid' => 'artbox_comment_id' ])
->andFilterWhere([ 'status' => self::STATUS_ACTIVE ])
->inverseOf('parent');
}
public function getParent()
{
return $this->hasOne(self::className(), [ 'artbox_comment_id' => 'artbox_comment_pid' ])
->inverseOf('children');
}
public function getUser()
{
$module = \Yii::$app->getModule('artbox-comment');
return $this->hasOne($module->userIdentityClass, [ 'id' => 'user_id' ]);
}
public function getRating()
{
return $this->hasOne(RatingModel::className(), [ 'model_id' => 'artbox_comment_id' ])
->andWhere(
[
'or',
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
265
|
[ 'artbox_comment_rating.model' => NULL ],
|
a2cde075
Yarik
first commit
|
266
267
268
269
|
[ 'artbox_comment_rating.model' => self::className() ],
]
);
}
|
ce440bf9
Alexey Boroda
-Comments upgrade...
|
270
271
272
273
274
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
|
public function getEntityModel()
{
$model = call_user_func_array(
[
$this->entity,
'findOne',
],
[ $this->entity_id ]
);
return $model;
}
public function getLink()
{
$model = $this->getEntityModel();
if (empty($model)) {
return Html::a('Страница не найдена', '#');
}
if (Product::className() == $this->entity) {
return Html::a(
'Перейти на товар',
\Yii::$app->urlManagerFrontend->createAbsoluteUrl(
[
'catalog/product',
'product' => $model->lang->alias,
'variant' => $model->variant->sku,
]
),
[
'target' => '_blank',
'data-pjax' => '0',
]
);
} else {
return Html::a('Неизвестная модель', '#');
}
}
|
0bacad2e
Yarik
Namespaces
|
308
309
|
}
|