ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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()
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_type, $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_type, $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 getSecurePostMessage()
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  return $pos_message;
291  }
292  return strip_tags($pos_message);
293  }
294 
304  private function sendMailWithAttachments(
305  string $subjectLanguageId,
306  int $userId,
307  string $customText,
308  string $action,
309  string $date = ''
310  ) {
311  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
312  $this->appendAttachments();
313  $this->addLinkToMail();
314  $this->sendMail(array($userId), array('system'));
315  }
316 
326  private function sendMailWithoutAttachments(
327  string $subjectLanguageId,
328  int $userId,
329  string $customText,
330  string $action,
331  string $date = ''
332  ) {
333  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
334  $this->addLinkToMail();
335  $this->sendMail(array($userId), array('system'));
336  }
337 
347  private function createMail(
348  string $subject,
349  int $userId,
350  string $customText,
351  string $action,
352  string $date
353  ) {
354  $date = $this->createMailDate($date);
355 
356  $this->addMailSubject($subject);
357 
358  $this->setBody(ilMail::getSalutation($userId, $this->getLanguage()));
359  $this->appendBody("\n\n");
360  $this->appendBody($customText);
361  $this->appendBody("\n\n");
362  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
363  $this->appendBody("\n\n");
364  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
365  $this->appendBody("\n\n");
366  $this->appendBody($this->getLanguageText($action) . ": \n------------------------------------------------------------\n");
367 
368  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
369  $this->appendBody("\n");
370  $this->appendBody($this->getLanguageText('date') . ": " . $date);
371  $this->appendBody("\n");
372  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
373  $this->appendBody("\n");
374  $this->appendBody($this->getLanguageText('frm_noti_message'));
375  $this->appendBody("\n");
376 
377  $message = strip_tags($this->getSecurePostMessage());
378 
379  if ($this->provider->getPostCensored() == 1) {
380  $message = $this->provider->getCensorshipComment();
381  }
382 
383  $this->appendBody($message . "\n");
384  $this->appendBody("------------------------------------------------------------\n");
385  }
386 
391  private function addMailSubject(string $subject)
392  {
393  $this->initMail();
394 
395  $this->setSubject(sprintf(
396  $this->getLanguageText($subject),
397  $this->provider->getForumTitle(),
398  $this->provider->getThreadTitle()
399  ));
400  }
401 
408  private function createMailDate(string $date) : string
409  {
411 
412  if ($date === '') {
413  $date = $this->provider->getPostDate();
414  }
415 
417 
418  return $date;
419  }
420 
424  private function addLinkToMail()
425  {
426  $this->appendBody($this->getPermanentLink());
428  }
429 }
getType()
Get notification type.
const IL_CAL_DATETIME
$action
global $DIC
Definition: saml.php:7
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.
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.
sendMail(array $a_rcp, $a_type, $a_parse_recipients=true)
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
$a_type
Definition: workflow.php:92
Base class for course/group mail notifications.
catch(Exception $e) $message
$lng
Date and time handling
Interface ilForumNotificationMailData.
getRecipients()
get array of recipients
static _getHttpPath()
getPermanentLink($type=self::PERMANENT_LINK_POST)
global $ilSetting
Definition: privfeed.php:17
appendBody($a_body)
Append body text.
Component logger with individual log levels by component id.
static _getInstallationSignature()
static getSalutation($a_usr_id, ilLanguage $a_language=null)