ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilForumMailEventNotificationSender.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 private const int TYPE_THREAD_DELETED = 54;
27 private const int TYPE_POST_NEW = 60;
28 private const int TYPE_POST_ACTIVATION = 61;
29 private const int TYPE_POST_UPDATED = 62;
30 private const int TYPE_POST_CENSORED = 63;
31 private const int TYPE_POST_DELETED = 64;
32 private const int TYPE_POST_ANSWERED = 65;
33 private const int TYPE_POST_UNCENSORED = 66;
34 private const string PERMANENT_LINK_POST = 'PL_Post';
35 private const string PERMANENT_LINK_FORUM = 'PL_Forum';
36 protected bool $is_cronjob = false;
37
38 public function __construct(protected ilForumNotificationMailData $provider, protected ilLogger $logger)
39 {
41 }
42
43 protected function initMail(): ilMail
44 {
45 $mail = parent::initMail();
46 $this->logger->debug('Initialized mail service');
47
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 recipient "%s" ...',
55 json_encode($a_rcp, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
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
66 return $value;
67 }
68
69 public function send(): bool
70 {
71 global $DIC;
72 $ilSetting = $DIC->settings();
73 $lng = $DIC->language();
74
75 if (!$ilSetting->get('forum_notification', '0')) {
76 $this->logger->debug('Forum notifications are globally disabled');
77
78 return false;
79 }
80
81 if (!$this->getRecipients()) {
82 $this->logger->debug('No notification recipients, nothing to do');
83
84 return false;
85 }
86
87 $lng->loadLanguageModule('forum');
88
91
92 $mailObjects = [];
93
94 switch ($this->getType()) {
96 foreach ($this->getRecipients() as $rcp) {
97 $this->initLanguage($rcp);
98 $customText = sprintf(
99 $this->getLanguageText('thread_deleted_by'),
100 $this->provider->getDeletedBy(),
101 $this->provider->getForumTitle()
102 );
103
104 $mailObjects[] = $this->createMailValueObjectWithoutAttachments(
105 'frm_noti_subject_del_thread',
106 (int) $rcp,
107 $customText,
108 'content_deleted_thread'
109 );
110 }
111 break;
112
114 foreach ($this->getRecipients() as $rcp) {
115 $this->initLanguage($rcp);
116 $customText = sprintf(
117 $this->getLanguageText('frm_noti_new_post'),
118 $this->provider->getForumTitle()
119 );
120
121 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
122 'frm_noti_subject_new_post',
123 (int) $rcp,
124 $customText,
125 'new_post'
126 );
127 }
128 break;
129
131 foreach ($this->getRecipients() as $rcp) {
132 $this->initLanguage($rcp);
133 $customText = $this->getLanguageText('forums_post_activation_mail');
134
135 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
136 'frm_noti_subject_act_post',
137 (int) $rcp,
138 $customText,
139 'new_post'
140 );
141 }
142 break;
143
145 foreach ($this->getRecipients() as $rcp) {
146 $this->initLanguage($rcp);
147 $customText = $this->getLanguageText('forum_post_replied');
148
149 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
150 'frm_noti_subject_answ_post',
151 (int) $rcp,
152 $customText,
153 'new_post'
154 );
155 }
156 break;
157
159 foreach ($this->getRecipients() as $rcp) {
160 $this->initLanguage($rcp);
161 $customText = sprintf(
162 $this->getLanguageText('post_updated_by'),
163 $this->provider->getPostUpdateUserName($this->getLanguage()),
164 $this->provider->getForumTitle()
165 );
166 $date = $this->provider->getPostUpdate();
167
168 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
169 'frm_noti_subject_upt_post',
170 (int) $rcp,
171 $customText,
172 'content_post_updated',
173 $date
174 );
175 }
176 break;
177
179 foreach ($this->getRecipients() as $rcp) {
180 $this->initLanguage($rcp);
181 $customText = sprintf(
182 $this->getLanguageText('post_censored_by'),
183 $this->provider->getPostUpdateUserName($this->getLanguage()),
184 $this->provider->getForumTitle()
185 );
186 $date = $this->provider->getPostCensoredDate();
187
188 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
189 'frm_noti_subject_cens_post',
190 (int) $rcp,
191 $customText,
192 'content_censored_post',
193 $date
194 );
195 }
196 break;
197
199 foreach ($this->getRecipients() as $rcp) {
200 $this->initLanguage($rcp);
201 $customText = sprintf(
202 $this->getLanguageText('post_uncensored_by'),
203 $this->provider->getPostUpdateUserName($this->getLanguage())
204 );
205 $date = $this->provider->getPostCensoredDate();
206
207 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
208 'frm_noti_subject_uncens_post',
209 (int) $rcp,
210 $customText,
211 'forums_the_post',
212 $date
213 );
214 }
215 break;
216
218 foreach ($this->getRecipients() as $rcp) {
219 $this->initLanguage($rcp);
220 $customText = sprintf(
221 $this->getLanguageText('post_deleted_by'),
222 $this->provider->getDeletedBy(),
223 $this->provider->getForumTitle()
224 );
225
226 $mailObjects[] = $this->createMailValueObjectWithoutAttachments(
227 'frm_noti_subject_del_post',
228 (int) $rcp,
229 $customText,
230 'content_deleted_post'
231 );
232 }
233 break;
234 }
235
236 $contextId = ilMailFormCall::getContextId();
237 if (null === $contextId) {
238 $contextId = '';
239 }
240
241 $contextParameters = ilMailFormCall::getContextParameters();
242
243 $processor = new ilMassMailTaskProcessor();
244
245 $processor->run(
246 $mailObjects,
248 $contextId,
249 $contextParameters
250 );
251
254
255 return true;
256 }
257
258 protected function initLanguage(int $a_usr_id): void
259 {
260 parent::initLanguage($a_usr_id);
261 $this->language->loadLanguageModule('forum');
262 }
263
264 public function isCronjob(): bool
265 {
266 return $this->is_cronjob;
267 }
268
269 public function setIsCronjob(bool $is_cronjob): void
270 {
271 $this->is_cronjob = $is_cronjob;
272 }
273
274 private function getPermanentLink(string $type = self::PERMANENT_LINK_POST): string
275 {
276 global $DIC;
277 $ilClientIniFile = $DIC['ilClientIniFile'];
278
279 if ($type === self::PERMANENT_LINK_FORUM) {
280 $language_text = $this->getLanguageText("forums_notification_show_frm");
281 $forum_parameters = $this->provider->getRefId();
282 } else {
283 $language_text = $this->getLanguageText("forums_notification_show_post");
284 $forum_parameters = $this->provider->getRefId() . "_" . $this->provider->getThreadId() . "_" . $this->provider->getPostId();
285 }
286
287 $this->logger->debug(sprintf(
288 'Building permanent with parameters %s',
289 $forum_parameters
290 ));
291
292 $posting_link = sprintf(
293 $language_text,
294 ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
295 ) . "\n\n";
296 $posting_link .= sprintf(
297 $this->getLanguageText("forums_notification_intro"),
298 $ilClientIniFile->readVariable("client", "name"),
299 ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
300 ) . "\n\n";
301
302 $this->logger->debug(sprintf(
303 'Link built: %s',
304 $posting_link
305 ));
306
307 return $posting_link;
308 }
309
310 private function getPostMessage(): string
311 {
312 $pos_message = $this->provider->getPostMessage() ?? '';
313 if (strip_tags($pos_message) !== $pos_message) {
314 $pos_message = preg_replace("/\n/i", "", $pos_message);
315 $pos_message = preg_replace("/<li([^>]*)>/i", "\n<li$1>", $pos_message);
316 $pos_message = preg_replace("/<\/ul([^>]*)>(?!\s*?(<p|<ul))/i", "</ul$1>\n", $pos_message);
317 $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
318 $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
319 return preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
320 }
321
322 return $pos_message;
323 }
324
334 string $subjectLanguageId,
335 int $recipientUserId,
336 string $customText,
337 string $action,
338 string $date = ''
340 $subjectText = $this->createSubjectText($subjectLanguageId);
341
342 $bodyText = $this->createMailBodyText(
343 $subjectLanguageId,
344 $recipientUserId,
345 $customText,
346 $action,
347 $date
348 );
349
350 $attachmentText = $this->createAttachmentText();
351 $bodyText .= $attachmentText;
352
353 $attachmentText = $this->createAttachmentLinkText();
354 $bodyText .= $attachmentText;
355
356 return new ilMailValueObject(
357 '',
358 ilObjUser::_lookupLogin($recipientUserId),
359 '',
360 '',
361 ilStr::strLen($subjectText) > 255 ? ilStr::substr($subjectText, 0, 255) : $subjectText,
362 $bodyText,
363 $this->provider->getAttachments(),
364 false,
365 false
366 );
367 }
368
373 string $subjectLanguageId,
374 int $recipientUserId,
375 string $customText,
376 string $action,
377 string $date = ''
379 $subjectText = $this->createSubjectText($subjectLanguageId);
380
381 $bodyText = $this->createMailBodyText(
382 $subjectLanguageId,
383 $recipientUserId,
384 $customText,
385 $action,
386 $date
387 );
388
389 return new ilMailValueObject(
390 '',
391 ilObjUser::_lookupLogin($recipientUserId),
392 '',
393 '',
394 ilStr::strLen($subjectText) > 255 ? ilStr::substr($subjectText, 0, 255) : $subjectText,
395 $bodyText,
396 [],
397 false,
398 false
399 );
400 }
401
402 private function createMailBodyText(
403 string $subject,
404 int $userId,
405 string $customText,
406 string $action,
407 string $date
408 ): string {
409 $date = $this->createMailDate($date);
410
411 $this->addMailSubject($subject);
412
413 $body = ilMail::getSalutation($userId, $this->getLanguage());
414
415 $body .= "\n\n";
416 $body .= $customText;
417 $body .= "\n\n";
418 $body .= $this->getLanguageText('forum') . ": " . $this->provider->getForumTitle();
419 $body .= "\n\n";
420 if ($this->provider->providesClosestContainer()) {
421 $body .= $this->getLanguageText('obj_' . $this->provider->closestContainer()->getType()) . ": " . $this->provider->closestContainer()->getTitle();
422 $body .= "\n\n";
423 }
424 $body .= $this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle();
425 $body .= "\n\n";
426 $body .= $this->getLanguageText($action) . ": \n------------------------------------------------------------\n";
427
428 $body .= $this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage());
429 $body .= "\n";
430 $body .= $this->getLanguageText('date') . ": " . $date;
431 $body .= "\n";
432 $body .= $this->getLanguageText('subject') . ": " . $this->provider->getPostTitle();
433 $body .= "\n";
434 $body .= $this->getLanguageText('frm_noti_message');
435 $body .= "\n";
436
437 $message = strip_tags($this->getPostMessage());
438
439 if ($this->provider->isPostCensored()) {
440 $message = $this->provider->getCensorshipComment();
441 }
442
443 $body .= $message . "\n";
444
445 return $body . "------------------------------------------------------------\n";
446 }
447
448 private function createAttachmentText(): string
449 {
450 $attachmentText = '';
451 if ($this->provider->getAttachments() !== []) {
452 $this->logger->debug('Adding attachments ...');
453 foreach ($this->provider->getAttachments() as $attachment) {
454 $attachmentText .= $this->getLanguageText('attachment') . ": " . $attachment . "\n";
455 }
456 $attachmentText .= "\n------------------------------------------------------------\n";
457 }
458
459 return $attachmentText;
460 }
461
462 private function createAttachmentLinkText(): string
463 {
464 $body = $this->getPermanentLink();
465
466 return $body . ilMail::_getInstallationSignature();
467 }
468
469 private function addMailSubject(string $subject): void
470 {
471 $this->initMail();
472
473 $this->setSubject($this->createSubjectText($subject));
474 }
475
476 private function createMailDate(string $date): string
477 {
479
480 if ($date === '') {
481 $date = $this->provider->getPostDate();
482 }
483
485 }
486
487 private function createSubjectText(string $subject): string
488 {
489 $container_text = '';
490 if ($this->provider->providesClosestContainer()) {
491 $container_text = " (" . $this->getLanguageText('frm_noti_obj_' . $this->provider->closestContainer()->getType()) .
492 " \"" . $this->provider->closestContainer()->getTitle() . "\")";
493 }
494
495 return sprintf(
496 $this->getLanguageText($subject),
497 $this->provider->getForumTitle(),
498 $container_text,
499 $this->provider->getThreadTitle()
500 );
501 }
502}
const IL_CAL_DATETIME
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
static setLanguage(ilLanguage $a_lng)
@classDescription Date and time handling
createMailBodyText(string $subject, int $userId, string $customText, string $action, string $date)
createMailValueObjectsWithAttachments(string $subjectLanguageId, int $recipientUserId, string $customText, string $action, string $date='')
Add body and send mail with attachments.
createMailValueObjectWithoutAttachments(string $subjectLanguageId, int $recipientUserId, string $customText, string $action, string $date='')
Add body and send mail without attachments.
sendMail(array $a_rcp, bool $a_parse_recipients=true)
__construct(protected ilForumNotificationMailData $provider, protected ilLogger $logger)
getPermanentLink(string $type=self::PERMANENT_LINK_POST)
Component logger with individual log levels by component id.
getLanguageText(string $a_keyword)
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
static _getInstallationSignature()
static _lookupLogin(int $a_user_id)
static strLen(string $a_string)
Definition: class.ilStr.php:60
static _getHttpPath()
const CLIENT_ID
Definition: constants.php:41
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilForumNotificationMailData.
$provider
Definition: ltitoken.php:80
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $lng
Definition: privfeed.php:31
global $ilSetting
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
getLanguage()
$message
Definition: xapiexit.php:31