Blame view

common/modules/comment/models/Comment.php 2.86 KB
b82db04a   Yarik   test
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
  <?php
      namespace common\modules\comment\models;
  
      /**
       * Class Comment
       * @property bool $guestComment
       * @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';
  
          public function rules()
          {
              return [
                  [
                      ['text'],
                      'required',
                  ],
                  [
                      ['user_email'],
                      'email',
                  ],
                  [
                      ['user_name'],
                      'string',
                  ],
              ];
          }
  
          public function scenarios()
          {
              return [
                  self::SCENARIO_GUEST => ['user_name', 'user_email', 'text'],
                  self::SCENARIO_USER => ['text'],
              ];
          }
  
          public static function tableName()
          {
              return '{{%comment}}';
          }
  
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'text' => \Yii::t('app', 'Комментарий'),
                  'user_name' => \Yii::t('app', 'Имя'),
                  'user_email' => \Yii::t('app', 'Email'),
              ];
          }
  
          public function getGuestComment($entity)
          {
              return true;
          }
  
          public function getComments($entity)
          {
              return $this->find()->where(['entity' => $this->entity]);
          }
  
          public function postComment($data)
          {
              if($this->load($data) && $this->insert($data)) {
                  $this->clearSafe();
                  return true;
              } else {
                  return false;
              }
          }
  
          public function updateComment($comment_id)
          {
              // TODO: Implement updateComment() method.
          }
  
          public function deleteComment($comment_id)
          {
              // TODO: Implement deleteComment() method.
          }
  
          public function checkCreate($entity)
          {
              if($this->getGuestComment($entity)) {
                  return true;
              } else {
                  return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, ['entity' => $entity]);
              }
          }
  
          protected function clearSafe($setNew = true) {
              $safe = $this->safeAttributes();
              $count = count($safe);
              $values = array_fill(0, $count, NULL);
              $result = array_combine($safe, $values);
              $this->setAttributes($result);
              $this->setIsNewRecord($setNew);
          }
  
      }