Blame view

common/modules/comment/models/CommentModel.php 8.05 KB
c05bf005   Yarik   Comment added
1
2
3
  <?php
      namespace common\modules\comment\models;
      
17ca60dd   Yarik   Added comments to...
4
      use common\behaviors\RatingBehavior;
c05bf005   Yarik   Comment added
5
6
7
8
9
10
11
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
      use common\modules\comment\behaviors\ParentBehavior;
      use common\modules\comment\models\interfaces\CommentInterface;
      use common\modules\comment\Module;
      use yii\base\InvalidConfigException;
      use yii\behaviors\AttributeBehavior;
      use yii\behaviors\BlameableBehavior;
      use yii\behaviors\TimestampBehavior;
      use yii\data\ActiveDataProvider;
      use yii\db\ActiveRecord;
      use yii\helpers\Json;
      
      /**
       * 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;
          
17ca60dd   Yarik   Added comments to...
42
43
44
          const SCENARIO_USER = 'user';
          const SCENARIO_GUEST = 'guest';
          
c05bf005   Yarik   Comment added
45
46
          public $encryptedEntity;
          
17ca60dd   Yarik   Added comments to...
47
48
49
50
51
52
53
54
          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;
          }
      
c05bf005   Yarik   Comment added
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
          public static function tableName()
          {
              return '{{%artbox_comment}}';
          }
          
          public function rules()
          {
              return [
                  [
                      [
                          'text',
                          'entity',
                          'entity_id',
                      ],
                      'required',
                  ],
                  [
                      [
17ca60dd   Yarik   Added comments to...
73
74
75
76
77
78
79
80
                          'username',
                          'email',
                      ],
                      'required',
                      'on' => self::SCENARIO_GUEST,
                  ],
                  [
                      [
c05bf005   Yarik   Comment added
81
82
                          'text',
                          'entity',
17ca60dd   Yarik   Added comments to...
83
                          'username',
c05bf005   Yarik   Comment added
84
85
86
87
                      ],
                      'string',
                  ],
                  [
17ca60dd   Yarik   Added comments to...
88
89
90
91
92
93
                      [
                          'email',
                      ],
                      'email',
                  ],
                  [
c05bf005   Yarik   Comment added
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
                      [ '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(),
                      '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(),
                  ],
17ca60dd   Yarik   Added comments to...
136
137
138
                  [
                      'class' => RatingBehavior::className(),
                  ],
c05bf005   Yarik   Comment added
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
              ];
          }
          
          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'),
                  'date_update' => \Yii::t('artbox-comment', 'Date update'),
                  'date_delete' => \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'),
              ];
          }
      
          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,
                      ],
                  ],
              ]);
          }
          
          function deleteComment(): bool {
              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' ])
                  ->andFilterWhere(['status' => self::STATUS_ACTIVE])
                          ->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()
          {
              return $this->hasOne(RatingModel::className(), ['model_id' => 'artbox_comment_id'])->andWhere(['or', ['artbox_comment_rating.model' => NULL], ['artbox_comment_rating.model' => self::className()] ]);
          }
      }