Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CommentsFormWidget.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  * Widget for view comments form for current model
11  */
13 {
14  /**
15  * @var string moderator email
16  */
17  public $moderatorEmail;
18 
19  /**
20  * @var bool If notify moderator on new comments
21  */
22  public $notifyModeratorOnNew = true;
23 
24  /**
25  * @var bool Allow anonymous comments or not
26  */
27  public $allowAnonymous = true;
28 
29  /**
30  * @var What fields in anonymous form will be required (default all)
31  */
32  public $requiredForAnonymous = null;
33  /**
34  * @var bool If comments needs moderator approve
35  */
36  public $dontNeedApprove = false;
37 
38  /**
39  * @var string Widget options hash, to get this options in controller
40  */
41  public $hash;
42 
43  /**
44  * Make widgets options hash key and store options in session
45  *
46  * @return void
47  */
48  public function init()
49  {
50  $options = array(
51  'contentId' => $this->contentId,
52  'moderatorEmail' => $this->moderatorEmail,
53  'notifyModeratorOnNew' => $this->notifyModeratorOnNew,
54  'allowAnonymous' => $this->allowAnonymous,
55  'requiredForAnonymous' => $this->requiredForAnonymous,
56  'dontNeedApprove' => $this->dontNeedApprove
57  );
58  $this->hash = md5(serialize($options) . Yii::app()->getModule('comments')->hashSalt);
59  if (Yii::app()->cache->get($this->hash) != $options) {
60  Yii::app()->cache->set($this->hash, $options);
61  }
62  parent::init();
63  }
64 
65  /**
66  * Show comment form or login invitation based on widget options
67  *
68  * @return void
69  */
70  public function run()
71  {
72  if (!$this->allowAnonymous && Yii::app()->user->isGuest) {
73  $this->render('CommentsFormWidget_Login');
74  } else {
75  $model = new Comment(Yii::app()->user->isGuest ? 'createAnonymous' : 'create');
76  if(isset($this->requiredForAnonymous)){
77  $model->setRequiredForAnonymous($this->requiredForAnonymous);
78  }
79  if (!Yii::app()->user->isGuest) {
80  $model->email = Yii::app()->user->email;
81  $profile = Profile::model()->findByPk(Yii::app()->user->id);
82  $model->firstname = $profile->firstname;
83  $model->lastname = $profile->lastname;
84  }
85  if (($modelData = Yii::app()->user->getState('Comment' . $this->contentId)) !== null) {
86  $model->attributes = $modelData['attributes'];
87  foreach ($modelData['errors'] as $attribute => $errors) {
88  foreach ($errors as $e) {
89  $model->addError($attribute, $e);
90  }
91  }
92  Yii::app()->user->setState('Comment' . $this->contentId, null);
93  }
94  $this->render('CommentsFormWidget', array('model' => $model));
95  }
96  }
97 }
98 
99 ?>