ILIAS  release_8 Revision v8.24
class.ilMailFolderGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Refinery\Factory as Refinery;
23
31{
32 private bool $confirmTrashDeletion = false;
33 private bool $errorDelete = false;
41 public ilMail $umail;
44 private Refinery $refinery;
45 private int $currentFolderId = 0;
47
48 public function __construct()
49 {
50 global $DIC;
51
52 $this->tpl = $DIC->ui()->mainTemplate();
53 $this->ctrl = $DIC->ctrl();
54 $this->lng = $DIC->language();
55 $this->toolbar = $DIC->toolbar();
56 $this->user = $DIC->user();
57 $this->tabs = $DIC->tabs();
58 $this->http = $DIC->http();
59 $this->refinery = $DIC->refinery();
60 $this->error = $DIC['ilErr'];
61
62 $this->umail = new ilMail($this->user->getId());
63 $this->mbox = new ilMailbox($this->user->getId());
64
65 $this->initFolder();
66 }
67
68 protected function initFolder(): void
69 {
70 if ($this->http->wrapper()->post()->has('mobj_id')) {
71 $folderId = $this->http->wrapper()->post()->retrieve('mobj_id', $this->refinery->kindlyTo()->int());
72 } elseif ($this->http->wrapper()->query()->has('mobj_id')) {
73 $folderId = $this->http->wrapper()->query()->retrieve('mobj_id', $this->refinery->kindlyTo()->int());
74 } else {
75 $folderId = $this->refinery->byTrying([
76 $this->refinery->kindlyTo()->int(),
77 $this->refinery->always($this->currentFolderId),
78 ])->transform(ilSession::get('mobj_id'));
79 }
80
81 if (0 === $folderId || !$this->mbox->isOwnedFolder($folderId)) {
82 $folderId = $this->mbox->getInboxFolder();
83 }
84
85 $this->currentFolderId = $folderId;
86 }
87
88 protected function parseCommand(string $originalCommand): string
89 {
90 if (preg_match('/^([a-zA-Z0-9]+?)_(\d+?)$/', $originalCommand, $matches) && 3 === count($matches)) {
91 $originalCommand = $matches[1];
92 }
93
94 return $originalCommand;
95 }
96
97 protected function parseFolderIdFromCommand(string $command): int
98 {
99 if (
100 preg_match('/^([a-zA-Z0-9]+?)_(\d+?)$/', $command, $matches) &&
101 3 === count($matches) && is_numeric($matches[2])
102 ) {
103 return (int) $matches[2];
104 }
105
106 throw new InvalidArgumentException("Cannot parse a numeric folder id from command string!");
107 }
108
109 public function executeCommand(): void
110 {
111 $cmd = $this->parseCommand(
112 $this->ctrl->getCmd()
113 );
114
115 $nextClass = $this->ctrl->getNextClass($this);
116 switch (strtolower($nextClass)) {
117 case strtolower(ilContactGUI::class):
118 $this->ctrl->forwardCommand(new ilContactGUI());
119 break;
120
121 case strtolower(ilPublicUserProfileGUI::class):
122 $this->tpl->setTitle($this->lng->txt('mail'));
123 $userId = 0;
124 if ($this->http->wrapper()->query()->has('user')) {
125 $userId = $this->http->wrapper()->query()->retrieve('user', $this->refinery->kindlyTo()->int());
126 }
127 $profileGui = new ilPublicUserProfileGUI($userId);
128
129 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
130 $profileGui->setBackUrl($this->ctrl->getLinkTarget($this, 'showMail'));
131 $this->ctrl->clearParameters($this);
132
133 $ret = $this->ctrl->forwardCommand($profileGui);
134 if ($ret !== '') {
135 $this->tpl->setContent($ret);
136 }
137 $this->tpl->printToStdout();
138 break;
139
140 default:
141 if (!method_exists($this, $cmd)) {
142 $cmd = 'showFolder';
143 }
144 $this->{$cmd}();
145 break;
146 }
147 }
148
149 protected function performEmptyTrash(): void
150 {
151 $this->umail->deleteMailsOfFolder($this->currentFolderId);
152
153 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_deleted'), true);
154 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
155 $this->ctrl->redirect($this, 'showFolder');
156 }
157
158 protected function confirmEmptyTrash(): void
159 {
160 if ($this->umail->countMailsOfFolder($this->currentFolderId)) {
161 $this->confirmTrashDeletion = true;
162 }
163
164 $this->showFolder();
165 }
166
170 protected function showUser(): void
171 {
172 $userId = 0;
173 if ($this->http->wrapper()->query()->has('user')) {
174 $userId = $this->http->wrapper()->query()->retrieve('user', $this->refinery->kindlyTo()->int());
175 }
176 $this->tpl->setVariable('TBL_TITLE', implode(' ', [
177 $this->lng->txt('profile_of'),
179 ]));
180 $this->tpl->setVariable('TBL_TITLE_IMG', ilUtil::getImagePath('icon_usr.svg'));
181 $this->tpl->setVariable('TBL_TITLE_IMG_ALT', $this->lng->txt('public_profile'));
182
183 $profile_gui = new ilPublicUserProfileGUI($userId);
184
185 $mailId = 0;
186 if ($this->http->wrapper()->query()->has('mail_id')) {
187 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
188 }
189
190 $this->ctrl->setParameter(
191 $this,
192 'mail_id',
193 $mailId
194 );
195 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
196 $profile_gui->setBackUrl($this->ctrl->getLinkTarget($this, 'showMail'));
197 $this->ctrl->clearParameters($this);
198
199 $this->tpl->setTitle($this->lng->txt('mail'));
200 $this->tpl->setContent($this->ctrl->getHTML($profile_gui));
201 $this->tpl->printToStdout();
202 }
203
204 protected function addSubFolderCommands(bool $isUserSubFolder = false): void
205 {
206 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
207 $this->toolbar->addButton(
208 $this->lng->txt('mail_add_subfolder'),
209 $this->ctrl->getLinkTarget($this, 'addSubFolder')
210 );
211
212 if ($isUserSubFolder) {
213 $this->toolbar->addButton(
214 $this->lng->txt('rename'),
215 $this->ctrl->getLinkTarget($this, 'renameSubFolder')
216 );
217 $this->toolbar->addButton(
218 $this->lng->txt('delete'),
219 $this->ctrl->getLinkTarget($this, 'deleteSubFolder')
220 );
221 }
222 $this->ctrl->clearParameters($this);
223 }
224
225 protected function showFolder(bool $oneConfirmationDialogueRendered = false): void
226 {
227 $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.mail.html', 'Services/Mail');
228 $this->tpl->setTitle($this->lng->txt('mail'));
229
230 $isTrashFolder = $this->currentFolderId === $this->mbox->getTrashFolder();
231
232 if (!$this->errorDelete && $isTrashFolder && 'deleteMails' === $this->parseCommand($this->ctrl->getCmd())) {
233 $confirmationGui = new ilConfirmationGUI();
234 $confirmationGui->setHeaderText($this->lng->txt('mail_sure_delete'));
235 foreach ($this->getMailIdsFromRequest() as $mailId) {
236 $confirmationGui->addHiddenItem('mail_id[]', (string) $mailId);
237 }
238 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
239 $confirmationGui->setFormAction($this->ctrl->getFormAction($this, 'showFolder'));
240 $this->ctrl->clearParameters($this);
241 $confirmationGui->setConfirm($this->lng->txt('confirm'), 'confirmDeleteMails');
242 $confirmationGui->setCancel($this->lng->txt('cancel'), 'showFolder');
243 $this->tpl->setVariable('CONFIRMATION', $confirmationGui->getHTML());
244 $oneConfirmationDialogueRendered = true;
245 }
246
247 $folders = $this->mbox->getSubFolders();
248 $mtree = new ilTree($this->user->getId());
249 $mtree->setTableNames('mail_tree', 'mail_obj_data');
250
251 $isUserSubFolder = false;
252 $isUserRootFolder = false;
253
254 $folder_d = $mtree->getNodeData($this->currentFolderId);
255 if ($folder_d['m_type'] === 'user_folder') {
256 $isUserSubFolder = true;
257 } elseif ($folder_d['m_type'] === 'local') {
258 $isUserRootFolder = true;
259 }
260
261 $mailtable = $this->getMailFolderTable();
262 $mailtable->setSelectedItems($this->getMailIdsFromRequest(true));
263
264 try {
265 $mailtable->prepareHTML();
266 } catch (Exception $e) {
267 $this->tpl->setOnScreenMessage('failure', $this->lng->txt($e->getMessage()) !== '-' . $e->getMessage() . '-' ?
268 $this->lng->txt($e->getMessage()) :
269 $e->getMessage());
270 }
271
272 $table_html = $mailtable->getHTML();
273
274 if ($oneConfirmationDialogueRendered === false && $this->confirmTrashDeletion === false) {
275 $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'showFolder'));
276
277 if ($isUserRootFolder || $isUserSubFolder) {
278 $this->addSubFolderCommands($isUserSubFolder);
279 }
280 }
281
282 if ($this->confirmTrashDeletion && $mailtable->isTrashFolder() && $mailtable->getNumberOfMails() > 0) {
283 $confirmationGui = new ilConfirmationGUI();
284 $confirmationGui->setHeaderText($this->lng->txt('mail_empty_trash_confirmation'));
285 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
286 $confirmationGui->setFormAction($this->ctrl->getFormAction($this, 'performEmptyTrash'));
287 $this->ctrl->clearParameters($this);
288 $confirmationGui->setConfirm($this->lng->txt('confirm'), 'performEmptyTrash');
289 $confirmationGui->setCancel($this->lng->txt('cancel'), 'showFolder');
290 $this->tpl->setVariable('CONFIRMATION', $confirmationGui->getHTML());
291 }
292
293 $this->tpl->setVariable('MAIL_TABLE', $table_html);
294 $this->tpl->printToStdout();
295 }
296
297 protected function deleteSubFolder(bool $a_show_confirm = true): void
298 {
299 if ($a_show_confirm) {
300 $confirmationGui = new ilConfirmationGUI();
301 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
302 $confirmationGui->setFormAction($this->ctrl->getFormAction($this, 'showFolder'));
303 $this->ctrl->clearParameters($this);
304 $confirmationGui->setHeaderText($this->lng->txt('mail_sure_delete_folder'));
305 $confirmationGui->setCancel($this->lng->txt('cancel'), 'showFolder');
306 $confirmationGui->setConfirm($this->lng->txt('confirm'), 'performDeleteSubFolder');
307 $this->tpl->setVariable('CONFIRMATION', $confirmationGui->getHTML());
308
309 $this->showFolder(true);
310 } else {
311 $this->showFolder();
312 }
313 }
314
318 protected function performDeleteSubFolder(): void
319 {
320 $parentFolderId = $this->mbox->getParentFolderId($this->currentFolderId);
321 if ($parentFolderId > 0 && $this->mbox->deleteFolder($this->currentFolderId)) {
322 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_folder_deleted'), true);
323 $this->ctrl->setParameterByClass(ilMailGUI::class, 'mobj_id', $parentFolderId);
324 $this->ctrl->redirectByClass(ilMailGUI::class);
325 } else {
326 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_error_delete'));
327 $this->showFolder();
328 }
329 }
330
331 protected function getSubFolderForm(string $mode = 'create'): ilPropertyFormGUI
332 {
333 $form = new ilPropertyFormGUI();
334 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
335 $form->setFormAction($this->ctrl->getFormAction($this, 'performAddSubFolder'));
336 $this->ctrl->clearParameters($this);
337 if ('edit' === $mode) {
338 $form->addCommandButton('performRenameSubFolder', $this->lng->txt('save'));
339 $form->setTitle($this->lng->txt('mail_rename_folder'));
340 } else {
341 $form->addCommandButton('performAddSubFolder', $this->lng->txt('save'));
342 $form->setTitle($this->lng->txt('mail_add_folder'));
343 }
344 $form->addCommandButton('showFolder', $this->lng->txt('cancel'));
345
346 $title = new ilTextInputGUI($this->lng->txt('title'), 'subfolder_title');
347 $title->setRequired(true);
348 $form->addItem($title);
349
350 return $form;
351 }
352
353 protected function performAddSubFolder(): void
354 {
355 $form = $this->getSubFolderForm();
356 $isFormValid = $form->checkInput();
357 $form->setValuesByPost();
358 if (!$isFormValid) {
359 $this->addSubFolder($form);
360 return;
361 }
362
363 $newFolderId = $this->mbox->addFolder($this->currentFolderId, $form->getInput('subfolder_title'));
364 if ($newFolderId > 0) {
365 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_folder_created'), true);
366 $this->ctrl->setParameterByClass(ilMailGUI::class, 'mobj_id', $newFolderId);
367 $this->ctrl->redirectByClass(ilMailGUI::class);
368 }
369
370 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_folder_exists'));
371 $this->addSubFolder($form);
372 }
373
374 protected function addSubFolder(ilPropertyFormGUI $form = null): void
375 {
376 if (null === $form) {
377 $form = $this->getSubFolderForm();
378 }
379
380 $this->tpl->setTitle($this->lng->txt('mail'));
381 $this->tpl->setContent($form->getHTML());
382 $this->tpl->printToStdout();
383 }
384
385 protected function performRenameSubFolder(): void
386 {
387 $form = $this->getSubFolderForm('edit');
388 $isFormValid = $form->checkInput();
389 $form->setValuesByPost();
390 if (!$isFormValid) {
391 $this->renameSubFolder($form);
392 return;
393 }
394
395 $folderData = $this->mbox->getFolderData($this->currentFolderId);
396 if ($folderData === null) {
397 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_operation_on_invalid_folder'), true);
398 $this->ctrl->setParameterByClass(ilMailGUI::class, 'mobj_id', $this->mbox->getInboxFolder());
399 $this->ctrl->redirectByClass(ilMailGUI::class);
400 }
401
402 if ($folderData['title'] === $form->getInput('subfolder_title')) {
403 $this->showFolder();
404 return;
405 }
406
407 if ($this->mbox->renameFolder($this->currentFolderId, $form->getInput('subfolder_title'))) {
408 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_folder_name_changed'), true);
409 $this->ctrl->setParameterByClass(ilMailGUI::class, 'mobj_id', $this->currentFolderId);
410 $this->ctrl->redirectByClass(ilMailGUI::class);
411 }
412
413 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_folder_exists'));
414 $this->renameSubFolder($form);
415 }
416
417 protected function renameSubFolder(ilPropertyFormGUI $form = null): void
418 {
419 if (null === $form) {
420 $form = $this->getSubFolderForm('edit');
421 $form->setValuesByArray(
422 ['subfolder_title' => $this->mbox->getFolderData($this->currentFolderId)['title'] ?? '']
423 );
424 }
425
426 $this->tpl->setTitle($this->lng->txt('mail'));
427 $this->tpl->setContent($form->getHTML());
428 $this->tpl->printToStdout();
429 }
430
434 protected function getMailIdsFromRequest(bool $ignoreHttpGet = false): array
435 {
436 $mailIds = [];
437 if ($this->http->wrapper()->post()->has('mail_id')) {
438 $mailIds = $this->http->wrapper()->post()->retrieve(
439 'mail_id',
440 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
441 );
442 }
443
444 if ($mailIds === [] && !$ignoreHttpGet) {
445 $mailId = 0;
446 if ($this->http->wrapper()->query()->has('mail_id')) {
447 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
448 }
449 if (is_numeric($mailId)) {
450 $mailIds = [$mailId];
451 }
452 }
453
454 return array_filter(array_map('intval', $mailIds));
455 }
456
457 protected function markMailsRead(): void
458 {
459 $mailIds = $this->getMailIdsFromRequest();
460 if ($mailIds !== []) {
461 $this->umail->markRead($mailIds);
462 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
463 } else {
464 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
465 }
466
467 $this->showFolder();
468 }
469
470 protected function markMailsUnread(): void
471 {
472 $mailIds = $this->getMailIdsFromRequest();
473 if ($mailIds !== []) {
474 $this->umail->markUnread($mailIds);
475 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
476 } else {
477 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
478 }
479
480 $this->showFolder();
481 }
482
483 protected function moveSingleMail(): void
484 {
485 $mailIds = $this->getMailIdsFromRequest();
486 if (1 !== count($mailIds)) {
487 $this->showMail();
488 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
489 return;
490 }
491
492 $newFolderId = 0;
493 if ($this->http->wrapper()->post()->has('folder_id')) {
494 $newFolderId = $this->http->wrapper()->post()->retrieve(
495 'folder_id',
496 $this->refinery->kindlyTo()->int()
497 );
498 }
499 $redirectFolderId = $newFolderId;
500
501 foreach ($mailIds as $mailId) {
502 $mailData = $this->umail->getMail($mailId);
503 if (isset($mailData['folder_id']) &&
504 is_numeric($mailData['folder_id']) &&
505 (int) $mailData['folder_id'] > 0
506 ) {
507 $redirectFolderId = (int) $mailData['folder_id'];
508 break;
509 }
510 }
511
512 if ($this->umail->moveMailsToFolder($mailIds, $newFolderId)) {
513 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_moved'), true);
514 $this->ctrl->setParameter($this, 'mobj_id', $redirectFolderId);
515 $this->ctrl->redirect($this, 'showFolder');
516 } else {
517 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_move_error'));
518 $this->showMail();
519 }
520 }
521
522 protected function moveMails(): void
523 {
524 $mailIds = $this->getMailIdsFromRequest();
525 if ($mailIds === []) {
526 $this->showFolder();
527 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
528 return;
529 }
530
531 $folderId = $this->parseFolderIdFromCommand($this->ctrl->getCmd());
532 if ($this->umail->moveMailsToFolder($mailIds, $folderId)) {
533 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_moved'), true);
534 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
535 $this->ctrl->redirect($this, 'showFolder');
536 } else {
537 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_move_error'));
538 $this->showFolder();
539 }
540 }
541
542 protected function deleteMails(): void
543 {
544 $trashFolderId = $this->mbox->getTrashFolder();
545 $mailIds = $this->getMailIdsFromRequest();
546
547 if ($trashFolderId === $this->currentFolderId) {
548 if ($mailIds === []) {
549 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
550 $this->errorDelete = true;
551 }
552 } elseif ($mailIds === []) {
553 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
554 } elseif ($this->umail->moveMailsToFolder($mailIds, $trashFolderId)) {
555 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_moved_to_trash'), true);
556 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
557 $this->ctrl->redirect($this, 'showFolder');
558 } else {
559 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_move_error'));
560 }
561
562 $this->showFolder();
563 }
564
565 protected function confirmDeleteMails(): void
566 {
567 $mailIds = $this->getMailIdsFromRequest();
568 if ($mailIds === []) {
569 $this->showFolder();
570 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one'));
571 return;
572 }
573
574 if ($this->mbox->getTrashFolder() === $this->currentFolderId) {
575 $this->umail->deleteMails($mailIds);
576 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_deleted'), true);
577 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
578 $this->ctrl->redirect($this, 'showFolder');
579 }
580
581 $this->showFolder();
582 }
583
584 protected function showMail(): void
585 {
586 $mailId = 0;
587 if ($this->http->wrapper()->query()->has('mail_id')) {
588 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
589 }
590
591 if ($mailId <= 0) {
592 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
593 }
594
595 $mailData = $this->umail->getMail($mailId);
596 if ($mailData === null) {
597 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
598 }
599
600 $this->umail->markRead([$mailId]);
601
602 $this->tpl->setTitle($this->lng->txt('mail_mails_of'));
603
604 $this->tabs->clearTargets();
605 $this->ctrl->setParameter($this, 'mobj_id', $mailData['folder_id']);
606 $this->tabs->setBackTarget(
607 $this->lng->txt('back_to_folder'),
608 $this->ctrl->getFormAction($this, 'showFolder')
609 );
610 $this->ctrl->clearParameters($this);
611
612 $this->ctrl->setParameter($this, 'mail_id', $mailId);
613 $this->ctrl->setParameter($this, 'mobj_id', $mailData['folder_id']);
614 $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'showMail'));
615 $this->ctrl->clearParameters($this);
616
617 $form = new ilPropertyFormGUI();
618 $form->setPreventDoubleSubmission(false);
619 $form->setTableWidth('100%');
620 $this->ctrl->setParameter($this, 'mobj_id', $mailData['folder_id']);
621 $this->ctrl->setParameter($this, 'mail_id', $mailId);
622 $form->setFormAction($this->ctrl->getFormAction($this, 'showMail'));
623 $this->ctrl->clearParameters($this);
624 $form->setTitle($this->lng->txt('mail_mails_of'));
625
627 $sender = ilObjectFactory::getInstanceByObjId($mailData['sender_id'], false);
628 $replyBtn = null;
629 if ($sender instanceof ilObjUser && $sender->getId() !== 0 && !$sender->isAnonymous()) {
630 $replyBtn = ilLinkButton::getInstance();
631 $replyBtn->setCaption('reply');
632 $this->ctrl->setParameterByClass(
633 ilMailFormGUI::class,
634 'mobj_id',
635 $mailData['folder_id']
636 );
637 $this->ctrl->setParameterByClass(ilMailFormGUI::class, 'mail_id', $mailId);
638 $this->ctrl->setParameterByClass(ilMailFormGUI::class, 'type', ilMailFormGUI::MAIL_FORM_TYPE_REPLY);
639 $replyBtn->setUrl($this->ctrl->getLinkTargetByClass(ilMailFormGUI::class));
640 $this->ctrl->clearParametersByClass(ilMailFormGUI::class);
641 $replyBtn->setPrimary(true);
642 $this->toolbar->addStickyItem($replyBtn);
643 }
644
645 $fwdBtn = ilLinkButton::getInstance();
646 $fwdBtn->setCaption('forward');
647 $this->ctrl->setParameterByClass(ilMailFormGUI::class, 'mobj_id', $mailData['folder_id']);
648 $this->ctrl->setParameterByClass(ilMailFormGUI::class, 'mail_id', $mailId);
649 $this->ctrl->setParameterByClass(ilMailFormGUI::class, 'type', ilMailFormGUI::MAIL_FORM_TYPE_FORWARD);
650 $fwdBtn->setUrl($this->ctrl->getLinkTargetByClass(ilMailFormGUI::class));
651 $this->ctrl->clearParametersByClass(ilMailFormGUI::class);
652 if (!$replyBtn) {
653 $fwdBtn->setPrimary(true);
654 $this->toolbar->addStickyItem($fwdBtn);
655 } else {
656 $this->toolbar->addButtonInstance($fwdBtn);
657 }
658
659 $printBtn = ilLinkButton::getInstance();
660 $printBtn->setCaption('print');
661 $this->ctrl->setParameter($this, 'mail_id', $mailId);
662 $this->ctrl->setParameter($this, 'mobj_id', $mailData['folder_id']);
663 $printBtn->setUrl($this->ctrl->getLinkTarget($this, 'printMail'));
664 $this->ctrl->clearParameters($this);
665 $printBtn->setTarget('_blank');
666 $this->toolbar->addButtonInstance($printBtn);
667
668 $deleteBtn = ilSubmitButton::getInstance();
669 $deleteBtn->setCaption('delete');
670 $deleteBtn->setCommand('deleteMails');
671 $this->toolbar->addButtonInstance($deleteBtn);
672
673 if ($sender && $sender->getId() && !$sender->isAnonymous()) {
674 $linked_fullname = $sender->getPublicName();
675 $picture = ilUtil::img(
676 $sender->getPersonalPicturePath('xsmall'),
677 $sender->getPublicName(),
678 '',
679 '',
680 0,
681 '',
682 'ilMailAvatar'
683 );
684
685 if (in_array(ilObjUser::_lookupPref($sender->getId(), 'public_profile'), ['y', 'g'])) {
686 $this->ctrl->setParameter($this, 'mail_id', $mailId);
687 $this->ctrl->setParameter($this, 'mobj_id', $mailData['folder_id']);
688 $this->ctrl->setParameter($this, 'user', $sender->getId());
689 $linked_fullname = '<br /><a href="' . $this->ctrl->getLinkTarget(
690 $this,
691 'showUser'
692 ) . '" title="' . $linked_fullname . '">' . $linked_fullname . '</a>';
693 $this->ctrl->clearParameters($this);
694 }
695
696 $from = new ilCustomInputGUI($this->lng->txt('from') . ':');
697 $from->setHtml($picture . ' ' . $linked_fullname);
698 } elseif (!$sender || !$sender->getId()) {
699 $from = new ilCustomInputGUI($this->lng->txt('from') . ':');
700 $from->setHtml(trim(($mailData['import_name'] ?? '') . ' (' . $this->lng->txt('user_deleted') . ')'));
701 } else {
702 $from = new ilCustomInputGUI($this->lng->txt('from') . ':');
703 $from->setHtml(
705 ilUtil::getImagePath('HeaderIconAvatar.svg'),
706 ilMail::_getIliasMailerName(),
707 '',
708 '',
709 0,
710 '',
711 'ilMailAvatar'
712 ) .
713 '<br />' . ilMail::_getIliasMailerName()
714 );
715 }
716 $form->addItem($from);
717
718 $to = new ilCustomInputGUI($this->lng->txt('mail_to') . ':');
720 $this->umail->formatNamesForOutput($mailData['rcp_to'] ?? ''),
721 false
722 ));
723 $form->addItem($to);
724
725 if ($mailData['rcp_cc']) {
726 $cc = new ilCustomInputGUI($this->lng->txt('mail_cc') . ':');
728 $this->umail->formatNamesForOutput($mailData['rcp_cc'] ?? ''),
729 false
730 ));
731 $form->addItem($cc);
732 }
733
734 if ($mailData['rcp_bcc']) {
735 $bcc = new ilCustomInputGUI($this->lng->txt('mail_bcc') . ':');
736 $bcc->setHtml(ilUtil::htmlencodePlainString(
737 $this->umail->formatNamesForOutput($mailData['rcp_bcc'] ?? ''),
738 false
739 ));
740 $form->addItem($bcc);
741 }
742
743 $subject = new ilCustomInputGUI($this->lng->txt('subject') . ':');
744 $subject->setHtml(ilUtil::htmlencodePlainString($mailData['m_subject'] ?? '', true));
745 $form->addItem($subject);
746
747 $date = new ilCustomInputGUI($this->lng->txt('date') . ':');
748 $date->setHtml(ilDatePresentation::formatDate(
749 new ilDateTime($mailData['send_time'], IL_CAL_DATETIME)
750 ));
751 $form->addItem($date);
752
753 $message = new ilCustomInputGUI($this->lng->txt('message') . ':');
754 $message->setHtml(ilUtil::htmlencodePlainString($mailData['m_message'] ?? '', true));
755 $form->addItem($message);
756
757 if ($mailData['attachments']) {
758 $att = new ilCustomInputGUI($this->lng->txt('attachments') . ':');
759
760 $radiog = new ilRadioGroupInputGUI('', 'filename');
761 foreach ($mailData['attachments'] as $file) {
762 $radiog->addOption(new ilRadioOption($file, md5($file)));
763 }
764
765 $att->setHtml($radiog->render());
766 $form->addCommandButton('deliverFile', $this->lng->txt('download'));
767 $form->addItem($att);
768 }
769
770 $isTrashFolder = false;
771 if ($this->mbox->getTrashFolder() === $mailData['folder_id']) {
772 $isTrashFolder = true;
773 }
774
775 $currentFolderData = $this->mbox->getFolderData((int) $mailData['folder_id']);
776 if ($currentFolderData === null) {
777 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_operation_on_invalid_folder'), true);
778 $this->ctrl->setParameterByClass(ilMailGUI::class, 'mobj_id', $this->mbox->getInboxFolder());
779 $this->ctrl->redirectByClass(ilMailGUI::class);
780 }
781
782 $actions = $this->mbox->getActions((int) $mailData['folder_id']);
783
784 $selectOptions = [];
785 foreach ($actions as $key => $action) {
786 if ($key === 'moveMails') {
787 $folders = $this->mbox->getSubFolders();
788 foreach ($folders as $folder) {
789 if (
790 ($folder['type'] !== 'trash' || !$isTrashFolder) &&
791 $folder['obj_id'] !== $mailData['folder_id']
792 ) {
793 $optionText = $action . ' ' . $folder['title'];
794 if ($folder['type'] !== 'user_folder') {
795 $optionText = $action . ' ' . $this->lng->txt(
796 'mail_' . $folder['title']
797 ) .
798 ($folder['type'] === 'trash' ? ' (' . $this->lng->txt('delete') . ')' : '');
799 }
800
801 $selectOptions[$folder['obj_id']] = $optionText;
802 }
803 }
804 }
805 }
806
807 $folderLabel = $this->lng->txt('mail_' . $currentFolderData['title']);
808 if ($currentFolderData['type'] === 'user_folder') {
809 $folderLabel = $currentFolderData['title'];
810 }
811
812 $this->toolbar->addSeparator();
813 $this->toolbar->addText(sprintf($this->lng->txt('current_folder'), $folderLabel));
814
815 if (is_array($selectOptions) && count($selectOptions) > 0) {
816 $actions = new ilSelectInputGUI('', 'folder_id');
817 $actions->setOptions($selectOptions);
818 $this->toolbar->addInputItem($actions);
819
820 $moveBtn = ilSubmitButton::getInstance();
821 $moveBtn->setCaption('execute');
822 $moveBtn->setCommand('moveSingleMail');
823 $this->toolbar->addButtonInstance($moveBtn);
824 }
825
826 $prevMail = $this->umail->getPreviousMail($mailId);
827 $nextMail = $this->umail->getNextMail($mailId);
828 if (is_array($prevMail) || is_array($nextMail)) {
829 $this->toolbar->addSeparator();
830
831 if ($prevMail && $prevMail['mail_id']) {
832 $prevBtn = ilLinkButton::getInstance();
833 $prevBtn->setCaption('previous');
834 $this->ctrl->setParameter($this, 'mail_id', $prevMail['mail_id']);
835 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
836 $prevBtn->setUrl($this->ctrl->getLinkTarget($this, 'showMail'));
837 $this->ctrl->clearParameters($this);
838 $this->toolbar->addButtonInstance($prevBtn);
839 }
840
841 if ($nextMail && $nextMail['mail_id']) {
842 $nextBtn = ilLinkButton::getInstance();
843 $nextBtn->setCaption('next');
844 $this->ctrl->setParameter($this, 'mail_id', $nextMail['mail_id']);
845 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
846 $nextBtn->setUrl($this->ctrl->getLinkTarget($this, 'showMail'));
847 $this->ctrl->clearParameters($this);
848 $this->toolbar->addButtonInstance($nextBtn);
849 }
850 }
851
852 $this->tpl->setContent($form->getHTML());
853 $this->tpl->printToStdout();
854 }
855
856 protected function printMail(): void
857 {
858 $tplprint = new ilTemplate('tpl.mail_print.html', true, true, 'Services/Mail');
859
860 $mailId = 0;
861 if ($this->http->wrapper()->query()->has('mail_id')) {
862 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
863 }
864 $mailData = $this->umail->getMail($mailId);
865
867 $sender = ilObjectFactory::getInstanceByObjId($mailData['sender_id'], false);
868
869 $tplprint->setVariable('TXT_FROM', $this->lng->txt('from'));
870 if ($sender instanceof ilObjUser && $sender->getId() !== 0 && !$sender->isAnonymous()) {
871 $tplprint->setVariable('FROM', $sender->getPublicName());
872 } elseif (null === $sender || 0 === $sender->getId()) {
873 $tplprint->setVariable(
874 'FROM',
875 trim(($mailData['import_name'] ?? '') . ' (' . $this->lng->txt('user_deleted') . ')')
876 );
877 } else {
878 $tplprint->setVariable('FROM', ilMail::_getIliasMailerName());
879 }
880
881 $tplprint->setVariable('TXT_TO', $this->lng->txt('mail_to'));
882 $tplprint->setVariable('TO', $mailData['rcp_to']);
883
884 if ($mailData['rcp_cc']) {
885 $tplprint->setCurrentBlock('cc');
886 $tplprint->setVariable('TXT_CC', $this->lng->txt('mail_cc'));
887 $tplprint->setVariable('CC', $mailData['rcp_cc']);
888 $tplprint->parseCurrentBlock();
889 }
890
891 if ($mailData['rcp_bcc']) {
892 $tplprint->setCurrentBlock('bcc');
893 $tplprint->setVariable('TXT_BCC', $this->lng->txt('mail_bcc'));
894 $tplprint->setVariable('BCC', $mailData['rcp_bcc']);
895 $tplprint->parseCurrentBlock();
896 }
897
898 $tplprint->setVariable('TXT_SUBJECT', $this->lng->txt('subject'));
899 $tplprint->setVariable('SUBJECT', htmlspecialchars($mailData['m_subject']));
900
901 $tplprint->setVariable('TXT_DATE', $this->lng->txt('date'));
902 $tplprint->setVariable(
903 'DATE',
904 ilDatePresentation::formatDate(new ilDateTime($mailData['send_time'], IL_CAL_DATETIME))
905 );
906
907 $tplprint->setVariable('TXT_MESSAGE', $this->lng->txt('message'));
908 $tplprint->setVariable('MAIL_MESSAGE', nl2br(htmlspecialchars($mailData['m_message'])));
909
910 $tplprint->show();
911 }
912
913 protected function deliverFile(): void
914 {
915 $mailId = 0;
916 if ($this->http->wrapper()->query()->has('mail_id')) {
917 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
918 }
919
920 if ($mailId <= 0) {
921 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
922 }
923
924 $filename = '';
925 if ($this->http->wrapper()->post()->has('filename')) {
926 $filename = $this->http->wrapper()->post()->retrieve('filename', $this->refinery->kindlyTo()->string());
927 }
928
929 if (is_string(ilSession::get('filename')) && ilSession::get('filename') !== '') {
930 $filename = ilSession::get('filename');
931 ilSession::set('filename', null);
932 }
933
934 try {
935 if ($mailId > 0 && $filename !== '') {
936 while (strpos($filename, '..') !== false) {
937 $filename = str_replace('..', '', $filename);
938 }
939
940 $mailFileData = new ilFileDataMail($this->user->getId());
941 try {
942 $file = $mailFileData->getAttachmentPathAndFilenameByMd5Hash($filename, (int) $mailId);
943 ilFileDelivery::deliverFileLegacy($file['path'], $file['filename']);
944 } catch (OutOfBoundsException $e) {
945 throw new ilException('mail_error_reading_attachment');
946 }
947 } else {
948 $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_attachment'));
949 $this->showMail();
950 }
951 } catch (Exception $e) {
952 $this->tpl->setOnScreenMessage('failure', $this->lng->txt($e->getMessage()), true);
953 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
954 $this->ctrl->redirect($this);
955 }
956 }
957
958 protected function deliverAttachments(): void
959 {
960 try {
961 $mailId = 0;
962 if ($this->http->wrapper()->query()->has('mail_id')) {
963 $mailId = $this->http->wrapper()->query()->retrieve('mail_id', $this->refinery->kindlyTo()->int());
964 }
965
966 $mailData = $this->umail->getMail((int) $mailId);
967 if (null === $mailData || 0 === count((array) $mailData['attachments'])) {
968 throw new ilException('mail_error_reading_attachment');
969 }
970
971 $type = '';
972 if ($this->http->wrapper()->query()->has('type')) {
973 $type = $this->http->wrapper()->query()->retrieve('type', $this->refinery->kindlyTo()->string());
974 }
975
976 $mailFileData = new ilFileDataMail($this->user->getId());
977 if (count($mailData['attachments']) === 1) {
978 $attachment = current($mailData['attachments']);
979
980 try {
981 if ('draft' === $type) {
982 if (!$mailFileData->checkFilesExist([$attachment])) {
983 throw new OutOfBoundsException('');
984 }
985 $pathToFile = $mailFileData->getAbsoluteAttachmentPoolPathByFilename($attachment);
986 $fileName = $attachment;
987 } else {
988 $file = $mailFileData->getAttachmentPathAndFilenameByMd5Hash(
989 md5($attachment),
990 (int) $mailId
991 );
992 $pathToFile = $file['path'];
993 $fileName = $file['filename'];
994 }
995 ilFileDelivery::deliverFileLegacy($pathToFile, $fileName);
996 } catch (OutOfBoundsException $e) {
997 throw new ilException('mail_error_reading_attachment');
998 }
999 } else {
1000 $mailFileData->deliverAttachmentsAsZip(
1001 $mailData['m_subject'],
1002 (int) $mailId,
1003 $mailData['attachments'],
1004 'draft' === $type
1005 );
1006 }
1007 } catch (Exception $e) {
1008 $this->tpl->setOnScreenMessage('failure', $this->lng->txt($e->getMessage()), true);
1009 $this->ctrl->setParameter($this, 'mobj_id', $this->currentFolderId);
1010 $this->ctrl->redirect($this);
1011 }
1012 }
1013
1015 {
1016 $table = new ilMailFolderTableGUI(
1017 $this,
1018 $this->currentFolderId,
1019 'showFolder',
1020 $this->currentFolderId === $this->mbox->getTrashFolder(),
1021 $this->currentFolderId === $this->mbox->getSentFolder(),
1022 $this->currentFolderId === $this->mbox->getDraftsFolder()
1023 );
1024 $table->initFilter();
1025
1026 return $table;
1027 }
1028
1029 protected function applyFilter(): void
1030 {
1031 $table = $this->getMailFolderTable();
1032 $table->resetOffset();
1033 $table->writeFilterToSession();
1034
1035 $this->showFolder();
1036 }
1037
1038 protected function resetFilter(): void
1039 {
1040 $table = $this->getMailFolderTable();
1041 $table->resetOffset();
1042 $table->resetFilter();
1043
1044 $this->showFolder();
1045 }
1046}
$filename
Definition: buildRTE.php:78
Builds data types.
Definition: Factory.php:21
const IL_CAL_DATETIME
error(string $a_errmsg)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
Error Handling & global info handling uses PEAR error class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class handles all operations on files (attachments) in directory ilias_data/mail.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
language handling
addSubFolderCommands(bool $isUserSubFolder=false)
ilGlobalTemplateInterface $tpl
addSubFolder(ilPropertyFormGUI $form=null)
deleteSubFolder(bool $a_show_confirm=true)
showFolder(bool $oneConfirmationDialogueRendered=false)
parseFolderIdFromCommand(string $command)
ilCtrlInterface $ctrl
GlobalHttpState $http
getMailIdsFromRequest(bool $ignoreHttpGet=false)
renameSubFolder(ilPropertyFormGUI $form=null)
ilErrorHandling $error
getSubFolderForm(string $mode='create')
parseCommand(string $originalCommand)
Mail Box class Base class for creating and handling mail boxes.
User class.
static _lookupPref(int $a_usr_id, string $a_keyword)
static _lookupLogin(int $a_user_id)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a selection list property in a property form.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static htmlencodePlainString(string $a_str, bool $a_make_links_clickable, bool $a_detect_goto_links=false)
Encodes a plain text string into HTML for display in a browser.
static img(string $a_src, ?string $a_alt=null, $a_width="", $a_height="", $a_border=0, $a_id="", $a_class="")
Build img tag.
global $DIC
Definition: feed.php:28
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
$type
$message
Definition: xapiexit.php:32