Blame view

common/modules/comment/models/CommentModel.php 9.08 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
  <?php
      namespace common\modules\comment\models;
      
d8c1a2e0   Yarik   Big commit artbox
4
5
6
      use common\behaviors\RatingBehavior;
      use common\modules\comment\behaviors\ParentBehavior;
      use common\modules\comment\models\interfaces\CommentInterface;
d8c1a2e0   Yarik   Big commit artbox
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;
d8c1a2e0   Yarik   Big commit artbox
12
13
14
      
      /**
       * Class CommentModel
8af13427   Yarik   For leha commit.
15
       *
d8c1a2e0   Yarik   Big commit artbox
16
17
18
19
20
       * @property int    $artbox_comment_id
       * @property string $text
       * @property int    $user_id
       * @property string $username
       * @property string $email
8af13427   Yarik   For leha commit.
21
22
23
       * @property int    $created_at
       * @property int    $updated_at
       * @property int    $deleted_at
d8c1a2e0   Yarik   Big commit artbox
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
       * @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
       * @package common\modules\comment\models
       */
      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;
          
5c2eb7c8   Yarik   Big commit almost...
45
46
          public $entityId;
          
d8c1a2e0   Yarik   Big commit artbox
47
48
          public function scenarios()
          {
5c2eb7c8   Yarik   Big commit almost...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
              $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',
              ];
d8c1a2e0   Yarik   Big commit artbox
65
66
              return $scenarios;
          }
5c2eb7c8   Yarik   Big commit almost...
67
          
d8c1a2e0   Yarik   Big commit artbox
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
102
103
104
105
106
          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',
                  ],
                  [
5c2eb7c8   Yarik   Big commit almost...
107
108
109
110
                      [
                          'entity_id',
                          'artbox_comment_pid',
                      ],
d8c1a2e0   Yarik   Big commit artbox
111
112
113
114
115
116
117
118
                      'integer',
                  ],
                  [
                      [ 'status' ],
                      'default',
                      'value' => 0,
                  ],
                  [
5c2eb7c8   Yarik   Big commit almost...
119
                      [ 'artbox_comment_pid' ],
d8c1a2e0   Yarik   Big commit artbox
120
121
                      'exist',
                      'targetAttribute' => 'artbox_comment_id',
5c2eb7c8   Yarik   Big commit almost...
122
                      'skipOnError'     => true,
d8c1a2e0   Yarik   Big commit artbox
123
124
125
126
127
128
129
130
                  ],
              ];
          }
          
          public function behaviors()
          {
              return [
                  [
8af13427   Yarik   For leha commit.
131
                      'class' => TimestampBehavior::className(),
d8c1a2e0   Yarik   Big commit artbox
132
133
134
135
136
137
138
139
140
141
142
                  ],
                  [
                      'class'              => BlameableBehavior::className(),
                      'createdByAttribute' => 'user_id',
                      'updatedByAttribute' => false,
                  ],
                  [
                      'class'      => AttributeBehavior::className(),
                      'attributes' => [
                          ActiveRecord::EVENT_BEFORE_INSERT => 'ip',
                      ],
8af13427   Yarik   For leha commit.
143
                      'value'      => function ($event) {
d8c1a2e0   Yarik   Big commit artbox
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
                          return \Yii::$app->request->userIP;
                      },
                  ],
                  [
                      'class' => ParentBehavior::className(),
                  ],
                  [
                      'class' => RatingBehavior::className(),
                  ],
                  /* Notification: uncomment to enable notifications.
                  [
                      'class' => NotifyBehavior::className(),
                  ],
                  */
              ];
          }
          
          public function attributeLabels()
          {
              return [
5c2eb7c8   Yarik   Big commit almost...
164
165
166
167
168
169
                  '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'),
8af13427   Yarik   For leha commit.
170
171
                  'updated_at'         => \Yii::t('artbox-comment', 'Date update'),
                  'deleted_at'         => \Yii::t('artbox-comment', 'Date delete'),
5c2eb7c8   Yarik   Big commit almost...
172
                  'status'             => \Yii::t('artbox-comment', 'Status'),
d8c1a2e0   Yarik   Big commit artbox
173
                  'artbox_comment_pid' => \Yii::t('artbox-comment', 'Comment parent'),
5c2eb7c8   Yarik   Big commit almost...
174
175
176
177
178
                  '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'),
