Blame view

common/modules/comment/models/Comment.php 8.73 KB
b82db04a   Yarik   test
1
2
3
4
5
  <?php
      namespace common\modules\comment\models;
  
      /**
       * Class Comment
2d107e9e   Yarik   test
6
7
       * @property bool    $guestComment
       * @property integer $comment_id
b82db04a   Yarik   test
8
9
10
11
12
13
14
15
16
17
18
19
20
21
       * @package common\modules\comment\models
       */
      class Comment extends \yii\db\ActiveRecord
          implements \common\modules\comment\interfaces\CommentInterface
      {
  
          const STATUS_HIDDEN = 0;
          const STATUS_DELETED = 2;
          const STATUS_ACTIVE = 1;
          const STATUS_PERSONAL = 3;
  
          const SCENARIO_USER = 'user';
          const SCENARIO_GUEST = 'guest';
  
2d107e9e   Yarik   test
22
23
24
25
26
          /**
           * @var bool
           */
          public $guestComment = true;
  
b82db04a   Yarik   test
27
28
29
30
          public function rules()
          {
              return [
                  [
2d107e9e   Yarik   test
31
32
33
34
35
                      [
                          'text',
                          'user_name',
                          'user_email',
                      ],
b82db04a   Yarik   test
36
37
38
                      'required',
                  ],
                  [
2d107e9e   Yarik   test
39
40
41
42
43
44
45
                      [
                          'rating',
                      ],
                      'safe',
                  ],
                  [
                      [ 'user_email' ],
b82db04a   Yarik   test
46
47
48
                      'email',
                  ],
                  [
2d107e9e   Yarik   test
49
                      [ 'user_name' ],
b82db04a   Yarik   test
50
51
                      'string',
                  ],
2d107e9e   Yarik   test
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                  [
                      [
                          'comment_id',
                          'comment_pid',
                      ],
                      'integer',
                  ],
                  [
                      [ 'status' ],
                      'default',
                      'value' => 1,
                  ],
                  [
                      [ 'comment_pid' ],
                      'exist',
                      'targetAttribute' => 'comment_id',
                      'filter'          => [
                          'entity' => $this->entity,
                      ],
                  ],
b82db04a   Yarik   test
72
73
74
75
76
77
              ];
          }
  
          public function scenarios()
          {
              return [
2d107e9e   Yarik   test
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
                  self::SCENARIO_GUEST => [
                      'user_name',
                      'user_email',
                      'text',
                      'comment_pid',
                  ],
                  self::SCENARIO_USER  => [
                      'text',
                      'comment_pid',
                  ],
              ];
          }
  
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
                  [
                      'class'              => \yii\behaviors\TimestampBehavior::className(),
                      'createdAtAttribute' => 'date_add',
                      'updatedAtAttribute' => 'date_update',
                      'value'              => new \yii\db\Expression('NOW()'),
                  ],
b82db04a   Yarik   test
103
104
105
106
107
108
109
110
111
112
113
114
115
116
              ];
          }
  
          public static function tableName()
          {
              return '{{%comment}}';
          }
  
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
2d107e9e   Yarik   test
117
118
                  'text'       => \Yii::t('app', 'Комментарий'),
                  'user_name'  => \Yii::t('app', 'Имя'),
b82db04a   Yarik   test
119
120
121
122
                  'user_email' => \Yii::t('app', 'Email'),
              ];
          }
  
2d107e9e   Yarik   test
123
124
125
126
127
128
          public function getGuestComment()
          {
              return $this->guestComment;
          }
  
          public function setGuestComment($value)
b82db04a   Yarik   test
129
          {
2d107e9e   Yarik   test
130
              $this->guestComment = $value;
b82db04a   Yarik   test
131
132
133
134
          }
  
          public function getComments($entity)
          {
2d107e9e   Yarik   test
135
136
137
138
139
              return $this->find()
                          ->where([
                              'entity' => $this->entity,
                              'status' => 1,
                          ]);
b82db04a   Yarik   test
140
141
          }
  
2d107e9e   Yarik   test
142
          public function postComment()
