ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilForumMailNotification.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  private const PERMANENT_LINK_POST = 'PL_Post';
27  private const PERMANENT_LINK_FORUM = 'PL_Forum';
28  public const TYPE_THREAD_DELETED = 54;
29  public const TYPE_POST_NEW = 60;
30  public const TYPE_POST_ACTIVATION = 61;
31  public const TYPE_POST_UPDATED = 62;
32  public const TYPE_POST_CENSORED = 63;
33  public const TYPE_POST_DELETED = 64;
34  public const TYPE_POST_ANSWERED = 65;
35  public const TYPE_POST_UNCENSORED = 66;
36 
37  private bool $is_cronjob = false;
39  private ilLogger $logger;
40 
41  public function __construct(ilForumNotificationMailData $provider, ilLogger $logger)
42  {
43  parent::__construct(false);
44  $this->provider = $provider;
45  $this->logger = $logger;
46  }
47 
48  protected function initMail(): ilMail
49  {
50  $mail = parent::initMail();
51  $this->logger->debug('Initialized mail service');
52  return $mail;
53  }
54 
55  public function sendMail(array $a_rcp, bool $a_parse_recipients = true): void
56  {
57  $this->logger->debug(sprintf(
58  'Delegating notification transport to mail service for recipients: %s',
59  print_r($a_rcp, true)
60  ));
61  parent::sendMail($a_rcp, $a_parse_recipients);
62  $this->logger->debug('Notification transport delegated');
63  }
64 
65  protected function setSubject(string $a_subject): string
66  {
67  $value = parent::setSubject($a_subject);
68  $this->logger->debug(sprintf('Setting subject to: %s', $a_subject));
69  return $value;
70  }
71 
72  protected function appendAttachments(): void
73  {
74  if (count($this->provider->getAttachments()) > 0) {
75  $this->logger->debug('Adding attachments ...');
76  foreach ($this->provider->getAttachments() as $attachment) {
77  $this->appendBody($this->getLanguageText('attachment') . ": " . $attachment . "\n");
78  }
79  $this->appendBody("\n------------------------------------------------------------\n");
80  $this->setAttachments($this->provider->getAttachments());
81  }
82  }
83 
84  public function send(): bool
85  {
86  global $DIC;
87  $ilSetting = $DIC->settings();
88  $lng = $DIC->language();
89 
90  if (!$ilSetting->get('forum_notification', '0')) {
91  $this->logger->debug('Forum notifications are globally disabled');
92  return false;
93  }
94 
95  if (!$this->getRecipients()) {
96  $this->logger->debug('No notification recipients, nothing to do');
97  return false;
98  }
99 
100  $lng->loadLanguageModule('forum');
101 
104 
105  switch ($this->getType()) {
106  case self::TYPE_THREAD_DELETED:
107  foreach ($this->getRecipients() as $rcp) {
108  $this->initLanguage($rcp);
109  $customText = sprintf(
110  $this->getLanguageText('thread_deleted_by'),
111  $this->provider->getDeletedBy(),
112  $this->provider->getForumTitle()
113  );
115  'frm_noti_subject_del_thread',
116  (int) $rcp,
117  $customText,
118  'content_deleted_thread'
119  );
120  }
121  break;
122 
123  case self::TYPE_POST_NEW:
124  foreach ($this->getRecipients() as $rcp) {
125  $this->initLanguage($rcp);
126  $customText = sprintf(
127  $this->getLanguageText('frm_noti_new_post'),
128  $this->provider->getForumTitle()
129  );
130  $this->sendMailWithAttachments('frm_noti_subject_new_post', (int) $rcp, $customText, 'new_post');
131  }
132  break;
133 
134  case self::TYPE_POST_ACTIVATION:
135  foreach ($this->getRecipients() as $rcp) {
136  $this->initLanguage($rcp);
137  $customText = $this->getLanguageText('forums_post_activation_mail');
138  $this->sendMailWithAttachments('frm_noti_subject_act_post', (int) $rcp, $customText, 'new_post');
139  }
140  break;
141 
142  case self::TYPE_POST_ANSWERED:
143  foreach ($this->getRecipients() as $rcp) {
144  $this->initLanguage($rcp);
145  $customText = $this->getLanguageText('forum_post_replied');
146  $this->sendMailWithAttachments('frm_noti_subject_answ_post', (int) $rcp, $customText, 'new_post');
147  }
148  break;
149 
150  case self::TYPE_POST_UPDATED:
151  foreach ($this->getRecipients() as $rcp) {
152  $this->initLanguage($rcp);
153  $customText = sprintf(
154  $this->getLanguageText('post_updated_by'),
155  $this->provider->getPostUpdateUserName($this->getLanguage()),
156  $this->provider->getForumTitle()
157  );
159  'frm_noti_subject_upt_post',
160  (int) $rcp,
161  $customText,
162  'content_post_updated',
163  $this->provider->getPostUpdate()
164  );
165  }
166  break;
167 
168  case self::TYPE_POST_CENSORED:
169  foreach ($this->getRecipients() as $rcp) {
170  $this->initLanguage($rcp);
171  $customText = sprintf(
172  $this->getLanguageText('post_censored_by'),
173  $this->provider->getPostUpdateUserName($this->getLanguage()),
174  $this->provider->getForumTitle()
175  );
177  'frm_noti_subject_cens_post',
178  (int) $rcp,
179  $customText,
180  'content_censored_post',
181  $this->provider->getPostCensoredDate()
182  );
183  }
184  break;
185 
186  case self::TYPE_POST_UNCENSORED:
187  foreach ($this->getRecipients() as $rcp) {
188  $this->initLanguage($rcp);
189  $customText = sprintf(
190  $this->getLanguageText('post_uncensored_by'),
191  $this->provider->getPostUpdateUserName($this->getLanguage())
192  );
194  'frm_noti_subject_uncens_post',
195  (int) $rcp,
196  $customText,
197  'forums_the_post',
198  $this->provider->getPostCensoredDate()
199  );
200  }
201  break;
202 
203  case self::TYPE_POST_DELETED:
204  foreach ($this->getRecipients() as $rcp) {
205  $this->initLanguage($rcp);
206  $customText = sprintf(
207  $this->getLanguageText('post_deleted_by'),
208  $this->provider->getDeletedBy(),
209  $this->provider->getForumTitle()
210  );
212  'frm_noti_subject_del_post',
213  (int) $rcp,
214  $customText,
215  'content_deleted_post'
216  );
217  }
218  break;
219  }
220 
223 
224  return true;
225  }
226 
227  protected function initLanguage(int $a_usr_id): void
228  {
229  parent::initLanguage($a_usr_id);
230  $this->language->loadLanguageModule('forum');
231  }
232 
233  public function isCronjob(): bool
234  {
235  return $this->is_cronjob;
236  }
237 
238  public function setIsCronjob(bool $is_cronjob): void
239  {
240  $this->is_cronjob = $is_cronjob;
241  }
242 
243  private function getPermanentLink(string $type = self::PERMANENT_LINK_POST): string
244  {
245  global $DIC;
246 
247  $ilClientIniFile = $DIC['ilClientIniFile'];
248 
249  if ($type === self::PERMANENT_LINK_FORUM) {
250  $language_text = $this->getLanguageText("forums_notification_show_frm");
251  $forum_parameters = $this->provider->getRefId();
252  } else {
253  $language_text = $this->getLanguageText("forums_notification_show_post");
254  $forum_parameters = $this->provider->getRefId() . "_" . $this->provider->getThreadId() . "_" . $this->provider->getPostId();
255  }
256 
257  $this->logger->debug(sprintf(
258  'Building permanent with parameters %s',
259  $forum_parameters
260  ));
261 
262  $posting_link = sprintf(
263  $language_text,
264  ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
265  ) . "\n\n";
266 
267  $posting_link .= sprintf(
268  $this->getLanguageText("forums_notification_intro"),
269  $ilClientIniFile->readVariable("client", "name"),
270  ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
271  ) . "\n\n";
272 
273  $this->logger->debug(sprintf(
274  'Link built: %s',
275  $posting_link
276  ));
277 
278  return $posting_link;
279  }
280 
281  private function getPostMessage(): string
282  {
283  $pos_message = $this->provider->getPostMessage();
284  if (strip_tags($pos_message) !== $pos_message) {
285  $pos_message = preg_replace("/\n/i", "", $pos_message);
286  $pos_message = preg_replace("/<li([^>]*)>/i", "\n<li$1>", $pos_message);
287  $pos_message = preg_replace("/<\/ul([^>]*)>(?!\s*?(<p|<ul))/i", "</ul$1>\n", $pos_message);
288  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
289  $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
290  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
291 
292  return $pos_message;
293  }
294 
295  return $pos_message;
296  }
297 
298  private function sendMailWithAttachments(
299  string $subjectLanguageId,
300  int $userId,
301  string $customText,
302  string $action,
303  string $date = ''
304  ): void {
305  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
306  $this->appendAttachments();
307  $this->addLinkToMail();
308  $this->sendMail([$userId]);
309  }
310 
311  private function sendMailWithoutAttachments(
312  string $subjectLanguageId,
313  int $userId,
314  string $customText,
315  string $action,
316  ?string $date = null
317  ): void {
318  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
319  $this->addLinkToMail();
320  $this->sendMail([$userId]);
321  }
322 
323  private function createMail(
324  string $subject,
325  int $userId,
326  string $customText,
327  string $action,
328  ?string $date
329  ): void {
330  if (is_string($date)) {
331  $date = $this->createMailDate($date);
332  }
333 
334  $this->addMailSubject($subject);
335 
336  $this->setBody(ilMail::getSalutation($userId, $this->getLanguage()));
337  $this->appendBody("\n\n");
338  $this->appendBody($customText);
339  $this->appendBody("\n\n");
340  $this->appendBody($this->getLanguageText('forum') . ": " . $this->provider->getForumTitle());
341  $this->appendBody("\n\n");
342  if ($this->provider->providesClosestContainer()) {
343  $this->appendBody(
344  $this->getLanguageText('frm_noti_obj_' . $this->provider->closestContainer()->getType()) . ": " .
345  $this->provider->closestContainer()->getTitle()
346  );
347  $this->appendBody("\n\n");
348  }
349  $this->appendBody($this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle());
350  $this->appendBody("\n\n");
351  $this->appendBody($this->getLanguageText($action) . ": \n------------------------------------------------------------\n");
352 
353  $this->appendBody($this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage()));
354  $this->appendBody("\n");
355  if (is_string($date) && $date !== '') {
356  $this->appendBody($this->getLanguageText('date') . ": " . $date);
357  $this->appendBody("\n");
358  }
359  $this->appendBody($this->getLanguageText('subject') . ": " . $this->provider->getPostTitle());
360  $this->appendBody("\n");
361  $this->appendBody($this->getLanguageText('frm_noti_message'));
362  $this->appendBody("\n");
363 
364  $message = strip_tags($this->getPostMessage());
365 
366  if ($this->provider->isPostCensored()) {
367  $message = $this->provider->getCensorshipComment();
368  }
369 
370  $this->appendBody($message . "\n");
371  $this->appendBody("------------------------------------------------------------\n");
372  }
373 
374  private function addMailSubject(string $subject): void
375  {
376  $this->initMail();
377 
378  $container_text = '';
379  if ($this->provider->providesClosestContainer()) {
380  $container_text = " (" .
381  $this->getLanguageText('obj_' . $this->provider->closestContainer()->getType()) .
382  " \"" . $this->provider->closestContainer()->getTitle() . "\")";
383  }
384 
385  $this->setSubject(sprintf(
386  $this->getLanguageText($subject),
387  $this->provider->getForumTitle(),
388  $container_text,
389  $this->provider->getThreadTitle()
390  ));
391  }
392 
393  private function createMailDate(string $date): string
394  {
396 
397  if ($date === '') {
398  $date = $this->provider->getPostDate();
399  }
400 
402  }
403 
404  private function addLinkToMail(): void
405  {
406  $this->appendBody($this->getPermanentLink());
408  }
409 }
const IL_CAL_DATETIME
$lng
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
getPermanentLink(string $type=self::PERMANENT_LINK_POST)
createMail(string $subject, int $userId, string $customText, string $action, ?string $date)
static setLanguage(ilLanguage $a_lng)
sendMailWithAttachments(string $subjectLanguageId, int $userId, string $customText, string $action, string $date='')
global $DIC
Definition: feed.php:28
Base class for course/group mail notifications.
ilForumNotificationMailData $provider
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
const CLIENT_ID
Definition: constants.php:41
getLanguageText(string $a_keyword)
sendMailWithoutAttachments(string $subjectLanguageId, int $userId, string $customText, string $action, ?string $date=null)
Interface ilForumNotificationMailData.
static _getHttpPath()
global $ilSetting
Definition: privfeed.php:17
__construct(Container $dic, ilPlugin $plugin)
$message
Definition: xapiexit.php:32
sendMail(array $a_rcp, bool $a_parse_recipients=true)
static setUseRelativeDates(bool $a_status)
set use relative dates
__construct(ilForumNotificationMailData $provider, ilLogger $logger)
static _getInstallationSignature()