ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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;
38 
39  public function __construct(
40  private readonly ilForumNotificationMailData $provider,
41  private readonly ilLogger $logger
42  ) {
43  parent::__construct(false);
44  }
45 
46  protected function initMail(): ilMail
47  {
48  $mail = parent::initMail();
49  $this->logger->debug('Initialized mail service');
50  return $mail;
51  }
52 
53  public function sendMail(array $a_rcp, bool $a_parse_recipients = true): void
54  {
55  $this->logger->debug(
56  sprintf(
57  'Delegating notification transport to mail service for recipients: %s',
58  print_r($a_rcp, true)
59  )
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 ($this->provider->getAttachments() !== []) {
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 = implode('_', [
255  $this->provider->getRefId(),
256  $this->provider->getThreadId(),
257  $this->provider->getPostId()
258  ]);
259  }
260 
261  $this->logger->debug(
262  sprintf(
263  'Building permanent with parameters %s',
264  $forum_parameters
265  )
266  );
267 
268  $posting_link = sprintf(
269  $language_text,
270  rtrim(ilUtil::_getHttpPath(), '/') . '/goto.php?target=frm_' . $forum_parameters . '&client_id=' . CLIENT_ID
271  ) . "\n\n";
272 
273  $posting_link .= sprintf(
274  $this->getLanguageText('forums_notification_intro'),
275  $ilClientIniFile->readVariable('client', 'name'),
276  rtrim(ilUtil::_getHttpPath(), '/') . '/?client_id=' . CLIENT_ID
277  ) . "\n\n";
278 
279  $this->logger->debug(
280  sprintf(
281  'Link built: %s',
282  $posting_link
283  )
284  );
285 
286  return $posting_link;
287  }
288 
289  private function getPostMessage(): string
290  {
291  $pos_message = $this->provider->getPostMessage() ?? '';
292  if (strip_tags($pos_message) !== $pos_message) {
293  $pos_message = preg_replace("/\n/i", '', $pos_message);
294  $pos_message = preg_replace('/<li([^>]*)>/i', "\n<li$1>", $pos_message);
295  $pos_message = preg_replace("/<\/ul([^>]*)>(?!\s*?(<p|<ul))/i", "</ul$1>\n", $pos_message);
296  $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
297  $pos_message = preg_replace('/<p([^>]*)>/i', "\n\n", $pos_message);
298  $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
299 
300  return $pos_message;
301  }
302 
303  return $pos_message;
304  }
305 
306  private function sendMailWithAttachments(
307  string $subjectLanguageId,
308  int $userId,
309  string $customText,
310  string $action,
311  string $date = ''
312  ): void {
313  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
314  $this->appendAttachments();
315  $this->addLinkToMail();
316  $this->sendMail([$userId]);
317  }
318 
319  private function sendMailWithoutAttachments(
320  string $subjectLanguageId,
321  int $userId,
322  string $customText,
323  string $action,
324  ?string $date = null
325  ): void {
326  $this->createMail($subjectLanguageId, $userId, $customText, $action, $date);
327  $this->addLinkToMail();
328  $this->sendMail([$userId]);
329  }
330 
331  private function createMail(
332  string $subject,
333  int $userId,
334  string $customText,
335  string $action,
336  ?string $date
337  ): void {
338  if (is_string($date)) {
339  $date = $this->createMailDate($date);
340  }
341 
342  $this->addMailSubject($subject);
343 
344  $this->setBody(ilMail::getSalutation($userId, $this->getLanguage()));
345  $this->appendBody("\n\n");
346  $this->appendBody($customText);
347  $this->appendBody("\n\n");
348  $this->appendBody($this->getLanguageText('forum') . ': ' . $this->provider->getForumTitle());
349  $this->appendBody("\n\n");
350  if ($this->provider->providesClosestContainer()) {
351  $this->appendBody(
352  $this->getLanguageText('frm_noti_obj_' . $this->provider->closestContainer()->getType()) . ': ' .
353  $this->provider->closestContainer()->getTitle()
354  );
355  $this->appendBody("\n\n");
356  }
357  $this->appendBody($this->getLanguageText('thread') . ': ' . $this->provider->getThreadTitle());
358  $this->appendBody("\n\n");
359  $this->appendBody(
360  $this->getLanguageText($action) . ": \n------------------------------------------------------------\n"
361  );
362 
363  $this->appendBody(
364  $this->getLanguageText('author') . ': ' . $this->provider->getPostUserName($this->getLanguage())
365  );
366  $this->appendBody("\n");
367  if (is_string($date) && $date !== '') {
368  $this->appendBody($this->getLanguageText('date') . ': ' . $date);
369  $this->appendBody("\n");
370  }
371  $this->appendBody($this->getLanguageText('subject') . ': ' . $this->provider->getPostTitle());
372  $this->appendBody("\n");
373  $this->appendBody($this->getLanguageText('frm_noti_message'));
374  $this->appendBody("\n");
375 
376  $message = strip_tags($this->getPostMessage());
377 
378  if ($this->provider->isPostCensored()) {
379  $message = $this->provider->getCensorshipComment();
380  }
381 
382  $this->appendBody($message . "\n");
383  $this->appendBody("------------------------------------------------------------\n");
384  }
385 
386  private function addMailSubject(string $subject): void
387  {
388  $this->initMail();
389 
390  $container_text = '';
391  if ($this->provider->providesClosestContainer()) {
392  $container_text = ' (' .
393  $this->getLanguageText('obj_' . $this->provider->closestContainer()->getType()) .
394  ' "' . $this->provider->closestContainer()->getTitle() . '")';
395  }
396 
397  $this->setSubject(
398  sprintf(
399  $this->getLanguageText($subject),
400  $this->provider->getForumTitle(),
401  $container_text,
402  $this->provider->getThreadTitle()
403  )
404  );
405  }
406 
407  private function createMailDate(string $date): string
408  {
410 
411  if ($date === '') {
412  $date = $this->provider->getPostDate();
413  }
414 
416  }
417 
418  private function addLinkToMail(): void
419  {
420  $this->appendBody($this->getPermanentLink());
422  }
423 }
static array static setUseRelativeDates(bool $a_status)
set use relative dates
const IL_CAL_DATETIME
getPermanentLink(string $type=self::PERMANENT_LINK_POST)
createMail(string $subject, int $userId, string $customText, string $action, ?string $date)
__construct(private readonly ilForumNotificationMailData $provider, private readonly ilLogger $logger)
static setLanguage(ilLanguage $a_lng)
sendMailWithAttachments(string $subjectLanguageId, int $userId, string $customText, string $action, string $date='')
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$provider
Definition: ltitoken.php:80
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:22
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:31
__construct(Container $dic, ilPlugin $plugin)
global $lng
Definition: privfeed.php:31
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
$message
Definition: xapiexit.php:31
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
sendMail(array $a_rcp, bool $a_parse_recipients=true)
static _getInstallationSignature()