Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Comment.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * This is the model class for table "{{comments}}".
11  *
12  * The followings are the available columns in table '{{comments}}':
13  * @property integer $id
14  * @property integer $parent_id
15  * @property string $content_id
16  * @property integer $user_id
17  * @property string $moderator_email
18  * @property string $firstname
19  * @property string $lastname
20  * @property string $email
21  * @property string $subject
22  * @property string $description
23  * @property integer $notify_moderator
24  * @property integer $notify_user
25  * @property integer $status
26  * @property string $createtime
27  * @property string $language
28  */
29 
30 class Comment extends CActiveRecord
31 {
32 
33  /**
34  * Values for $status field
35  */
36  const NOT_APPROVED = 0;
37  const APPROVED = 1;
38  const DELETED = -1;
39 
40  /**
41  * @var string field for capcha validation
42  */
43  public $verifyCode;
44  protected $requiredForAnonymous = null;
45 
46  /**
47  * Returns the static model of the specified AR class.
48  *
49  * @param string $className active record class name
50  *
51  * @return array|CActiveRecord
52  */
53  public static function model($className = __CLASS__)
54  {
55  return parent::model($className);
56  }
57 
58  /**
59  * Returns the name of the associated database table.
60  * By default this method returns the class name as the table name.
61  * You may override this method if the table is not named after this convention.
62  *
63  * @return string the table name
64  */
65  public function tableName()
66  {
67  return '{{comments}}';
68  }
69  /**
70  * Set the value of the protected variable $requiredForAnonymous
71  */
72  public function setRequiredForAnonymous($requiredForAnonymous){
73 
74  $this->requiredForAnonymous = $requiredForAnonymous;
75  }
76  /**
77  * Returns the array which include information about fields requirement
78  *
79  * @return array if field is require
80  */
81  public function getRequiredForAnonymous(){
82 
83  $initialValue = true;
84 
85  if(isset($this->requiredForAnonymous) && $this->requiredForAnonymous == false){
86  $initialValue = false;
87  }
88 
89  $Required = array(
90  'email' => $initialValue,
91  'firstname' => $initialValue,
92  'lastname' => $initialValue,
93  );
94 
95  if(isset($this->requiredForAnonymous) && $this->requiredForAnonymous){
96 
97  $requiredFields = explode(',', $this->requiredForAnonymous);
98  function trim_value(&$value){
99  $value = trim($value);
100  }
101  array_walk($requiredFields, 'trim_value');
102  foreach($Required as $fieldName=>&$isRequired){
103  if(in_array($fieldName,$requiredFields)){
104  $isRequired = true;
105  }else{
106  $isRequired = false;
107  }
108  }
109  }
110  return $Required;
111  }
112 
113  /**
114  * Returns the validation rules for attributes.
115  *
116  * @return array
117  */
118  public function rules()
119  {
120  $rules = array();
121 
122  $requiredForAnonymous = $this->getRequiredForAnonymous();
123 
124  foreach($requiredForAnonymous as $fieldName => $isRequire){
125  if($isRequire){
126  $rules[] = array( $fieldName, 'required', 'on' => 'createAnonymous');
127  }
128  }
129 
130  $rules = CMap::mergeArray($rules, array(
131  array('content_id, subject, description, language', 'required'),
132  array('email', 'required', 'except' => 'createAnonymous'),
133  array('parent_id, user_id, status', 'numerical', 'integerOnly' => true),
134  array('notify_moderator, notify_user', 'boolean'),
135  array('content_id, moderator_email, firstname, lastname, email, subject, createtime, language', 'length', 'max' => 255),
136  array('email, moderator_email', 'email'),
137  // The following rule is used by search().
138  // Please remove those attributes that should not be searched.
139  array('id, content_id, email, subject, description, parent_id, user_id, notify, status, moderator_email, firstname, lastname, createtime', 'safe', 'on' => 'search'),
140  array('firstname, lastname, description, email, subject','filter', 'filter'=>array($obj=new CHtmlPurifier(),'purify')),
141  ));
142  if (Yii::app()->getController()) {
143  $rules[] = array('verifyCode', 'captcha', 'on' => 'createAnonymous');
144  $rules[] = array('verifyCode', 'captcha', 'on' => 'create');
145  }
146  return $rules;
147  }
148 
149  /**
150  * Return list of related object declarations
151  *
152  * @return array
153  */
154  public function relations()
155  {
156  return array(
157  'childs' => array(self::HAS_MANY, 'Comment', 'parent_id', 'alias' => 'comments_childs'),
158  'parent' => array(self::BELONGS_TO, 'Comment', 'parent_id')
159  );
160  }
161 
162  /**
163  * Returns the default named scope that should be implicitly applied to all queries for this model.
164  *
165  * @return array
166  */
167  public function defaultScope()
168  {
169  return array(
170  'condition' => 'status != ' . self::DELETED
171  );
172  }
173 
174  /**
175  * Returns the declaration of named scopes.
176  *
177  * @return array
178  */
179  public function scopes()
180  {
181  return array(
182  'approved' => array(
183  'condition' => 'status = ' . self::APPROVED
184  ),
185  'isParent' => array(
186  'condition' => 't.parent_id is NULL'
187  ),
188  'isChild' => array(
189  'condition' => 't.parent_id is NOT NULL'
190  )
191  );
192  }
193 
194  /**
195  * Delete comment
196  *
197  * @param bool $completely if true delete from database
198  *
199  * @return bool
200  */
201  public function delete($completely = false)
202  {
203  if ($completely) {
204  return parent::delete();
205  } else {
206  $this->status = self::DELETED;
207  return $this->update('status');
208  }
209  }
210 
211  /**
212  * Get comment hash
213  *
214  * @return string comment hash for delete via email
215  */
216  public function hash()
217  {
218  $str = $this->id . $this->parent_id . $this->subject . $this->description . $this->email . Yii::app()->getModule('comments')->hashSalt;
219  return md5($str);
220  }
221 
222  /**
223  * Get attribute labels
224  *
225  * @return array
226  */
227  public function attributeLabels()
228  {
229  return array(
230  'content_id' => CommentsModule::t('Content id'),
231  'email' => CommentsModule::t('Email'),
232  'subject' => CommentsModule::t('Subject'),
233  'description' => CommentsModule::t('Description'),
234  'parent_id' => CommentsModule::t('Parent id'),
235  'user_id' => CommentsModule::t('User id'),
236  'notify_user' => CommentsModule::t('Notify me on new comments'),
237  'status' => CommentsModule::t('Status'),
238  'moderator_email' => CommentsModule::t('Moderator email'),
239  'firstname' => CommentsModule::t('First name'),
240  'lastname' => CommentsModule::t('Last name'),
241  'verifyCode' => CommentsModule::t('Verify Code'),
242  );
243  }
244 
245  /**
246  * Comment status in readable format
247  *
248  * @return string
249  */
250  public function getStatusText()
251  {
252  $label = '';
253  switch ($this->status) {
254  case self::DELETED:
255  $label = CommentsModule::t('Deleted');
256  break;
257  case self::NOT_APPROVED:
258  $label = CommentsModule::t('Not approved');
259  break;
260  case self::APPROVED:
261  $label = CommentsModule::t('Approved');
262  break;
263  }
264  return $label;
265  }
266 
267  /**
268  * Search
269  *
270  * @return CActiveDataProvider
271  */
272  public function search()
273  {
274  // Warning: Please modify the following code to remove attributes that
275  // should not be searched.
276 
277  $criteria = new CDbCriteria;
278 
279  $criteria->compare('id', $this->id);
280  $criteria->compare('parent_id', $this->parent_id);
281  $criteria->compare('content_id', $this->content_id, true);
282  $criteria->compare('user_id', $this->user_id);
283  $criteria->compare('moderator_email', $this->moderator_email, true);
284  $criteria->compare('firstname', $this->firstname, true);
285  $criteria->compare('lastname', $this->lastname, true);
286  $criteria->compare('email', $this->email, true);
287  $criteria->compare('subject', $this->subject, true);
288  $criteria->compare('description', $this->description, true);
289  $criteria->compare('notify_moderator', $this->notify_moderator);
290  $criteria->compare('notify_user', $this->notify_user);
291  $criteria->compare('status', $this->status);
292  $criteria->compare('createtime', $this->createtime, true);
293 
294  return new CActiveDataProvider($this, array('criteria' => $criteria,));
295  }
296 
297 }