Blame view

common/modules/comment/models/CommentModel.php 8.21 KB
4253cbec   root   first commit
1
2
3
4
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
  <?php
      namespace common\modules\comment\models;
      
      use common\behaviors\NotifyBehavior;
      use common\behaviors\RatingBehavior;
      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;
          
          const SCENARIO_USER = 'user';
          const SCENARIO_GUEST = 'guest';
          
          public $encryptedEntity;
          
          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;
          }
      
          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(),
                      '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(),
                  ],
a3127a63   Yarik   Comment notificat...
140
                  /* Notification: uncomment to enable notifications.
4253cbec   root   first commit
141
142
143
                  [
                      'class' => NotifyBehavior::className(),
                  ],
a3127a63   Yarik   Comment notificat...
144
                  */
4253cbec   root   first commit
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
              ];
          }
          
          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'),
              ];
          }
      
bff65579   Виталий   test commit
169
          function setEntity( $entity)
4253cbec   root   first commit
170
171
172
173
          {
              $this->entity = $entity;
          }
          
bff65579   Виталий   test commit
174
          function getEntity()
4253cbec   root   first commit
175
176
177
178
          {
              return $this->entity;
          }
          
bff65579   Виталий   test commit
179
          static function getTree( $entity,  $entityId)
4253cbec   root   first commit
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
          {
              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,
                      ],
                  ],
              ]);
          }
          
bff65579   Виталий   test commit
205
          function deleteComment() {
4253cbec   root   first commit
206
207
208
209
210
211
212
213
              if(\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) {
                  if($this->delete()) {
                      return true;
                  }
              }
              return false;
          }
          
bff65579   Виталий   test commit
214
          function setEntityId( $entityId)
4253cbec   root   first commit
215
216
217
218
          {
              $this->entityId = $entityId;
          }
          
bff65579   Виталий   test commit
219
          function getEntityId()
4253cbec   root   first commit
220
221
222
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
          {
              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()] ]);
          }
      }