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