b82db04a   Yarik   test
143
          {
2d107e9e   Yarik   test
144
145
146
147
148
149
150
              if($this->checkCreate()) {
                  if($this->insert()) {
                      $this->clearSafe();
                      return true;
                  } else {
                      return false;
                  }
b82db04a   Yarik   test
151
              } else {
2d107e9e   Yarik   test
152
                  $this->addError('comment_id', 'You can`t post comment here');
b82db04a   Yarik   test
153
154
155
156
                  return false;
              }
          }
  
2d107e9e   Yarik   test
157
          public function updateComment()
b82db04a   Yarik   test
158
          {
2d107e9e   Yarik   test
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
              if($this->checkUpdate()) {
                  if(empty( $this->comment_id )) {
                      $this->addError('comment_id', 'Comment ID not found');
                      return false;
                  } else {
                      if($this->update()) {
                          $this->clearSafe();
                          return true;
                      } else {
                          return false;
                      }
                  }
              } else {
                  $this->addError('comment_id', 'You can`t update this post');
                  return false;
              }
b82db04a   Yarik   test
175
176
          }
  
2d107e9e   Yarik   test
177
          public function deleteComment()
b82db04a   Yarik   test
178
          {
2d107e9e   Yarik   test
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
              if($this->checkDelete()) {
                  if(empty( $this->comment_id )) {
                      $this->addError('comment_id', 'Comment ID not found');
                      return false;
                  } else {
                      if($this->status == self::STATUS_DELETED) {
                          return false;
                      }
                      $this->status = self::STATUS_DELETED;
                      if($this->update()) {
                          $this->clearSafe();
                          return true;
                      } else {
                          return false;
                      }
                  }
              } else {
                  $this->addError('comment_id', 'You can`t delete this post');
                  return false;
              }
b82db04a   Yarik   test
199
200
          }
  
2d107e9e   Yarik   test
201
          public function checkCreate()
b82db04a   Yarik   test
202
          {
2d107e9e   Yarik   test
203
              if($this->getGuestComment()) {
b82db04a   Yarik   test
204
205
                  return true;
              } else {
2d107e9e   Yarik   test
206
                  return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [ 'entity' => $this->entity ]);
b82db04a   Yarik   test
207
208
209
              }
          }
  
2d107e9e   Yarik   test
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
          public function checkUpdate()
          {
              if($this->scenario == self::SCENARIO_GUEST) {
                  return false;
              } else {
                  return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [ 'entity' => $this->entity ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [ 'entity' => $this->entity ]);
              }
          }
  
          public function checkDelete()
          {
              if($this->scenario == self::SCENARIO_GUEST) {
                  return false;
              } else {
                  return \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE, [ 'entity' => $this->entity ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE_OWN, [ 'entity' => $this->entity ]);
              }
          }
  
          protected function clearSafe($setNew = true)
          {
b82db04a   Yarik   test
230
231
232
233
234
235
236
237
              $safe = $this->safeAttributes();
              $count = count($safe);
              $values = array_fill(0, $count, NULL);
              $result = array_combine($safe, $values);
              $this->setAttributes($result);
              $this->setIsNewRecord($setNew);
          }
  
2d107e9e   Yarik   test
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
          public function getParent()
          {
              return $this->hasOne(self::className(), [ 'comment_id' => 'comment_pid' ]);
          }
  
          public function getAuthor()
          {
              //            if($this->user_id != NULL) {
              return $this->hasOne(\common\models\User::className(), [ 'id' => 'user_id' ]);
              //            } else {
              //                return ['firstname' => $this->user_name, 'email' => $this->user_email];
              //            }
          }
  
          public function checkRating()
          {
              $rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'entity' => 'entityId' ])
                             ->one();
              if(!$rating instanceof \common\modules\comment\models\Rating) {
                  $rating = new \common\modules\comment\models\Rating([
                      'entity'  => $this->entityId,
                      'user_id' => $this->user_id,
                  ]);
                  $rating->save();
              }
          }
  
          public function getRating()
          {
              $this->checkRating();
              return $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'entity' => 'entityId' ]);
          }
  
          public function hasRating($return = true)
          {
              $rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'entity' => 'entityId' ])
                             ->andWhere([
                                 'not',
                                 [ 'value' => NULL ],
                             ])
                             ->one();
              if($return) {
                  return $rating;
              } else {
                  return $rating ? true : false;
              }
          }
  
          public function getEntityId()
          {
              return $this->formName() . '-' . $this->getPrimaryKey();
          }
      }