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