d8c1a2e0   Yarik   Big commit artbox
179
180
              ];
          }
5c2eb7c8   Yarik   Big commit almost...
181
          
8af13427   Yarik   For leha commit.
182
          public function setEntity(string $entity)
d8c1a2e0   Yarik   Big commit artbox
183
184
185
186
          {
              $this->entity = $entity;
          }
          
8af13427   Yarik   For leha commit.
187
          public function getEntity(): string
d8c1a2e0   Yarik   Big commit artbox
188
189
190
191
          {
              return $this->entity;
          }
          
8af13427   Yarik   For leha commit.
192
          public static function getTree(string $entity, int $entityId): ActiveDataProvider
d8c1a2e0   Yarik   Big commit artbox
193
          {
8af13427   Yarik   For leha commit.
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
              return new ActiveDataProvider(
                  [
                      'query'      => self::find()
                                          ->with(
                                              [
                                                  'children',
                                                  'user',
                                                  'children.user',
                                              ]
                                          )
                                          ->where(
                                              [
                                                  'entity'             => $entity,
                                                  'entity_id'          => $entityId,
                                                  'status'             => 1,
                                                  'artbox_comment_pid' => null,
                                              ]
                                          ),
                      'pagination' => [
                          'pageSize' => 20,
d8c1a2e0   Yarik   Big commit artbox
214
                      ],
8af13427   Yarik   For leha commit.
215
216
217
218
219
220
221
                      'sort'       => [
                          'defaultOrder' => [
                              'created_at' => SORT_DESC,
                          ],
                      ],
                  ]
              );
d8c1a2e0   Yarik   Big commit artbox
222
223
          }
          
8af13427   Yarik   For leha commit.
224
          public function deleteComment(): bool
5c2eb7c8   Yarik   Big commit almost...
225
          {
8af13427   Yarik   For leha commit.
226
227
              if (\Yii::$app->user->id != null && \Yii::$app->user->id == $this->user_id) {
                  if ($this->delete()) {
d8c1a2e0   Yarik   Big commit artbox
228
229
230
231
232
233
                      return true;
                  }
              }
              return false;
          }
          
8af13427   Yarik   For leha commit.
234
          public function setEntityId(int $entityId)
d8c1a2e0   Yarik   Big commit artbox
235
236
237
238
          {
              $this->entityId = $entityId;
          }
          
8af13427   Yarik   For leha commit.
239
          public function getEntityId(): int
d8c1a2e0   Yarik   Big commit artbox
240
241
242
243
          {
              return $this->entityId;
          }
          
8af13427   Yarik   For leha commit.
244
          public function getChildren()
d8c1a2e0   Yarik   Big commit artbox
245
246
          {
              return $this->hasMany(self::className(), [ 'artbox_comment_pid' => 'artbox_comment_id' ])
5c2eb7c8   Yarik   Big commit almost...
247
                          ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ])
d8c1a2e0   Yarik   Big commit artbox
248
249
250
                          ->inverseOf('parent');
          }
          
8af13427   Yarik   For leha commit.
251
          public function getParent()
d8c1a2e0   Yarik   Big commit artbox
252
253
254
255
256
          {
              return $this->hasOne(self::className(), [ 'artbox_comment_id' => 'artbox_comment_pid' ])
                          ->inverseOf('children');
          }
          
8af13427   Yarik   For leha commit.
257
          public function getUser()
d8c1a2e0   Yarik   Big commit artbox
258
259
260
261
262
          {
              $module = \Yii::$app->getModule('artbox-comment');
              return $this->hasOne($module->userIdentityClass, [ 'id' => 'user_id' ]);
          }
          
8af13427   Yarik   For leha commit.
263
          public function getRating()
d8c1a2e0   Yarik   Big commit artbox
264
          {
5c2eb7c8   Yarik   Big commit almost...
265
              return $this->hasOne(RatingModel::className(), [ 'model_id' => 'artbox_comment_id' ])
8af13427   Yarik   For leha commit.
266
267
268
269
270
271
272
                          ->andWhere(
                              [
                                  'or',
                                  [ 'artbox_comment_rating.model' => null ],
                                  [ 'artbox_comment_rating.model' => self::className() ],
                              ]
                          );
d8c1a2e0   Yarik   Big commit artbox
273
274
          }
      }