ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Refinery\Factory as Refinery;
30
34{
36 private readonly ilLanguage $lng;
37 private readonly ilObjUser $user;
38 private readonly ilFormatMail $umail;
39 private readonly ilFileDataMail $mfile;
40 private readonly Refinery $refinery;
41 private readonly \ILIAS\UI\Factory $ui_factory;
42 private readonly \ILIAS\UI\Renderer $ui_renderer;
43 private readonly ilTabsGUI $tabs;
44 private AttachmentManagement $mode = AttachmentManagement::MANAGE;
45
46 public function __construct()
47 {
48 global $DIC;
49
51 $this->tpl = $DIC->ui()->mainTemplate();
52 $this->lng = $DIC->language();
53 $this->user = $DIC->user();
54 $this->tabs = $DIC->tabs();
55 $this->refinery = $DIC->refinery();
56 $this->ui_factory = $DIC->ui()->factory();
57 $this->ui_renderer = $DIC->ui()->renderer();
58
59 $this->ctrl->saveParameter($this, 'mobj_id');
60
61 $this->umail = new ilFormatMail($DIC->user()->getId());
62 $this->mfile = new ilFileDataMail($DIC->user()->getId());
63 }
64
65 public function getUnsafeGetCommands(): array
66 {
67 return [
69 ];
70 }
71
72 public function getSafePostCommands(): array
73 {
74 return [];
75 }
76
77 public function manage(): self
78 {
79 $this->mode = AttachmentManagement::MANAGE;
80 return $this;
81 }
82
83 public function consume(): self
84 {
86 return $this;
87 }
88
89 public function executeCommand(): void
90 {
91 $cmd = $this->ctrl->getCmd();
92 switch ($cmd) {
93 case AbstractCtrlAwareUploadHandler::CMD_UPLOAD:
94 case AbstractCtrlAwareUploadHandler::CMD_INFO:
95 case AbstractCtrlAwareUploadHandler::CMD_REMOVE:
96 parent::executeCommand();
97 break;
98
99 default:
100 if ($cmd === null || $cmd === '' || !method_exists($this, $cmd . 'Command')) {
101 $cmd = self::DEFAULT_CMD;
102 }
103 $verified_command = $cmd . 'Command';
104 $this->$verified_command();
105 break;
106 }
107 }
108
109 private function saveAttachments(): void
110 {
111 $files = [];
112
113 // Important: Do not check for uploaded files here,
114 // otherwise it is no more possible to remove files (please ignore bug reports like 10137)
115
116 $size_of_affected_files = 0;
117 $files_of_request = $this->http->wrapper()->query()->retrieve(
118 'mail_attachments_filename',
119 $this->refinery->byTrying([
120 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
121 $this->refinery->always([])
122 ])
123 );
124
125 if ($files_of_request !== [] && $files_of_request[0] === 'ALL_OBJECTS') {
126 $files_of_request = array_map(static fn(array $file): string => $file['name'], $this->mfile->getUserFilesData());
127 }
128
129 foreach ($files_of_request as $file) {
130 if (is_file($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode((string) $file)))) {
131 $files[] = urldecode((string) $file);
132 $size_of_affected_files += filesize(
133 $this->mfile->getMailPath() . '/' .
134 basename($this->user->getId() . '_' . urldecode((string) $file))
135 );
136 }
137 }
138
139 if ($files !== [] &&
140 $this->mfile->getAttachmentsTotalSizeLimit() !== null &&
141 $size_of_affected_files > $this->mfile->getAttachmentsTotalSizeLimit()) {
142 $this->tpl->setOnScreenMessage(
143 $this->tpl::MESSAGE_TYPE_FAILURE,
144 $this->lng->txt('mail_max_size_attachments_total_error') . ' ' .
145 ilUtil::formatSize((int) $this->mfile->getAttachmentsTotalSizeLimit())
146 );
147 $this->showAttachmentsCommand();
148 return;
149 }
150
151 $this->umail->saveAttachments($files);
152
153 $this->ctrl->returnToParent($this);
154 }
155
156 private function cancelSaveAttachmentsCommand(): void
157 {
158 $this->ctrl->setParameter($this, 'type', ilMailFormGUI::MAIL_FORM_TYPE_ATTACH);
159 $this->ctrl->returnToParent($this);
160 }
161
162 private function confirmDeleteAttachments(): void
163 {
164 $files = $this->http->wrapper()->query()->retrieve(
165 'mail_attachments_filename',
166 $this->refinery->byTrying([
167 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
168 $this->refinery->always([])
169 ])
170 );
171
172 if ($files !== [] && $files[0] === 'ALL_OBJECTS') {
173 $files = array_map(static fn(array $file): string => $file['name'], $this->mfile->getUserFilesData());
174 }
175
176 if ($files === []) {
177 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_INFO, $this->lng->txt('select_one'), true);
178 $this->ctrl->redirect($this);
179 }
180
181 $this->tpl->setTitle($this->lng->txt('mail'));
182
183 $confirmation = new ilConfirmationGUI();
184 $confirmation->setFormAction($this->ctrl->getFormAction($this, self::CMD_DELETE_ATTACHMENTS));
185 $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_DELETE_ATTACHMENTS);
186 $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_SHOW_ATTACHMENTS);
187 $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
188
189 foreach ($files as $filename) {
190 $confirmation->addItem(
191 'filename[]',
193 ilUtil::stripSlashes(urldecode((string) $filename))
194 );
195 }
196
197 $this->tpl->setContent($confirmation->getHTML());
198 $this->tpl->printToStdout();
199 }
200
201 private function deleteAttachmentsCommand(): void
202 {
203 $files = $this->http->wrapper()->post()->retrieve(
204 'filename',
205 $this->refinery->byTrying([
206 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
207 $this->refinery->always([])
208 ])
209 );
210
211 if ($files === []) {
212 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_INFO, $this->lng->txt('mail_select_one_mail'));
213 $this->showAttachmentsCommand();
214 return;
215 }
216
217 $decoded_files = [];
218 foreach ($files as $value) {
219 $decoded_files[] = urldecode((string) $value);
220 }
221
222 $error = $this->mfile->unlinkFiles($decoded_files);
223 if ($error !== '') {
224 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('mail_error_delete_file') . ' ' . $error, true);
225 } else {
226 $mail_data = $this->umail->retrieveFromStage();
227 if (is_array($mail_data['attachments'])) {
228 $tmp = [];
229 foreach ($mail_data['attachments'] as $attachment) {
230 if (!in_array($attachment, $decoded_files, true)) {
231 $tmp[] = $attachment;
232 }
233 }
234 $this->umail->saveAttachments($tmp);
235 }
236
237 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('mail_files_deleted'), true);
238 }
239
240 $this->ctrl->redirect($this);
241 }
242
243 private function showAttachmentsCommand(): void
244 {
245 $this->tpl->setTitle($this->lng->txt('mail'));
246
247 if ($this->mode === AttachmentManagement::CONSUME) {
248 $this->tabs->clearTargets();
249 $this->tabs->setBackTarget(
250 $this->lng->txt('mail_manage_attachments_back_to_compose'),
251 $this->ctrl->getLinkTarget($this, self::CMD_CANCEL_SAVE_ATTACHMENTS)
252 );
253 }
254
255 $components = [];
256 if ($this->mode === AttachmentManagement::MANAGE) {
257 $dropzone = $this->ui_factory
258 ->dropzone()
259 ->file()
260 ->standard(
261 $this->lng->txt('mail_manage_attachments'),
262 $this->lng->txt('mail_manage_attachments_drop_files_msg'),
263 '#',
264 $this->ui_factory->input()->field()->file(
265 $this,
266 $this->lng->txt('file')
267 )->withMaxFiles(42) // The answer to life, universe and the rest
268 )
269 ->withBulky(true)
270 ->withUploadButton(
271 $this->ui_factory->button()->shy(
272 $this->lng->txt('upload'),
273 '#'
274 )
275 );
276 $components[] = $dropzone;
277 }
278
279 $mail_data = $this->umail->retrieveFromStage();
280 $files = $this->mfile->getUserFilesData();
281 $records = [];
282 $checked_items = [];
283 foreach ($files as $file) {
284 if (is_array($mail_data['attachments']) && in_array($file['name'], $mail_data['attachments'], true)) {
285 $checked_items[] = urlencode($file['name']);
286 }
287
288 $records[] = [
289 'filename' => $file['name'],
290 'filesize' => (int) $file['size'],
291 'filecreatedate' => (int) $file['ctime'],
292 ];
293 }
294
295 $table = new MailAttachmentTableGUI(
296 $this,
297 $this->user,
298 $records,
299 $this->ui_factory,
300 $this->ui_renderer,
301 $this->lng,
302 $this->ctrl,
303 $this->http->request(),
304 new ILIAS\Data\Factory(),
305 self::CMD_HANDLE_TABLE_ACTIONS,
306 $this->mode
307 );
308 $components[] = $table->get();
309
310 $this->tpl->setContent($this->ui_renderer->render($components));
311
312 if ($this->mode === AttachmentManagement::CONSUME) {
313 // The table above has to be rendered first, because it deselects all checkboxes
314 $this->tpl->addOnLoadCode('
315 const checked_items = ' . json_encode($checked_items, JSON_THROW_ON_ERROR) . ';
316 for (const item of checked_items) {
317 const checkbox = document.querySelector("input[type=\'checkbox\'][value=\'" + item + "\']");
318 if (checkbox) {
319 checkbox.checked = true;
320 }
321 }
322 ');
323 }
324
325 $this->tpl->printToStdout();
326 }
327
328 private function handleTableActionsCommand(): void
329 {
330 $query = $this->http->wrapper()->query();
331 if (!$query->has('mail_attachments_table_action')) {
332 return;
333 }
334
335 $action = $query->retrieve('mail_attachments_table_action', $this->refinery->to()->string());
336 match ($action) {
337 self::TABLE_ACTION_SAVE_ATTACHMENTS => $this->saveAttachments(),
338 self::TABLE_CONFIRM_DELETE_ATTACHMENTS => $this->confirmDeleteAttachments(),
339 default => $this->ctrl->redirect($this),
340 };
341 }
342
343 protected function getUploadResult(): HandlerResult
344 {
345 $this->upload->process();
346 $array = $this->upload->getResults();
347 $result = end($array);
348
349 if ($result instanceof UploadResult && $result->isOK()) {
350 $identifier = $this->mfile->storeUploadedFile($result);
351 $status = HandlerResult::STATUS_OK;
352 $message = $this->lng->txt('saved_successfully');
353 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('saved_successfully'), true);
354 } else {
355 $status = HandlerResult::STATUS_FAILED;
356 $identifier = '';
357 $message = $result->getStatus()->getMessage();
358 }
359
360 return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
361 }
362
363 protected function getRemoveResult(string $identifier): HandlerResult
364 {
365 throw new DomainException('Not necessary for this handler');
366 }
367
368 public function getInfoResult(string $identifier): ?FileInfoResult
369 {
370 throw new DomainException('Not necessary for this handler');
371 }
372
373 public function getInfoForExistingFiles(array $file_ids): array
374 {
375 throw new DomainException('Not necessary for this handler');
376 }
377
378 public function getFileIdentifierParameterName(): string
379 {
380 return 'userfile';
381 }
382}
$filename
Definition: buildRTE.php:78
$components
Builds data types.
Definition: Factory.php:36
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
getInfoForExistingFiles(array $file_ids)
readonly ilFormatMail $umail
readonly ilFileDataMail $mfile
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
readonly ILIAS UI Factory $ui_factory
getInfoResult(string $identifier)
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
readonly ilGlobalTemplateInterface $tpl
getRemoveResult(string $identifier)
getSafePostCommands()
This method must return a list of safe POST commands.
__construct()
ilUIDemoFileUploadHandlerGUI constructor.
readonly ILIAS UI Renderer $ui_renderer
AttachmentManagement $mode
final const string MAIL_FORM_TYPE_ATTACH
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
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.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31