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  if ($options['dontNeedApprove']) {
90  Yii::app()->user->setFlash('commentCreateSuccess' . $options['contentId'], CommentsModule::t('Your comment added successfully and will be displayed'));
91  } else {
92  Yii::app()->user->setFlash('commentCreateSuccess' . $options['contentId'], CommentsModule::t('Your comment added successfully and will be visible after approval of a moderator'));
93  }
94 
95  Yii::app()->user->setState('Comment' . $options['contentId'], null);
96  $this->onCommentCreated($comment);
97  }
98  if ($comment->hasErrors() && !isset($_POST['ajax'])) {
99  Yii::app()->user->setState('Comment' . $options['contentId'], array('attributes' => $comment->attributes, 'errors' => $comment->getErrors()));
100  }
101  }
102  } else {
103  Yii::app()->user->setFlash('commentCreateError', CommentsModule::t('Form error'));
104  }
105  $this->redirect($_GET['return_url']);
106  }
107 
108  /**
109  * Delete comment
110  *
111  * @param int $id comment id
112  * @param string $key options hash key
113  * @param string $return_url return url
114  *
115  * @throws CHttpException
116  *
117  * @return void
118  */
119  public function actionDelete($id, $key, $return_url)
120  {
121  $options = Yii::app()->cache->get($key);
122 
123  if (!empty($options) && !Yii::app()->user->isGuest) {
124  $comment = Comment::model()->findByPk($id);
125  if ($comment && CommentsModule::canDelete($comment)) {
126  if ($comment->delete()) {
127  $this->onCommentDeleted($comment);
128  Yii::app()->user->setFlash('commentDeleteSuccess' . $options['contentId'], CommentsModule::t("Comment deleted successfully"));
129  }
130  } else {
131  Yii::app()->user->setFlash('commentDeleteError' . $options['contentId'], CommentsModule::t("You have no access or comment deleted"));
132  }
133  } else {
134  Yii::app()->user->setFlash('commentDeleteError' . $options['contentId'], CommentsModule::t("You have no access"));
135  }
136  $this->redirect($return_url);
137  }
138 
139  /**
140  * Delete comment be not logged in. Mainly using in admin mails.
141  *
142  * @param int $id comment id
143  * @param string $key access hash key
144  *
145  * @return void
146  * @throws CHttpException
147  */
148  public function actionRemoteDelete($id, $key)
149  {
150  $comment = Comment::model()->resetScope()->findByPk($id);
151  if ($comment && $comment->hash() == $key) {
152  if ($comment->status == Comment::DELETED) {
153  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment already deleted'));
154  } else {
155  $comment->delete();
156  $this->onCommentDeleted($comment);
157  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment deleted'));
158  }
159  }else{
160  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('You have no access'));
161  $comment = false;
162  }
163  $this->render('remoteAction', array('comment' => $comment));
164  }
165 
166 
167  /**
168  * Approve comment
169  *
170  * @param int $id comment id
171  * @param string $key options hash key
172  * @param int $status flag Comment::NOT_APPROVED|Comment::APPROVED. Specify approve comment or not
173  * @param string $return_url return url
174  *
175  * @return void
176  */
177  public function actionApprove($id, $key, $status, $return_url)
178  {
179  $options = Yii::app()->cache->get($key);
180  if (!empty($options) && !Yii::app()->user->isGuest && in_array($status, array(Comment::APPROVED, Comment::NOT_APPROVED))) {
181  $comment = Comment::model()->findByPk($id);
182  if ($comment && CommentsModule::canApprove($comment)) {
183  $comment->status = $status;
184  $comment->update('status');
185  Yii::app()->user->setFlash(
186  'commentApproveSuccess' . $options['contentId'],
187  CommentsModule::t('Comment ' . ($status == Comment::NOT_APPROVED ? 'not' : '') . ' approved')
188  );
189  } else {
190  Yii::app()->user->setFlash('commentApproveError' . $options['contentId'], CommentsModule::t("You have no access"));
191  }
192  } else {
193  Yii::app()->user->setFlash('commentApproveError' . $options['contentId'], CommentsModule::t("You have no access or comment deleted"));
194  }
195  $this->redirect($return_url);
196  }
197 
198  /**
199  * Approve comment not logged in. Mainly using in admin mails.
200  *
201  * @param int $id comment id
202  * @param string $key access hash key
203  * @param int $status flag Comment::NOT_APPROVED|Comment::APPROVED. Specify approve comment or not
204  *
205  * @return void
206  * @throws CHttpException
207  */
208  public function actionRemoteApprove($id, $key, $status)
209  {
210  $comment = Comment::model()->resetScope()->findByPk($id);
211  if ($comment && $comment->hash() == $key) {
212  if ($comment->status == Comment::DELETED) {
213  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment already deleted'));
214  } elseif (in_array($status, array(Comment::APPROVED, Comment::NOT_APPROVED))) {
215  $comment->status = $status;
216  $comment->update('status');
217  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment ' . ($comment->status == Comment::NOT_APPROVED ? 'not' : '') . ' approved'));
218  }
219  }else{
220  $comment = false;
221  Yii::app()->user->setFlash('remoteActionState', CommentsModule::t('Comment is not available anymore'));
222  }
223  $this->render('remoteAction', array('comment' => $comment));
224  }
225 
226 
227  /**
228  * This method invoked when new comment created
229  *
230  * @param Comment $comment comment
231  *
232  * @return void
233  */
234  public function onCommentCreated(Comment $comment)
235  {
236  $pageUrl = $this->getWidgetPageUrl();
237  if ($comment->notify_moderator && !empty($comment->moderator_email)) {
238  $subject = CommentsModule::t('Comment created (Moderator) {subject}', array('{subject}' => $comment->subject));
239  $notification = new Notification(
240  $subject,
241  $this->renderMail('/mail_templates/comment_created_admin', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
242  array(new EmailRecipient($comment->moderator_email, $this->module->notificationsEmail))
243  );
244  Yii::app()->notificationManager->notifyAbout($notification);
245  }
246 
247  $subscribers = Yii::app()->db->createCommand()
248  ->select('email')
249  ->from('{{comments}}')
250  ->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))
251  ->group('email')->queryAll();
252  $recipients = array();
253  $fromEmail = $this->module->notificationsEmail;
254  foreach ($subscribers as $subscriber) {
255  if (trim($subscriber["email"]) != ""){
256  $recipients[] = new EmailRecipient($subscriber['email'], $fromEmail);
257  }
258  }
259  if (sizeof($recipients) > 0) {
260  $subject = CommentsModule::t('Comment created: {subject}', array('{subject}' => $comment->subject));
261  $notification = new Notification(
262  $subject,
263  $this->renderMail('/mail_templates/comment_created_user', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
264  $recipients
265  );
266 
267  Yii::app()->notificationManager->notifyAbout($notification);
268  }
269 
270  }
271 
272  /**
273  * This method invoked when comment deleted
274  *
275  * @param Comment $comment comment
276  *
277  * @return void
278  */
279  public function onCommentDeleted(Comment $comment)
280  {
281  if ($comment->notify_user) {
282  $pageUrl = $this->getWidgetPageUrl();
283  $subject = CommentsModule::t('Comment was deleted');
284  $notification = new Notification(
285  $subject,
286  $this->renderMail('/mail_templates/comment_deleted', $subject, array('comment' => $comment, 'pageUrl' => $pageUrl), true),
287  array(new EmailRecipient($comment->email))
288  );
289  Yii::app()->notificationManager->notifyAbout($notification);
290  }
291  }
292 }