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