Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CommentController.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  * Comment controller class file.
11  */
13 {
14 
15  /**
16  * Returns a list of external action classes.
17  *
18  * @return array
19  */
20  public function actions()
21  {
22  return (isset($_POST['ajax']) && $_POST['ajax'] === 'comment-form') ? array() : array(
23  'captcha' => array(
24  'class' => 'CCaptchaAction',
25  'backColor' => 0xFFFFFF,
26  'fixedVerifyCode' => YII_DEBUG ? 'polomo' : null
27  ),
28  );
29  }
30 
31  /**
32  * Get page url on which comments widget was placed
33  *
34  * @return string
35  */
36  protected function getWidgetPageUrl()
37  {
38  if (isset($_GET['return_url'])) {
39  return Yii::app()->createAbsoluteUrl($_GET['return_url']);
40  } elseif (isset($_SERVER['HTTP_REFERER'])) {
41  return $_SERVER['HTTP_REFERER'];
42  } else {
43  return '';
44  }
45 
46  }
47 
48  /**
49  * Intermediate action. Used for saving $return_url in session to return back after log in.
50  *
51  * @param string $return_url Return url
52  *
53  * @return void
54  */
55  public function actionLogin($return_url)
56  {
57  Yii::app()->user->setReturnUrl($return_url);
58  $this->redirect($this->module->loginUrl);
59  }
60 
61  /**
62  * Create Comment action.
63  * Reads widget options from session using options hash key. Using options to make validation.
64  *
65  * @return void
66  */
67  public function actionCreate()
68  {
69  if (isset($_POST['key'])) {
70  $options = Yii::app()->cache->get($_POST['key']);
71  }
72 
73  if (!empty($options)) {
74  if (!$options['allowAnonymous'] && Yii::app()->user->isGuest) {
75  Yii::app()->user->setFlash('commentCreateError' . $options['contentId'], CommentsModule::t('Anonymous comments not allowed'));
76  } elseif (isset($_POST['Comment'])) {
77  $comment = new Comment(Yii::app()->user->isGuest ? 'createAnonymous' : 'create');
78  $comment->attributes = $_POST['Comment'];
79  $comment->content_id = $options['contentId'];
80  $comment->notify_moderator = $options['notifyModeratorOnNew'];
81  $comment->moderator_email = $options['moderatorEmail'];
82  $comment->status = $options['dontNeedApprove'] ? Comment::APPROVED : Comment::NOT_APPROVED;
83  $comment->language = Yii::app()->language;
84  if (!Yii::app()->user->isGuest) {
85  $comment->user_id = Yii::app()->user->id;
86  $comment->email = Yii::app()->user->email;
87  }
88  if ($comment->save()) {
89  Yii::app()->user->setFlash('commentCreateSuccess' . $options['contentId'], CommentsModule::t('Your comment added successfully and will be displayed'));
90  Yii::app()->user->setState('Comment' . $options['contentId'], null);
91  $this->onCommentCreated($comment);
92  }
93  if ($comment->hasErrors() && !isset($_POST['ajax'])) {
94  Yii::app()->user->setState('Comment' . $options['contentId'], array('attributes' => $comment->attributes, 'errors' => $comment->getErrors()));
95  }
96  }
97  } else {
98  Yii::app()->user->setFlash('commentCreateError', CommentsModule::t('Form error'));
99  }
100  $this->redirect($_GET['return_url']);
101  }
102 
103  /**
104  * Delete comment
105  *
106  * @param int $id comment id
107  * @param string $key options hash key
108  * @param string $return_url return url
109  *
110  * @throws CHttpException
111  *
112  * @return void
113  */
114  public function actionDelete($id, $key, $return_url)
115  {
116  $options = Yii::app()->cache->get($key);
117 
118  if (!empty($options) && !Yii::app()->user->isGuest) {
119  $comment = Comment::model()->findByPk($id);
120  if ($comment && CommentsModule::canDelete($comment)) {
121  if ($comment->delete()) {
122  $this->onCommentDeleted($comment);
123  Yii::app()->user->setFlash('commentDeleteSuccess' . $options['contentId'], CommentsModule::t("Comment deleted successfully"));
124  }
125  } else {
126  Yii::app()->user->setFlash('commentDeleteError' . $options['contentId'], CommentsModule::t("You have no access or comment deleted"));
127  }
128  } else {
129  Yii::app()->user->setFlash('commentDeleteError' . $options['contentId'], CommentsModule::t("You have no access"));
130  }
131  $this->redirect($return_url);
132  }
133 
134  /**
135  * Delete comment be not logged in. Mainly using in admin mails.
136  *
137  * @param int $id comment id
138  * @param string $key access hash key
139  *
140  * @return void
141  * @throws CHttpException
142  */
143  public function actionRemoteDelete($id, $key)
144  {
145  $comment = Comment::model()->resetScope()->findByPk($id);
146  if ($comment && $comment->hash() == $key) {
147  if ($comment->status == Comment::DELETED) {
148  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment already deleted'));
149  } else {
150  $comment->delete();
151  $this->onCommentDeleted($comment);
152  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment deleted'));
153  }
154  }else{
155  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('You have no access'));
156  $comment = false;
157  }
158  $this->render('remoteAction', array('comment' => $comment));
159  }
160 
161 
162  /**
163  * Approve comment
164  *
165  * @param int $id comment id
166  * @param string $key options hash key
167  * @param int $status flag Comment::NOT_APPROVED|Comment::APPROVED. Specify approve comment or not
168  * @param string $return_url return url
169  *
170  * @return void
171  */
172  public function actionApprove($id, $key, $status, $return_url)
173  {
174  $options = Yii::app()->cache->get($key);
175  if (!empty($options) && !Yii::app()->user->isGuest && in_array($status, array(Comment::APPROVED, Comment::NOT_APPROVED))) {
176  $comment = Comment::model()->findByPk($id);
177  if ($comment && CommentsModule::canApprove($comment)) {
178  $comment->status = $status;
179  $comment->update('status');
180  Yii::app()->user->setFlash(
181  'commentApproveSuccess' . $options['contentId'],
182  CommentsModule::t('Comment ' . ($status == Comment::NOT_APPROVED ? 'not' : '') . ' approved')
183  );
184  } else {
185  Yii::app()->user->setFlash('commentApproveError' . $options['contentId'], CommentsModule::t("You have no access"));
186  }
187  } else {
188  Yii::app()->user->setFlash('commentApproveError' . $options['contentId'], CommentsModule::t("You have no access or comment deleted"));
189  }
190  $this->redirect($return_url);
191  }
192 
193  /**
194  * Approve comment not logged in. Mainly using in admin mails.
195  *
196  * @param int $id comment id
197  * @param string $key access hash key
198  * @param int $status flag Comment::NOT_APPROVED|Comment::APPROVED. Specify approve comment or not
199  *
200  * @return void
201  * @throws CHttpException
202  */
203  public function actionRemoteApprove($id, $key, $status)
204  {
205  $comment = Comment::model()->resetScope()->findByPk($id);
206  if ($comment && $comment->hash() == $key) {
207  if ($comment->status == Comment::DELETED) {
208  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment already deleted'));
209  } elseif (in_array($status, array(Comment::APPROVED, Comment::NOT_APPROVED))) {
210  $comment->status = $status;
211  $comment->update('status');
212  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment ' . ($comment->status == Comment::NOT_APPROVED ? 'not' : '') . ' approved'));
213  }
214  }else{
215  $comment = false;
216  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment is not available anymore'));
217  }
218  $this->render('remoteAction', array('comment' => $comment));
219  }
220 
221 
222  /**
223  * This method invoked when new comment created
224  *
225  * @param Comment $comment comment
226  *
227  * @return void
228  */
229  public function onCommentCreated(Comment $comment)
230  {
231  $pageUrl = $this->getWidgetPageUrl();
232  if ($comment->notify_moderator && !empty($comment->moderator_email)) {
233  $subject = CommentsModule::t('Comment created (Moderator) {subject}', array('{subject}' => $comment->subject));
234  $notification = new Notification(
235  $subject,
236  $this->renderMail('/mail_templates/comment_created_admin', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
237  array(new EmailRecipient($comment->moderator_email, $this->module->notificationsEmail))
238  );
239  Yii::app()->notificationManager->notifyAbout($notification);
240  }
241 
242  $subscribers = Yii::app()->db->createCommand()
243  ->select('email')
244  ->from('{{comments}}')
245  ->where('content_id = :content_id AND notify_user = :notify_user AND status != :status', array(':content_id' => $comment->content_id, ':notify_user' => true, ':status' => Comment::DELETED))
246  ->group('email')->queryAll();
247  $recipients = array();
248  $fromEmail = $this->module->notificationsEmail;
249  foreach ($subscribers as $subscriber) {
250  if (trim($subscriber["email"]) != ""){
251  $recipients[] = new EmailRecipient($subscriber['email'], $fromEmail);
252  }
253  }
254  if (sizeof($recipients) > 0) {
255  $subject = CommentsModule::t('Comment created: {subject}', array('{subject}' => $comment->subject));
256  $notification = new Notification(
257  $subject,
258  $this->renderMail('/mail_templates/comment_created_user', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
259  $recipients
260  );
261 
262  Yii::app()->notificationManager->notifyAbout($notification);
263  }
264 
265  }
266 
267  /**
268  * This method invoked when comment deleted
269  *
270  * @param Comment $comment comment
271  *
272  * @return void
273  */
274  public function onCommentDeleted(Comment $comment)
275  {
276  if ($comment->notify_user) {
277  $pageUrl = $this->getWidgetPageUrl();
278  $subject = CommentsModule::t('Comment was deleted');
279  $notification = new Notification(
280  $subject,
281  $this->renderMail('/mail_templates/comment_deleted', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
282  array(new EmailRecipient($comment->email))
283  );
284  Yii::app()->notificationManager->notifyAbout($notification);
285  }
286  }
287 }