ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailNotification.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 abstract class ilMailNotification
22 {
23  final public const int SUBJECT_TITLE_LENGTH = 60;
24 
25  protected int $type;
26  protected int $sender;
27  protected ?ilMail $mail = null;
28  protected string $subject = '';
29  protected string $body = '';
30  protected array $attachments = [];
31  protected ilLanguage $language;
32  protected array $lang_modules = [];
33  protected array $recipients = [];
34  protected int $ref_id;
35  protected int $obj_id = 0;
36  protected string $obj_type = '';
37  protected array $additional_info = [];
40 
41  public function __construct(protected bool $is_in_wsp = false)
42  {
43  global $DIC;
45  $this->language = ilLanguageFactory::_getLanguage($DIC->language()->getDefaultLanguage());
46 
47  if ($this->is_in_wsp) {
48  $this->wsp_tree = new ilWorkspaceTree($DIC->user()->getId()); // owner of tree is irrelevant
49  $this->wsp_access_handler = new ilWorkspaceAccessHandler($this->wsp_tree);
50  }
51  }
52 
53  public function setType(int $a_type): void
54  {
55  $this->type = $a_type;
56  }
57 
58  public function getType(): int
59  {
60  return $this->type;
61  }
62 
63  public function setSender(int $a_usr_id): void
64  {
65  $this->sender = $a_usr_id;
66  }
67 
68  public function getSender(): int
69  {
70  return $this->sender;
71  }
72 
73  protected function setSubject(string $a_subject): string
74  {
75  if (ilStr::strLen($a_subject) > 255) {
76  $a_subject = ilStr::subStr($a_subject, 0, 255);
77  }
78 
79  return $this->subject = $a_subject;
80  }
81 
82  protected function getSubject(): string
83  {
84  return $this->subject;
85  }
86 
87  protected function setBody(string $a_body): void
88  {
89  $this->body = $a_body;
90  }
91 
92  protected function appendBody(string $a_body): string
93  {
94  return $this->body .= $a_body;
95  }
96 
97  protected function getBody(): string
98  {
99  return $this->body;
100  }
101 
102  public function setRecipients(array $a_rcp): void
103  {
104  $this->recipients = $a_rcp;
105  }
106 
107  public function getRecipients(): array
108  {
109  return $this->recipients;
110  }
111 
112  public function setAttachments(array $a_att): void
113  {
114  $this->attachments = $a_att;
115  }
116 
117  public function getAttachments(): array
118  {
119  return $this->attachments;
120  }
121 
122  public function setLangModules(array $a_modules): void
123  {
124  $this->lang_modules = $a_modules;
125  }
126 
127  protected function initLanguage(int $a_usr_id): void
128  {
129  $this->language = $this->getUserLanguage($a_usr_id);
130  }
131 
132  public function getUserLanguage(int $a_usr_id): ilLanguage
133  {
134  $language = ilLanguageFactory::_getLanguageOfUser($a_usr_id);
135  $language->loadLanguageModule('mail');
136 
137  foreach ($this->lang_modules as $lmod) {
138  $language->loadLanguageModule($lmod);
139  }
140 
141  return $language;
142  }
143 
144  protected function initLanguageByIso2Code(string $a_code = ''): void
145  {
146  $this->language = ilLanguageFactory::_getLanguage($a_code);
147  $this->language->loadLanguageModule('mail');
148 
149  foreach ($this->lang_modules as $lmod) {
150  $this->language->loadLanguageModule($lmod);
151  }
152  }
153 
154  protected function setLanguage(ilLanguage $a_language): void
155  {
156  $this->language = $a_language;
157  }
158 
159  protected function getLanguage(): ilLanguage
160  {
161  return $this->language;
162  }
163 
164  protected function getLanguageText(string $a_keyword): string
165  {
166  return str_replace('\n', "\n", $this->getLanguage()->txt($a_keyword));
167  }
168 
169  public function setRefId(int $a_id): void
170  {
171  if (!$this->is_in_wsp) {
172  $this->ref_id = $a_id;
173  $obj_id = ilObject::_lookupObjId($this->ref_id);
174  } else {
175  $this->ref_id = $a_id;
176  $obj_id = $this->wsp_tree->lookupObjectId($this->getRefId());
177  }
178 
179  $this->setObjId($obj_id);
180  }
181 
182  public function getRefId(): int
183  {
184  return $this->ref_id;
185  }
186 
187  public function getObjId(): int
188  {
189  return $this->obj_id;
190  }
191 
192  public function setObjId(int $a_obj_id): void
193  {
194  $this->obj_id = $a_obj_id;
195  $this->obj_type = ilObject::_lookupType($this->obj_id);
196  }
197 
198  public function getObjType(): string
199  {
200  return $this->obj_type;
201  }
202 
203  public function setAdditionalInformation(array $a_info): void
204  {
205  $this->additional_info = $a_info;
206  }
207 
208  public function getAdditionalInformation(): array
209  {
210  return $this->additional_info;
211  }
212 
213  protected function getObjectTitle(bool $a_shorten = false): string
214  {
215  if ($this->getObjId() === 0) {
216  return '';
217  }
219  if ($a_shorten) {
220  $txt = ilStr::shortenTextExtended($txt, self::SUBJECT_TITLE_LENGTH, true);
221  }
222  return $txt;
223  }
224 
225  public function sendMail(array $a_rcp, bool $a_parse_recipients = true): void
226  {
227  $recipients = [];
228  foreach ($a_rcp as $rcp) {
229  if ($a_parse_recipients) {
230  $recipients[] = ilObjUser::_lookupLogin((int) $rcp);
231  } else {
232  $recipients[] = $rcp;
233  }
234  }
235  $recipients = implode(',', $recipients);
236  $errors = $this->getMail()->enqueue(
237  $recipients,
238  '',
239  '',
240  $this->getSubject(),
241  $this->getBody(),
242  $this->getAttachments()
243  );
244  if ($errors !== []) {
245  ilLoggerFactory::getLogger('mail')->dump($errors, ilLogLevel::ERROR);
246  }
247  }
248 
249  protected function initMail(): ilMail
250  {
251  return $this->mail = new ilMail($this->getSender());
252  }
253 
254  protected function getMail(): ilMail
255  {
256  return is_object($this->mail) ? $this->mail : $this->initMail();
257  }
258 
259  protected function createPermanentLink(array $a_params = [], string $a_append = ''): ?string
260  {
261  if ($this->getRefId() !== 0) {
262  if (!$this->is_in_wsp) {
263  return ilLink::_getLink($this->ref_id, $this->getObjType(), $a_params, $a_append);
264  }
265  return ilWorkspaceAccessHandler::getGotoLink($this->getRefId(), $this->getObjId(), $a_append);
266  }
267  return ilLink::_getLink(ROOT_FOLDER_ID, 'root');
268  }
269 
270  protected function userToString(int $a_usr_id): string
271  {
272  $name = ilObjUser::_lookupName($a_usr_id);
273  return ($name['title'] ? $name['title'] . ' ' : '') .
274  ($name['firstname'] ? $name['firstname'] . ' ' : '') .
275  ($name['lastname'] ? $name['lastname'] . ' ' : '');
276  }
277 
278  protected function isRefIdAccessible(int $a_user_id, int $a_ref_id, string $a_permission = 'read'): bool
279  {
280  global $DIC;
281 
282  // no given permission == accessible
283 
284  if (!$this->is_in_wsp) {
285  if (trim($a_permission) &&
286  !$DIC->access()->checkAccessOfUser(
287  $a_user_id,
288  $a_permission,
289  '',
290  $a_ref_id,
291  $this->getObjType()
292  )) {
293  return false;
294  }
295  } elseif (
296  trim($a_permission) &&
297  !$this->wsp_access_handler->checkAccessOfUser(
298  $this->wsp_tree,
299  $a_user_id,
300  $a_permission,
301  '',
302  $a_ref_id,
303  $this->getObjType()
304  )
305  ) {
306  return false;
307  }
308 
309  return true;
310  }
311 
312  public function getBlockBorder(): string
313  {
314  return "----------------------------------------\n";
315  }
316 }
final const int SUBJECT_TITLE_LENGTH
__construct(protected bool $is_in_wsp=false)
const ANONYMOUS_USER_ID
Definition: constants.php:27
static getLogger(string $a_component_id)
Get component logger.
sendMail(array $a_rcp, bool $a_parse_recipients=true)
const ROOT_FOLDER_ID
Definition: constants.php:32
isRefIdAccessible(int $a_user_id, int $a_ref_id, string $a_permission='read')
static getGotoLink(int $a_node_id, int $a_obj_id, string $a_additional="")
static _lookupName(int $a_user_id)
lookup user name
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initLanguageByIso2Code(string $a_code='')
loadLanguageModule(string $a_module)
Load language module.
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static strLen(string $a_string)
Definition: class.ilStr.php:60
static _lookupTitle(int $obj_id)
ilWorkspaceAccessHandler $wsp_access_handler
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
setLangModules(array $a_modules)
global $DIC
Definition: shib_login.php:26
static _getLanguage(string $a_lang_key='')
Get language object.
$txt
Definition: error.php:31
getLanguageText(string $a_keyword)
getObjectTitle(bool $a_shorten=false)
createPermanentLink(array $a_params=[], string $a_append='')
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
setSubject(string $a_subject)
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
static _lookupType(int $id, bool $reference=false)
setLanguage(ilLanguage $a_language)
setAdditionalInformation(array $a_info)
static _lookupLogin(int $a_user_id)