1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
/**
|
4
|
|
|
* This is the model class for table "comments".
|
5
|
|
|
*
|
6
|
|
|
* The followings are the available columns in table 'comments':
|
7
|
|
|
* @property integer $id
|
8
|
|
|
* @property integer $content_id
|
9
|
|
|
* @property integer $author_id
|
10
|
|
|
* @property string $comment
|
11
|
|
|
* @property string $created
|
12
|
|
|
* @property string $updated
|
13
|
|
|
*
|
14
|
|
|
* The followings are the available model relations:
|
15
|
|
|
* @property CommentMetadata[] $commentMetadatas
|
16
|
|
|
* @property Content $parent
|
17
|
|
|
* @property Content $content
|
18
|
|
|
* @property Users $user
|
19
|
|
|
*/
|
20
|
|
|
class Comments extends CiiModel
|
|
|
|
|
21
|
|
|
{
|
22
|
|
|
/**
|
23
|
|
|
* API Attribute for retrieving the counts
|
24
|
|
|
* @var int
|
25
|
|
|
*/
|
26
|
|
|
public $count = 0;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* AfterSave IsNewRecord
|
30
|
|
|
* @var boolean
|
31
|
|
|
*/
|
32
|
|
|
private $_isNewRecord = false;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* Returns the static model of the specified AR class.
|
36
|
|
|
* @param string $className active record class name.
|
37
|
|
|
* @return Comments the static model class
|
38
|
|
|
*/
|
39
|
|
|
public static function model($className=__CLASS__)
|
40
|
|
|
{
|
41
|
|
|
return parent::model($className);
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* @return string the associated database table name
|
46
|
|
|
*/
|
47
|
|
|
public function tableName()
|
48
|
|
|
{
|
49
|
|
|
return 'comments';
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* @return array validation rules for model attributes.
|
54
|
|
|
*/
|
55
|
|
|
public function rules()
|
56
|
|
|
{
|
57
|
|
|
// NOTE: you should only define rules for those attributes that
|
58
|
|
|
// will receive user inputs.
|
59
|
|
|
return array(
|
60
|
|
|
array('content_id, author_id, comment', 'required'),
|
61
|
|
|
array('content_id, author_id', 'numerical', 'integerOnly'=>true),
|
62
|
|
|
// The following rule is used by search().
|
63
|
|
|
array('id, content_id, author_id, comment, created, updated', 'safe', 'on'=>'search'),
|
64
|
|
|
);
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
/**
|
68
|
|
|
* @return array relational rules.
|
69
|
|
|
*/
|
70
|
|
|
public function relations()
|
71
|
|
|
{
|
72
|
|
|
// NOTE: you may need to adjust the relation name and the related
|
73
|
|
|
// class name for the relations automatically generated below.
|
74
|
|
|
return array(
|
75
|
|
|
'content' => array(self::BELONGS_TO, 'Content', 'content_id'),
|
76
|
|
|
'author' => array(self::BELONGS_TO, 'Users', 'author_id'),
|
77
|
|
|
);
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* @return array customized attribute labels (name=>label)
|
82
|
|
|
*/
|
83
|
|
|
public function attributeLabels()
|
84
|
|
|
{
|
85
|
|
|
return array(
|
86
|
|
|
'id' => Yii::t('ciims.models.Comments', 'ID'),
|
87
|
|
|
'content_id' => Yii::t('ciims.models.Comments', 'Content'),
|
88
|
|
|
'author_id' => Yii::t('ciims.models.Comments', 'User'),
|
89
|
|
|
'comment' => Yii::t('ciims.models.Comments', 'Comment'),
|
90
|
|
|
'created' => Yii::t('ciims.models.Comments', 'Created'),
|
91
|
|
|
'updated' => Yii::t('ciims.models.Comments', 'Updated'),
|
92
|
|
|
);
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* Retrieves a list of models based on the current search/filter conditions.
|
97
|
|
|
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
|
98
|
|
|
*/
|
99
|
|
|
public function search()
|
100
|
|
|
{
|
101
|
|
|
$criteria=new CDbCriteria;
|
102
|
|
|
|
103
|
|
|
$criteria->compare('id',$this->id);
|
104
|
|
|
$criteria->compare('content_id',$this->content_id);
|
105
|
|
|
$criteria->compare('author_id',$this->author_id);
|
106
|
|
|
$criteria->compare('comment',$this->comment,true);
|
107
|
|
|
$criteria->compare('created',$this->created,true);
|
108
|
|
|
$criteria->compare('updated',$this->updated,true);
|
109
|
|
|
|
110
|
|
|
return new CActiveDataProvider($this, array(
|
111
|
|
|
'criteria'=>$criteria,
|
112
|
|
|
));
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* Returns the API attributes for the model
|
117
|
|
|
* @return array
|
118
|
|
|
*/
|
119
|
|
|
public function getApiAttributes($params = array(), $relations = false)
|
120
|
|
|
{
|
121
|
|
|
$data = parent::getApiAttributes($params, $relations);
|
122
|
|
|
$user = Users::model()->findByPk($this->author_id);
|
123
|
|
|
$attributes = $user->getApiAttributes();
|
124
|
|
|
$data['user'] = array(
|
125
|
|
|
'firstName' => $attributes['firstName'],
|
126
|
|
|
'lastName' => $attributes['lastName'],
|
127
|
|
|
'username' => $attributes['username'],
|
128
|
|
|
);
|
129
|
|
|
|
130
|
|
|
$content = Content::model()->findByPk($data['content_id']);
|
131
|
|
|
$data['content'] = array(
|
132
|
|
|
'id' => $data['content_id'],
|
133
|
|
|
'title' => $content->title,
|
134
|
|
|
'slug' => $content->slug
|
135
|
|
|
);
|
136
|
|
|
|
137
|
|
|
// If this user cannot comment without approval
|
138
|
|
|
if ($user->getReputation() < 100)
|
139
|
|
|
$data['banned_comment'] = true;
|
140
|
|
|
|
141
|
|
|
return $data;
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
/**
|
145
|
|
|
* Set the created and updated records
|
146
|
|
|
* @see CiiModel::beforeSave();
|
147
|
|
|
*/
|
148
|
|
|
public function beforeSave()
|
149
|
|
|
{
|
150
|
|
|
if ($this->isNewRecord)
|
151
|
|
|
$this->_isNewRecord = true;
|
152
|
|
|
|
153
|
|
|
if (Content::model()->findByPk($this->content_id)->commentable)
|
154
|
|
|
return parent::beforeSave();
|
155
|
|
|
else
|
156
|
|
|
return false;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* After a new comment is posted, set the reputation += 10
|
161
|
|
|
* @see parent::afterSave();
|
162
|
|
|
*/
|
163
|
|
|
public function afterSave()
|
164
|
|
|
{
|
165
|
|
|
if ($this->_isNewRecord)
|
166
|
|
|
{
|
167
|
|
|
$user = Users::model()->findByPk($this->author_id);
|
168
|
|
|
$user->setReputation(10);
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
return parent::afterSave();
|
172
|
|
|
}
|
173
|
|
|
}
|
174
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.