ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilForumMailNotification.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/Mail/classes/class.ilMailNotification.php';
5 
12 {
13  const TYPE_THREAD_DELETED = 54;
14 
15  const TYPE_POST_NEW = 60;
17  const TYPE_POST_UPDATED = 62;
18  const TYPE_POST_CENSORED = 63;
19  const TYPE_POST_DELETED = 64;
20  const TYPE_POST_ANSWERED = 65;
22 
23  const PERMANENT_LINK_POST = 'PL_Post';
24  const PERMANENT_LINK_FORUM = 'PL_Forum';
25 
29  protected $is_cronjob = false;
30 
34  protected $provider;
35 
39  protected $logger;
40 
47  {
48  parent::__construct(false);
49  $this->provider = $provider;
50  $this->logger = $logger;
51  }
52 
56  protected function initMail()
57  {
58  $mail = parent::initMail();
59  $this->logger->debug('Initialized mail service');
60  return $mail;
61  }
62 
66  public function sendMail(array $a_rcp, $a_type, $a_parse_recipients = true)
67  {
68  $this->logger->debug('Delegating notification transport to mail service ...');
69  parent::sendMail($a_rcp, $a_type, $a_parse_recipients);
70  $this->logger->debug('Notification transport delegated');
71  }
72 
76  protected function setSubject($a_subject)
77  {
78  $value = parent::setSubject($a_subject);
79  $this->logger->debug(sprintf('Setting subject to: %s', $a_subject));
80  return $value;
81  }
82 
86  protected function appendAttachments()
87  {
88  if (count($this->provider->getAttachments()) > 0) {
89  $this->logger->debug('Adding attachments ...');
90  foreach ($this->provider->getAttachments() as $attachment) {
91  $this->appendBody($this->getLanguageText('attachment') . ": " . $attachment . "\n");
92  }
93  $this->appendBody("\n------------------------------------------------------------\n");
94  $this->setAttachments($this->provider->getAttachments());
95  }
96  }
97 
101  public function send()
102  {
108  global $ilSetting, $lng, $ilUser;
109 
110  if(!$ilSetting->get('forum_notification', 0))
111  {
112  $this->logger->debug('Forum notifications are globally disabled');
113  return false;
114  }
115 
116  if(!$this->getRecipients())
117  {
118  $this->logger->debug('No notification recipients, nothing to do');
119  return false;
120  }
121 
122  $lng->loadLanguageModule('forum');
123 
126 
127  switch($this->getType())
128  {
129  case self::TYPE_THREAD_DELETED:
130  foreach($this->getRecipients() as $rcp)
131  {
132  $this->initLanguage($rcp);
134 
135  $this->initMail();
136 
137  $this->setSubject(sprintf(
138  $this->getLanguageText('frm_noti_subject_del_thread'),
139  $this->provider->getForumTitle(),
140  $this->provider->getThreadTitle()
141  ));
142 
143  $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
144  $this->appendBody("\n\n");
145  $this->appendBody(sprintf($this->getLanguageText('thread_deleted_by'), $ilUser->getLogin(), $this->provider->getForumTitle()));
146  $this->appendBody("\n\n");
147  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
148  $this->appendBody("\n\n");
149  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
150  $this->appendBody("\n\n");
151  $this->appendBody($this->getLanguageText('content_deleted_thread') ."\n------------------------------------------------------------\n");
152 
153  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
154  $this->appendBody("\n");
155 
156  $post_date = ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostDate(), IL_CAL_DATETIME));
157  $this->appendBody($this->getLanguageText('date') . ": " . $post_date);
158  $this->appendBody("\n");
159  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
160  $this->appendBody("\n");
161  $this->appendBody($this->getLanguageText('frm_noti_message'));
162  $this->appendBody("\n");
163 
164  if($this->provider->getPostCensored() == 1)
165  {
166  $this->appendBody($this->provider->getCensorshipComment() . "\n");
167  }
168  else
169  {
170  $pos_message = $this->getSecurePostMessage();
171  $this->appendBody(strip_tags($pos_message) . "\n");
172  }
173  $this->appendBody("------------------------------------------------------------\n");
174 
175  $this->appendBody($this->getPermanentLink(self::PERMANENT_LINK_FORUM));
176  $this->appendBody(ilMail::_getInstallationSignature());
177 
178  $this->sendMail(array($rcp), array('system'));
179  }
180  break;
181 
182  case self::TYPE_POST_NEW:
183  foreach($this->getRecipients() as $rcp)
184  {
185  $this->initLanguage($rcp);
187 
188  $this->initMail();
189 
190  $this->setSubject(sprintf(
191  $this->getLanguageText('frm_noti_subject_new_post'),
192  $this->provider->getForumTitle(),
193  $this->provider->getThreadTitle()
194  ));
195 
196  $this->setBody(ilMail::getSalutation($rcp,$this->getLanguage()));
197  $this->appendBody("\n\n");
198  $this->appendBody(sprintf($this->getLanguageText('frm_noti_new_post'), $this->provider->getForumTitle()));
199  $this->appendBody("\n\n");
200  $this->appendBody($this->getLanguageText('forum').": ".$this->provider->getForumTitle());
201  $this->appendBody("\n\n");
202  $this->appendBody($this->getLanguageText('thread').": ".$this->provider->getThreadTitle());
203  $this->appendBody("\n\n");
204  $this->appendBody($this->getLanguageText('new_post').": \n------------------------------------------------------------\n");
205 
206  $this->appendBody($this->getLanguageText('author').": ". $this->provider->getPostUserName($this->getLanguage()));
207  $this->appendBody("\n");
208  $this->appendBody($this->getLanguageText('date').": ". ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostDate(), IL_CAL_DATETIME)));
209  $this->appendBody("\n");
210  $this->appendBody($this->getLanguageText('subject').": ". $this->provider->getPostTitle());
211  $this->appendBody("\n");
212  $this->appendBody($this->getLanguageText('frm_noti_message'));
213  $this->appendBody("\n");
214 
215  if($this->provider->getPostCensored() == 1)
216  {
217  $this->appendBody($this->provider->getCensorshipComment() . "\n");
218  }
219  else
220  {
221  $pos_message = $this->getSecurePostMessage();
222  $this->appendBody(strip_tags($pos_message) . "\n");
223  }
224  $this->appendBody("------------------------------------------------------------\n");
225 
226  $this->appendAttachments();
227 
228  $this->appendBody($this->getPermanentLink());
229  $this->appendBody(ilMail::_getInstallationSignature());
230 
231  $this->sendMail(array($rcp), array('system'));
232 
233  }
234  break;
235 
236  case self::TYPE_POST_ACTIVATION:
237  foreach($this->getRecipients() as $rcp)
238  {
239  $this->initLanguage($rcp);
241 
242  $this->initMail();
243 
244  $this->setSubject(sprintf(
245  $this->getLanguageText('frm_noti_subject_act_post'),
246  $this->provider->getForumTitle(),
247  $this->provider->getThreadTitle()
248  ));
249 
250  $this->setBody(ilMail::getSalutation($rcp,$this->getLanguage()));
251  $this->appendBody("\n\n");
252 
253  $this->appendBody($this->getLanguageText('forums_post_activation_mail'));
254  $this->appendBody("\n\n");
255  $this->appendBody($this->getLanguageText('forum').": ".$this->provider->getForumTitle());
256  $this->appendBody("\n\n");
257  $this->appendBody($this->getLanguageText('thread').": ".$this->provider->getThreadTitle());
258  $this->appendBody("\n\n");
259  $this->appendBody($this->getLanguageText('new_post').": \n------------------------------------------------------------\n");
260 
261  $this->appendBody($this->getLanguageText('author').": ". $this->provider->getPostUserName($this->getLanguage()));
262  $this->appendBody("\n");
263  $this->appendBody($this->getLanguageText('date').": ". ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostDate(), IL_CAL_DATETIME)));
264  $this->appendBody("\n");
265  $this->appendBody($this->getLanguageText('subject').": ". $this->provider->getPostTitle());
266  $this->appendBody("\n");
267  $this->appendBody($this->getLanguageText('frm_noti_message'));
268  $this->appendBody("\n");
269 
270  if($this->provider->getPostCensored() == 1)
271  {
272  $this->appendBody($this->provider->getCensorshipComment() . "\n");
273  }
274  else
275  {
276  $pos_message = $this->getSecurePostMessage();
277  $this->appendBody(strip_tags($pos_message) . "\n");
278  }
279  $this->appendBody("------------------------------------------------------------\n");
280 
281  $this->appendAttachments();
282 
283  $this->appendBody($this->getPermanentLink());
284  $this->appendBody(ilMail::_getInstallationSignature());
285 
286  $this->sendMail(array($rcp), array('system'));
287  }
288  break;
289 
290  case self::TYPE_POST_ANSWERED;
291  foreach($this->getRecipients() as $rcp)
292  {
293  $this->initLanguage($rcp);
295 
296  $this->initMail();
297 
298  $this->setSubject(sprintf(
299  $this->getLanguageText('frm_noti_subject_answ_post'),
300  $this->provider->getForumTitle(),
301  $this->provider->getThreadTitle()
302  ));
303 
304  $this->setBody(ilMail::getSalutation($rcp,$this->getLanguage()));
305  $this->appendBody("\n\n");
306 
307  $this->appendBody($this->getLanguageText('forum_post_replied'));
308  $this->appendBody("\n\n");
309  $this->appendBody($this->getLanguageText('forum').": ".$this->provider->getForumTitle());
310 
311  $this->appendBody("\n\n");
312  $this->appendBody($this->getLanguageText('thread').": ".$this->provider->getThreadTitle());
313  $this->appendBody("\n\n");
314  $this->appendBody($this->getLanguageText('new_post').": \n------------------------------------------------------------\n");
315 
316  $this->appendBody($this->getLanguageText('author').": ". $this->provider->getPostUserName($this->getLanguage()));
317  $this->appendBody("\n");
318  $this->appendBody($this->getLanguageText('date').": ". ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostDate(), IL_CAL_DATETIME)));
319  $this->appendBody("\n");
320  $this->appendBody($this->getLanguageText('subject').": ". $this->provider->getPostTitle());
321  $this->appendBody("\n");
322  $this->appendBody($this->getLanguageText('frm_noti_message'));
323  $this->appendBody("\n");
324 
325  if($this->provider->getPostCensored() == 1)
326  {
327  $this->appendBody($this->provider->getCensorshipComment() . "\n");
328  }
329  else
330  {
331  $pos_message = $this->getSecurePostMessage();
332  $this->appendBody(strip_tags($pos_message) . "\n");
333  }
334  $this->appendBody("------------------------------------------------------------\n");
335 
336  $this->appendAttachments();
337 
338  $this->appendBody($this->getPermanentLink());
339  $this->appendBody(ilMail::_getInstallationSignature());
340 
341  $this->sendMail(array($rcp), array('system'));
342 
343  }
344  break;
345 
346  case self::TYPE_POST_UPDATED:
347  foreach($this->getRecipients() as $rcp)
348  {
349  $this->initLanguage($rcp);
351 
352  $this->initMail();
353 
354  $this->setSubject(sprintf(
355  $this->getLanguageText('frm_noti_subject_upt_post'),
356  $this->provider->getForumTitle(),
357  $this->provider->getThreadTitle()
358  ));
359 
360  $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
361  $this->appendBody("\n\n");
362  $this->appendBody(sprintf($this->getLanguageText('post_updated_by'), $this->provider->getPostUpdateUserName($this->getLanguage()), $this->provider->getForumTitle()));
363  $this->appendBody("\n\n");
364  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
365  $this->appendBody("\n\n");
366  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
367  $this->appendBody("\n\n");
368  $this->appendBody($this->getLanguageText('content_post_updated') . "\n------------------------------------------------------------\n");
369 
370  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
371  $this->appendBody("\n");
372  $this->appendBody($this->getLanguageText('date') . ": " . ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostUpdate(), IL_CAL_DATETIME)));
373  $this->appendBody("\n");
374  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
375  $this->appendBody("\n");
376  $this->appendBody($this->getLanguageText('frm_noti_message'));
377  $this->appendBody("\n");
378 
379  if($this->provider->getPostCensored() == 1)
380  {
381  $this->appendBody($this->provider->getCensorshipComment() . "\n");
382  }
383  else
384  {
385  $pos_message = $this->getSecurePostMessage();
386  $this->appendBody(strip_tags($pos_message) . "\n");
387  }
388  $this->appendBody("------------------------------------------------------------\n");
389 
390  $this->appendAttachments();
391 
392  $this->appendBody($this->getPermanentLink());
393  $this->appendBody(ilMail::_getInstallationSignature());
394 
395  $this->sendMail(array($rcp), array('system'));
396  }
397  break;
398 
399  case self::TYPE_POST_CENSORED:
400  foreach($this->getRecipients() as $rcp)
401  {
402  $this->initLanguage($rcp);
404 
405  $this->initMail();
406 
407  $this->setSubject(sprintf(
408  $this->getLanguageText('frm_noti_subject_cens_post'),
409  $this->provider->getForumTitle(),
410  $this->provider->getThreadTitle()
411  ));
412 
413  $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
414  $this->appendBody("\n\n");
415  $this->appendBody(sprintf($this->getLanguageText('post_censored_by'), $this->provider->getPostUpdateUserName($this->getLanguage()) ,$this->provider->getForumTitle()));
416  $this->appendBody("\n\n");
417  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
418  $this->appendBody("\n\n");
419  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
420  $this->appendBody("\n\n");
421  $this->appendBody($this->getLanguageText('content_censored_post') . "\n------------------------------------------------------------\n");
422 
423  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
424  $this->appendBody("\n");
425  $this->appendBody($this->getLanguageText('date') . ": " . ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostCensoredDate(), IL_CAL_DATETIME)));
426  $this->appendBody("\n");
427  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
428  $this->appendBody("\n");
429  $this->appendBody($this->getLanguageText('frm_noti_message'));
430  $this->appendBody("\n");
431 
432  if($this->provider->getPostCensored() == 1)
433  {
434  $this->appendBody($this->provider->getCensorshipComment() . "\n");
435  }
436  else
437  {
438  $pos_message = $this->getSecurePostMessage();
439  $this->appendBody(strip_tags($pos_message) . "\n");
440  }
441  $this->appendBody("------------------------------------------------------------\n");
442 
443  $this->appendAttachments();
444 
445  $this->appendBody($this->getPermanentLink());
446  $this->appendBody(ilMail::_getInstallationSignature());
447 
448  $this->sendMail(array($rcp), array('system'));
449  }
450  break;
451  case self::TYPE_POST_UNCENSORED:
452  foreach($this->getRecipients() as $rcp)
453  {
454  $this->initLanguage($rcp);
456 
457  $this->initMail();
458 
459  $this->setSubject(sprintf(
460  $this->getLanguageText('frm_noti_subject_uncens_post'),
461  $this->provider->getForumTitle(),
462  $this->provider->getThreadTitle()
463  ));
464 
465  $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
466  $this->appendBody("\n\n");
467  $this->appendBody(sprintf($this->getLanguageText('post_uncensored_by'), $this->provider->getPostUpdateUserName($this->getLanguage())));
468  $this->appendBody("\n\n");
469  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
470  $this->appendBody("\n\n");
471  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
472  $this->appendBody("\n\n");
473  $this->appendBody($this->getLanguageText('forums_the_post') . "\n------------------------------------------------------------\n");
474 
475  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
476  $this->appendBody("\n");
477  $this->appendBody($this->getLanguageText('date') . ": " . ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostCensoredDate(), IL_CAL_DATETIME)));
478  $this->appendBody("\n");
479  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
480  $this->appendBody("\n");
481  $this->appendBody($this->getLanguageText('frm_noti_message'));
482  $this->appendBody("\n");
483 
484  $pos_message = $this->getSecurePostMessage();
485  $this->appendBody(strip_tags($pos_message) . "\n");
486 
487  $this->appendBody("------------------------------------------------------------\n");
488 
489  $this->appendAttachments();
490 
491  $this->appendBody($this->getPermanentLink());
492  $this->appendBody(ilMail::_getInstallationSignature());
493 
494  $this->sendMail(array($rcp), array('system'));
495  }
496  break;
497 
498  case self::TYPE_POST_DELETED:
499  foreach($this->getRecipients() as $rcp)
500  {
501  $this->initLanguage($rcp);
503 
504  $this->initMail();
505 
506  $this->setSubject(sprintf(
507  $this->getLanguageText('frm_noti_subject_del_post'),
508  $this->provider->getForumTitle(),
509  $this->provider->getThreadTitle()
510  ));
511 
512  $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
513  $this->appendBody("\n\n");
514  $this->appendBody(sprintf($this->getLanguageText('post_deleted_by'), $ilUser->getLogin(), $this->provider->getForumTitle()));
515  $this->appendBody("\n\n");
516  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
517  $this->appendBody("\n\n");
518  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
519  $this->appendBody("\n\n");
520  $this->appendBody($this->getLanguageText('content_deleted_post') ."\n------------------------------------------------------------\n");
521 
522  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
523  $this->appendBody("\n");
524 
525  $post_date = ilDatePresentation::formatDate(new ilDateTime($this->provider->getPostDate(), IL_CAL_DATETIME));
526  $this->appendBody($this->getLanguageText('date') . ": " . $post_date);
527  $this->appendBody("\n");
528  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
529  $this->appendBody("\n");
530  $this->appendBody($this->getLanguageText('frm_noti_message'));
531  $this->appendBody("\n");
532 
533  if($this->provider->getPostCensored() == 1)
534  {
535  $this->appendBody($this->provider->getCensorshipComment() . "\n");
536  }
537  else
538  {
539  $pos_message = $this->getSecurePostMessage();
540  $this->appendBody(strip_tags($pos_message) . "\n");
541  }
542  $this->appendBody("------------------------------------------------------------\n");
543 
544  $this->appendBody($this->getPermanentLink(self::PERMANENT_LINK_FORUM));
545  $this->appendBody(ilMail::_getInstallationSignature());
546 
547  $this->sendMail(array($rcp), array('system'));
548  }
549  break;
550  }
551 
554 
555  return true;
556  }
557 
561  protected function initLanguage($a_usr_id)
562  {
563  parent::initLanguage($a_usr_id);
564  $this->language->loadLanguageModule('forum');
565  }
566 
570  public function isCronjob()
571  {
572  return (bool)$this->is_cronjob;
573  }
574 
578  public function setIsCronjob($is_cronjob)
579  {
580  $this->is_cronjob = (bool)$is_cronjob;
581  }
582 
587  private function getPermanentLink($type = self::PERMANENT_LINK_POST)
588  {
592  global $ilClientIniFile;
593 
594  if($type == self::PERMANENT_LINK_FORUM)
595  {
596  $language_text = $this->getLanguageText("forums_notification_show_frm");
597  $forum_parameters = $this->provider->getRefId();
598  }
599  else
600  {
601  $language_text = $this->getLanguageText("forums_notification_show_post");
602  $forum_parameters = $this->provider->getRefId() . "_" . $this->provider->getThreadId() . "_" . $this->provider->getPostId();
603  }
604 
605  $this->logger->debug(sprintf(
606  'Building permanent with parameters %s', $forum_parameters
607  ));
608 
609  if($this->isCronjob())
610  {
611  $posting_link = sprintf($language_text,
612  ilUtil::_getHttpPath() . "/goto.php?target=frm_" .$forum_parameters. '&client_id=' . CLIENT_ID) . "\n\n";
613 
614  $posting_link .= sprintf($this->getLanguageText("forums_notification_intro"),
615  $ilClientIniFile->readVariable("client", "name"),
616  ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID) . "\n\n";
617  }
618  else
619  {
620  $posting_link = sprintf($language_text,
621  ilUtil::_getHttpPath() . "/goto.php?target=frm_" .$forum_parameters. '&client_id=' . CLIENT_ID) . "\n\n";
622 
623  $posting_link .= sprintf($this->getLanguageText("forums_notification_intro"),
624  $ilClientIniFile->readVariable("client", "name"),
625  ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID) . "\n\n";
626  }
627 
628  $this->logger->debug(sprintf(
629  'Link built: %s', $posting_link
630  ));
631 
632  return $posting_link;
633  }
634 
638  private function getSecurePostMessage()
639  {
640  $pos_message = $this->provider->getPostMessage();
641  if(strip_tags($pos_message) != $pos_message)
642  {
643  $pos_message = preg_replace("/\n/i", "", $pos_message);
644  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
645  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
646  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
647  return $pos_message;
648  }
649  return strip_tags($pos_message);
650  }
651 }
getType()
Get notification type.
const IL_CAL_DATETIME
setAttachments($a_att)
Set attachments.
static setUseRelativeDates($a_status)
set use relative dates
__construct(ilForumNotificationMailData $provider, \ilLogger $logger)
ilForumMailNotification constructor.
sendMail(array $a_rcp, $a_type, $a_parse_recipients=true)
static useRelativeDates()
check if relative dates are used
static setLanguage($a_lng)
set language
$a_type
Definition: workflow.php:93
Base class for course/group mail notifications.
static formatDate(ilDateTime $date)
Format a date public.
Date and time handling
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
Interface ilForumNotificationMailData.
getRecipients()
get array of recipients
static _getHttpPath()
global $ilSetting
Definition: privfeed.php:17
global $lng
Definition: privfeed.php:17
appendBody($a_body)
Append body text.
Component logger with individual log levels by component id.