Blame view

common/modules/comment/models/CommentModel.php 8.74 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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
      
      /**
       * Class CommentModel
       * @property int    $artbox_comment_id
       * @property string $text
       * @property int    $user_id
       * @property string $username
       * @property string $email
       * @property int    $date_add
       * @property int    $date_update
       * @property int    $date_delete
       * @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...
44
45
          public $entityId;
          
d8c1a2e0   Yarik   Big commit artbox
46
47
          public function scenarios()
          {
5c2eb7c8   Yarik   Big commit almost...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
              $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
64
65
              return $scenarios;
          }
5c2eb7c8   Yarik   Big commit almost...
66
          
d8c1a2e0   Yarik   Big commit artbox
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
102
103
104
105
          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...
106
107
108
109
                      [
                          'entity_id',
                          'artbox_comment_pid',
                      ],
d8c1a2e0   Yarik   Big commit artbox
110
111
112
113
114
115
116
117
                      'integer',
                  ],
                  [
                      [ 'status' ],
                      'default',
                      'value' => 0,
                  ],
                  [
5c2eb7c8   Yarik   Big commit almost...
118
                      [ 'artbox_comment_pid' ],
d8c1a2e0   Yarik   Big commit artbox
119
120
                      'exist',
                      'targetAttribute' => 'artbox_comment_id',
5c2eb7c8   Yarik   Big commit almost...
121
                      'skipOnError'     => true,
d8c1a2e0   Yarik   Big commit artbox
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                  ],
              ];
          }
          
          public function behaviors()
          {
              return [
                  [
                      'class'              => TimestampBehavior::className(),
                      'createdAtAttribute' => 'date_add',
                      'updatedAtAttribute' => 'date_update',
                  ],
                  [
                      'class'              => BlameableBehavior::className(),
                      'createdByAttribute' => 'user_id',
                      'updatedByAttribute' => false,
                  ],
                  [
                      'class'      => AttributeBehavior::className(),
                      'attributes' => [
                          ActiveRecord::EVENT_BEFORE_INSERT => 'ip',
                      ],
                      'value'      => function($event) {
                          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...
165
166
167
168
169
170
171
172
173
                  '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'),
                  'date_update'        => \Yii::t('artbox-comment', 'Date update'),
                  'date_delete'        => \Yii::t('artbox-comment', 'Date delete'),
                  'status'             => \Yii::t('artbox-comment', 'Status'),
d8c1a2e0   Yarik   Big commit artbox
174
                  'artbox_comment_pid' => \Yii::t('artbox-comment', 'Comment parent'),
5c2eb7c8   Yarik   Big commit almost...
175
176
177
178
179
                  '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
180
181
              ];
          }
5c2eb7c8   Yarik   Big commit almost...
182
          
d8c1a2e0   Yarik   Big commit artbox
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
          function setEntity(string $entity)
          {
              $this->entity = $entity;
          }
          
          function getEntity(): string
          {
              return $this->entity;
          }
          
          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,
                                          'artbox_comment_pid' => NULL,
                                      ]),
                  'pagination' => [
                      'pageSize' => 20,
                  ],
                  'sort'       => [
                      'defaultOrder' => [
                          'date_add' => SORT_DESC,
                      ],
                  ],
              ]);
          }
          
5c2eb7c8   Yarik   Big commit almost...
219
220
          function deleteComment(): bool
          {
d8c1a2e0   Yarik   Big commit artbox
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
              if(\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) {
                  if($this->delete()) {
                      return true;
                  }
              }
              return false;
          }
          
          function setEntityId(int $entityId)
          {
              $this->entityId = $entityId;
          }
          
          function getEntityId(): int
          {
              return $this->entityId;
          }
          
          function getChildren()
          {
              return $this->hasMany(self::className(), [ 'artbox_comment_pid' => 'artbox_comment_id' ])
5c2eb7c8   Yarik   Big commit almost...
242
                          ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ])
d8c1a2e0   Yarik   Big commit artbox
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
                          ->inverseOf('parent');
          }
          
          function getParent()
          {
              return $this->hasOne(self::className(), [ 'artbox_comment_id' => 'artbox_comment_pid' ])
                          ->inverseOf('children');
          }
          
          function getUser()
          {
              $module = \Yii::$app->getModule('artbox-comment');
              return $this->hasOne($module->userIdentityClass, [ 'id' => 'user_id' ]);
          }
          
          function getRating()
          {
5c2eb7c8   Yarik   Big commit almost...
260
261
262
263
264
265
              return $this->hasOne(RatingModel::className(), [ 'model_id' => 'artbox_comment_id' ])
                          ->andWhere([
                              'or',
                              [ 'artbox_comment_rating.model' => NULL ],
                              [ 'artbox_comment_rating.model' => self::className() ],
                          ]);
d8c1a2e0   Yarik   Big commit artbox
266
267
          }
      }