ILIAS  release_8 Revision v8.24
class.ilWebDAVMountInstructionsDocumentFormGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
25
27{
31 protected ilObjUser $actor;
34 protected string $form_action;
35 protected string $save_command;
36 protected string $cancel_command;
37 protected bool $is_editable = false;
38 protected string $translated_error = '';
39 protected string $translated_info = '';
40
41 public function __construct(
47 FileUpload $fileupload,
48 string $form_action,
49 string $save_command,
50 string $cancel_command,
51 bool $is_editable
52 ) {
53 $this->document = $document;
54 $this->mount_instructions_repository = $mount_instructions_repository;
55 $this->document_purifier = $document_purifier;
56 $this->actor = $actor;
57 $this->tmp_filesystem = $tmp_filesystem;
58 $this->file_upload = $fileupload;
59 $this->form_action = $form_action;
60 $this->save_command = $save_command;
61 $this->cancel_command = $cancel_command;
62 $this->is_editable = $is_editable;
63
65
66 $this->initForm();
67 }
68
69 protected function initForm(): void
70 {
71 $document_already_exists = $this->document->getId() > 0;
72 if ($document_already_exists) {
73 $this->setTitle($this->lng->txt('webdav_form_edit_doc_head'));
74 } else {
75 $this->setTitle($this->lng->txt('webdav_form_new_doc_head'));
76 }
77
78 $this->setFormAction($this->form_action);
79
80 $title = new ilTextInputGUI($this->lng->txt('webdav_form_document_title'), 'title');
81 $title->setInfo($this->lng->txt('webdav_form_document_title_info'));
82 $title->setRequired(true);
83 $title->setDisabled(!$this->is_editable);
84 $title->setValue($this->document->getTitle());
85 $title->setMaxLength(255);
86 $this->addItem($title);
87
88 $document_label = $this->lng->txt('webdav_form_document');
89 $document_by_line = $this->lng->txt('webdav_form_document_info');
90
91 $language_selection = new ilSelectInputGUI(
92 $this->lng->txt('language'),
93 'lng'
94 );
95 $language_selection->setRequired(true);
96
97 $options = [];
98 foreach ($this->lng->getInstalledLanguages() as $lng) {
99 $options[$lng] = $this->lng->txt('meta_l_' . $lng, 'meta');
100 }
101
102 asort($options);
103
104 $language_selection->setOptions(['' => $this->lng->txt('please_choose')] + $options);
105 $language_selection->setValue($this->document->getLanguage());
106
107 $this->addItem($language_selection);
108
109 if ($document_already_exists) {
110 $document_id = new ilHiddenInputGUI('document_id');
111 $document_id->setValue((string) $this->document->getId());
112 $this->addItem($document_id);
113 } else {
114 $document_upload = new ilFileInputGUI($document_label, 'document');
115 $document_upload->setInfo($document_by_line);
116 $document_upload->setRequired(true);
117 $document_upload->setDisabled(!$this->is_editable);
118 $document_upload->setSuffixes(['html', 'htm', 'txt']);
119 $this->addItem($document_upload);
120 }
121
122 if ($this->is_editable) {
123 $this->addCommandButton($this->save_command, $this->lng->txt('save'));
124 }
125 }
126
127 public function saveObject(): bool
128 {
129 try {
130 $this->document = $this->createFilledObject($this->document);
131 $this->mount_instructions_repository->createMountInstructionsDocumentEntry($this->document);
132 } catch (InvalidArgumentException $e) {
133 $this->setValuesByPost();
134 $this->translated_error .= $e->getMessage();
135 return false;
136 }
137
138 return true;
139 }
140
141 public function updateObject(): bool
142 {
143 try {
144 $this->document = $this->createFilledObject($this->document);
145 $this->mount_instructions_repository->updateMountInstructions($this->document);
146 } catch (InvalidArgumentException $e) {
147 $this->setValuesByPost();
148 $this->translated_error .= $e->getMessage();
149 return false;
150 }
151
152 return true;
153 }
154
155 public function hasTranslatedInfo(): bool
156 {
157 return strlen($this->translated_info) > 0;
158 }
159
160 public function hasTranslatedError(): bool
161 {
162 return strlen($this->translated_error) > 0;
163 }
164
165 public function getTranslatedInfo(): string
166 {
168 }
169
170 public function getTranslatedError(): string
171 {
173 }
174
176 {
177 if (!$this->checkInput()) {
178 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
179 }
180
181 $document_already_exists = $document->getId() > 0;
182
183 if (!$document_already_exists) {
184 $upload_result = $this->getFileUploadResult();
185 }
186
187 if (!$document_already_exists && !$upload_result->isOK()) {
188 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
189 }
190
191 $title = $this->getInput('title');
192 $language = $this->getInput('lng');
193 $creation_ts = $document_already_exists ? $document->getCreationTs() : ilUtil::now();
194 $modification_ts = $document_already_exists ? ilUtil::now() : $creation_ts;
195 $owner_id = $document_already_exists ? $document->getOwnerUsrId() : $this->actor->getId();
196 $last_modified_usr_id = $this->actor->getId();
197 $sorting = $document_already_exists ? $document->getSorting() : $this->mount_instructions_repository->getHighestSortingNumber() + 1;
198
199 $mount_instruction_for_language_exists = $this->mount_instructions_repository->doMountInstructionsExistByLanguage($language);
200
201 if (!$document_already_exists && $mount_instruction_for_language_exists) {
202 throw new InvalidArgumentException($this->lng->txt("webdav_choosen_language_already_used"));
203 }
204
205 if ($document_already_exists && $document->getLanguage() != $language &&
206 $mount_instruction_for_language_exists > 0 &&
207 $mount_instruction_for_language_exists != $document->getId()) {
208 throw new InvalidArgumentException($this->lng->txt("webdav_chosen_language_already_used"));
209 }
210
211 if ($document_already_exists) {
212 $raw_mount_instructions = '';
213 $processed_mount_instructions = '';
214 } else {
215 $raw_mount_instructions = $this->getRawMountInstructionsFromFileUpload($upload_result);
216 $document_processor = $upload_result->getMimeType() == 'text/html'
217 ? new ilWebDAVMountInstructionsHtmlDocumentProcessor($this->document_purifier)
219 $processed_mount_instructions = $document_processor->processMountInstructions($raw_mount_instructions);
220 }
221
222 $id = $document_already_exists ? $document->getId()
223 : $this->mount_instructions_repository->getNextMountInstructionsDocumentId();
224
226 $id,
227 $title,
228 $raw_mount_instructions,
229 json_encode($processed_mount_instructions),
230 $language,
231 $creation_ts,
232 $modification_ts,
233 $owner_id,
234 $last_modified_usr_id,
235 $sorting
236 );
237 }
238
239 protected function getRawMountInstructionsFromFileUpload(UploadResult $upload_result): string
240 {
241 if ($upload_result->getName() === '') {
242 throw new InvalidArgumentException('uploaded file has no name');
243 }
244
245 if (!$upload_result->isOK()) {
246 $this->getItemByPostVar('document')->setAlert($upload_result->getStatus()->getMessage());
247 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
248 }
249
250 $this->file_upload->moveOneFileTo(
251 $upload_result,
252 '/mount_instructions',
253 Location::TEMPORARY,
254 '',
255 true
256 );
257
258 $path_to_file = '/mount_instructions/' . $upload_result->getName();
259 if (!$this->tmp_filesystem->has($path_to_file)) {
260 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
261 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
262 }
263
264 $raw_content = $this->tmp_filesystem->read($path_to_file);
265
266 $this->tmp_filesystem->delete($path_to_file);
267
268 return $raw_content;
269 }
270
271 protected function getFileUploadResult(): UploadResult
272 {
273 if (!$this->file_upload->hasUploads()) {
274 throw new InvalidArgumentException("webdav_error_no_upload");
275 } elseif ($this->file_upload->hasBeenProcessed()) {
276 throw new InvalidArgumentException("webdav_error_upload_already_processed");
277 }
278
279 $this->file_upload->process();
280
281 $upload_result = array_values($this->file_upload->getResults())[0];
282
283 if (!$upload_result) {
284 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
285 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
286 }
287
288 return $upload_result;
289 }
290}
This class represents a file property in a property form.
setFormAction(string $a_formaction)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
User class.
This class represents a property form user interface.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
This class represents a selection list property in a property form.
This class represents a text property in a property form.
static now()
Return current timestamp in Y-m-d H:i:s format.
__construct(ilWebDAVMountInstructionsDocument $document, ilWebDAVMountInstructionsRepository $mount_instructions_repository, ?ilHtmlPurifierInterface $document_purifier, ilObjUser $actor, Filesystem $tmp_filesystem, FileUpload $fileupload, string $form_action, string $save_command, string $cancel_command, bool $is_editable)
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Location.
Definition: Location.php:30
Interface Filesystem.
Definition: Filesystem.php:40
Interface for html sanitizing functionality.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc