ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
10 {
11  const TYPE_THREAD_DELETED = 54;
12 
13  const TYPE_POST_NEW = 60;
15  const TYPE_POST_UPDATED = 62;
16  const TYPE_POST_CENSORED = 63;
17  const TYPE_POST_DELETED = 64;
18  const TYPE_POST_ANSWERED = 65;
20 
21  const PERMANENT_LINK_POST = 'PL_Post';
22  const PERMANENT_LINK_FORUM = 'PL_Forum';
23 
27  protected $is_cronjob = false;
28 
32  protected $provider;
33 
37  protected $logger;
38 
45  {
46  parent::__construct(false);
47  $this->provider = $provider;
48  $this->logger = $logger;
49  }
50 
54  protected function initMail() : ilMail
55  {
56  $mail = parent::initMail();
57  $this->logger->debug('Initialized mail service');
58  return $mail;
59  }
60 
64  public function sendMail(array $a_rcp, $a_parse_recipients = true)
65  {
66  $this->logger->debug(sprintf(
67  'Delegating notification transport to mail service for recipients: %s',
68  print_r($a_rcp, true)
69  ));
70  parent::sendMail($a_rcp, $a_parse_recipients);
71  $this->logger->debug('Notification transport delegated');
72  }
73 
77  protected function setSubject($a_subject)
78  {
79  $value = parent::setSubject($a_subject);
80  $this->logger->debug(sprintf('Setting subject to: %s', $a_subject));
81  return $value;
82  }
83 
87  protected function appendAttachments()
88  {
89  if (count($this->provider->getAttachments()) > 0) {
90  $this->logger->debug('Adding attachments ...');
91  foreach ($this->provider->getAttachments() as $attachment) {
92  $this->appendBody($this->getLanguageText('attachment') . ": " . $attachment . "\n");
93  }
94  $this->appendBody("\n------------------------------------------------------------\n");
95  $this->setAttachments($this->provider->getAttachments());
96  }
97  }
98 
102  public function send()
103  {
104  global $DIC;
105  $ilSetting = $DIC->settings();
106  $lng = $DIC->language();
107 
108  if (!$ilSetting->get('forum_notification', 0)) {
109  $this->logger->debug('Forum notifications are globally disabled');
110  return false;
111  }
112 
113  if (!$this->getRecipients()) {
114  $this->logger->debug('No notification recipients, nothing to do');
115  return false;
116  }
117 
118  $lng->loadLanguageModule('forum');
119 
122 
123  switch ($this->getType()) {
124  case self::TYPE_THREAD_DELETED:
125  foreach ($this->getRecipients() as $rcp) {
126  $this->initLanguage($rcp);
127  $customText = sprintf($this->getLanguageText('thread_deleted_by'), $this->provider->getDeletedBy(), $this->provider->getForumTitle());
128  $this->sendMailWithoutAttachments('frm_noti_subject_del_thread', (int) $rcp, (string) $customText, 'content_deleted_thread');
129  }
130  break;
131 
132  case self::TYPE_POST_NEW:
133  foreach ($this->getRecipients() as $rcp) {
134  $this->initLanguage($rcp);
135  $customText = sprintf($this->getLanguageText('frm_noti_new_post'), $this->provider->getForumTitle());
136  $this->sendMailWithAttachments('frm_noti_subject_new_post', (int) $rcp, (string) $customText, 'new_post');
137  }
138  break;
139 
140  case self::TYPE_POST_ACTIVATION:
141  foreach ($this->getRecipients() as $rcp) {
142  $this->initLanguage($rcp);
143  $customText = $this->getLanguageText('forums_post_activation_mail');
144  $this->sendMailWithAttachments('frm_noti_subject_act_post', (int) $rcp, (string) $customText, 'new_post');
145  }
146  break;
147 
148  case self::TYPE_POST_ANSWERED:
149  foreach ($this->getRecipients() as $rcp) {
150  $this->initLanguage($rcp);
151  $customText = $this->getLanguageText('forum_post_replied');
152  $this->sendMailWithAttachments('frm_noti_subject_answ_post', (int) $rcp, (string) $customText, 'new_post');
153  }
154  break;
155 
156  case self::TYPE_POST_UPDATED:
157  foreach ($this->getRecipients() as $rcp) {
158  $this->initLanguage($rcp);
159  $customText = sprintf($this->getLanguageText('post_updated_by'), $this->provider->getPostUpdateUserName($this->getLanguage()), $this->provider->getForumTitle());
160  $date = $this->provider->getPostUpdate();
161  $this->sendMailWithAttachments('frm_noti_subject_upt_post', (int) $rcp, (string) $customText, 'content_post_updated', $date);
162  }
163  break;
164 
165  case self::TYPE_POST_CENSORED:
166  foreach ($this->getRecipients() as $rcp) {
167  $this->initLanguage($rcp);
168  $customText = sprintf($this->getLanguageText('post_censored_by'), $this->provider->getPostUpdateUserName($this->getLanguage()), $this->provider->getForumTitle());
169  $date = $this->provider->getPostCensoredDate();
170  $this->sendMailWithAttachments('frm_noti_subject_cens_post', (int) $rcp, (string) $customText, 'content_censored_post', $date);
171  }
172  break;
173 
174  case self::TYPE_POST_UNCENSORED:
175  foreach ($this->getRecipients() as $rcp) {
176  $this->initLanguage($rcp);
177  $customText = sprintf($this->getLanguageText('post_uncensored_by'), $this->provider->getPostUpdateUserName($this->getLanguage()));
178  $date = $this->provider->getPostCensoredDate();
179  $this->sendMailWithAttachments('frm_noti_subject_uncens_post', (int) $rcp, (string) $customText, 'forums_the_post', $date);
180  }
181  break;
182 
183  case self::TYPE_POST_DELETED:
184  foreach ($this->getRecipients() as $rcp) {
185  $this->initLanguage($rcp);
186  $customText = sprintf($this->getLanguageText('post_deleted_by'), $this->provider->getDeletedBy(), $this->provider->getForumTitle());
187  $this->sendMailWithoutAttachments('frm_noti_subject_del_post', (int) $rcp, (string) $customText, 'content_deleted_post');
188  }
189  break;
190  }
191 
194 
195  return true;
196  }
197 
201  protected function initLanguage($a_usr_id)
202  {
203  parent::initLanguage($a_usr_id);
204  $this->language->loadLanguageModule('forum');
205  }
206 
210  public function isCronjob()
211  {
212  return (bool) $this->is_cronjob;
213  }
214 
218  public function setIsCronjob($is_cronjob)
219  {
220  $this->is_cronjob = (bool) $is_cronjob;
221  }
222 
227  private function getPermanentLink($type = self::PERMANENT_LINK_POST)
228  {
229  global $DIC;
230  $ilClientIniFile = $DIC['ilClientIniFile'];
231 
232  if ($type == self::PERMANENT_LINK_FORUM) {
233  $language_text = $this->getLanguageText("forums_notification_show_frm");
234  $forum_parameters = $this->provider->getRefId();
235  } else {
236  $language_text = $this->getLanguageText("forums_notification_show_post");
237  $forum_parameters = $this->provider->getRefId() . "_" . $this->provider->getThreadId() . "_" . $this->provider->getPostId();
238  }
239 
240  $this->logger->debug(sprintf(
241  'Building permanent with parameters %s',
242  $forum_parameters
243  ));
244 
245  if ($this->isCronjob()) {
246  $posting_link = sprintf(
247  $language_text,
248  ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
249  ) . "\n\n";
250 
251  $posting_link .= sprintf(
252  $this->getLanguageText("forums_notification_intro"),
253  $ilClientIniFile->readVariable("client", "name"),
254  ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
255  ) . "\n\n";
256  } else {
257  $posting_link = sprintf(
258  $language_text,
259  ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
260  ) . "\n\n";
261 
262  $posting_link .= sprintf(
263  $this->getLanguageText("forums_notification_intro"),
264  $ilClientIniFile->readVariable("client", "name"),
265  ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
266  ) . "\n\n";
267  }
268 
269  $this->logger->debug(sprintf(
270  'Link built: %s',
271  $posting_link
272  ));
273 
274  return $posting_link;
275  }
276 
280  private function getPostMessage() : string
281  {
282  $pos_message = $this->provider->getPostMessage();
283  if (strip_tags($pos_message) !== $pos_message) {
284  $pos_message = preg_replace("/\n/i", "", $pos_message);
285  $pos_message = preg_replace("/<li([^>]*)>/i", "\n<li$1>", $pos_message);
286  $pos_message = preg_replace("/<\/ul([^>]*)>(?!\s*?(<p|<ul))/i", "</ul$1>\n", $pos_message);
287  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
288  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
289  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
290 
291  return $pos_message;
292  }
293 
294  return $pos_message;
295  }
296 
306  private function sendMailWithAttachments(
307  string $subjectLanguageId,
308  int $userId,
309  string $customText,
310  string $action,
311  string $date = ''
312  ) {
313  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
314  $this->appendAttachments();
315  $this->addLinkToMail();
316  $this->sendMail(array($userId));
317  }
318 
328  private function sendMailWithoutAttachments(
329  string $subjectLanguageId,
330  int $userId,
331  string $customText,
332  string $action,
333  string $date = ''
334  ) {
335  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
336  $this->addLinkToMail();
337  $this->sendMail(array($userId));
338  }
339 
349  private function createMail(
350  string $subject,
351  int $userId,
352  string $customText,
353  string $action,
354  string $date
355  ) {
356  $date = $this->createMailDate($date);
357 
358  $this->addMailSubject($subject);
359 
360  $this->setBody(ilMail::getSalutation($userId, $this->getLanguage()));
361  $this->appendBody("\n\n");
362  $this->appendBody($customText);
363  $this->appendBody("\n\n");
364  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
365  $this->appendBody("\n\n");
366  if ($this->provider->providesClosestContainer()) {
367  $this->appendBody(
368  $this->getLanguageText('frm_noti_obj_' . $this->provider->closestContainer()->getType()) . ": " .
369  $this->provider->closestContainer()->getTitle()
370  );
371  $this->appendBody("\n\n");
372  }
373  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
374  $this->appendBody("\n\n");
375  $this->appendBody($this->getLanguageText($action) . ": \n------------------------------------------------------------\n");
376 
377  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
378  $this->appendBody("\n");
379  $this->appendBody($this->getLanguageText('date') . ": " . $date);
380  $this->appendBody("\n");
381  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
382  $this->appendBody("\n");
383  $this->appendBody($this->getLanguageText('frm_noti_message'));
384  $this->appendBody("\n");
385 
386  $message = strip_tags($this->getPostMessage());
387 
388  if ($this->provider->getPostCensored() == 1) {
389  $message = $this->provider->getCensorshipComment();
390  }
391 
392  $this->appendBody($message . "\n");
393  $this->appendBody("------------------------------------------------------------\n");
394  }
395 
400  private function addMailSubject(string $subject)
401  {
402  $this->initMail();
403 
404  $container_text = '';
405  if ($this->provider->providesClosestContainer()) {
406  $container_text = " (" .
407  $this->getLanguageText('obj_' . $this->provider->closestContainer()->getType()) .
408  " \"" . $this->provider->closestContainer()->getTitle() . "\")";
409  }
410 
411  $this->setSubject(sprintf(
412  $this->getLanguageText($subject),
413  $this->provider->getForumTitle(),
414  $container_text,
415  $this->provider->getThreadTitle()
416  ));
417  }
418 
425  private function createMailDate(string $date) : string
426  {
428 
429  if ($date === '') {
430  $date = $this->provider->getPostDate();
431  }
432 
434 
435  return $date;
436  }
437 
441  private function addLinkToMail()
442  {
443  $this->appendBody($this->getPermanentLink());
445  }
446 }
getType()
Get notification type.
const IL_CAL_DATETIME
createMail(string $subject, int $userId, string $customText, string $action, string $date)
sendMailWithoutAttachments(string $subjectLanguageId, int $userId, string $customText, string $action, string $date='')
Add body and send mail without attachments.
setAttachments($a_att)
Set attachments.
sendMail(array $a_rcp, $a_parse_recipients=true)
static setUseRelativeDates($a_status)
set use relative dates
__construct(ilForumNotificationMailData $provider, \ilLogger $logger)
ilForumMailNotification constructor.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
static useRelativeDates()
check if relative dates are used
sendMailWithAttachments(string $subjectLanguageId, int $userId, string $customText, string $action, string $date='')
Add body and send mail with attachments.
static setLanguage($a_lng)
set language
Base class for course/group mail notifications.
$lng
const CLIENT_ID
Definition: constants.php:39
global $DIC
Definition: goto.php:24
Interface ilForumNotificationMailData.
getRecipients()
get array of recipients
static _getHttpPath()
getPermanentLink($type=self::PERMANENT_LINK_POST)
global $ilSetting
Definition: privfeed.php:17
__construct(Container $dic, ilPlugin $plugin)
appendBody($a_body)
Append body text.
$message
Definition: xapiexit.php:14
Component logger with individual log levels by component id.
language()
Definition: language.php:2
static _getInstallationSignature()
static getSalutation($a_usr_id, ilLanguage $a_language=null)