ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilWebDAVMountInstructionsDocumentFormGUI.php
Go to the documentation of this file.
1<?php
2
3use ILIAS\FileSystem\Filesystem;
8
10{
12 protected $document;
13
16
19
21 protected $actor;
22
24 protected $file_upload;
25
27 protected $tmp_filesystem;
28
30 protected $form_action;
31
33 protected $save_command;
34
36 protected $cancel_command;
37
39 protected $is_editable = false;
40
43
46
60 public function __construct(
63 ?ilHtmlPurifierInterface $a_document_purifier,
64 ilObjUser $a_actor,
65 FileSystem $a_tmp_filesystem,
66 FileUpload $a_fileupload,
67 string $a_form_action,
68 string $a_save_command,
69 string $a_cancel_command,
70 bool $a_is_editable
71 ) {
72 $this->document = $a_document;
73 $this->mount_instructions_repository = $mount_instructions_repository;
74 $this->document_purifier = $a_document_purifier;
75 $this->actor = $a_actor;
76 $this->tmp_filesystem = $a_tmp_filesystem;
77 $this->file_upload = $a_fileupload;
78 $this->form_action = $a_form_action;
79 $this->save_command = $a_save_command;
80 $this->cancel_command = $a_cancel_command;
81 $this->is_editable = $a_is_editable;
82
84
85 $this->initForm();
86 }
87
91 protected function initForm() : void
92 {
93 $document_already_exists = $this->document->getId() > 0;
94 if ($document_already_exists) {
95 $this->setTitle($this->lng->txt('webdav_form_edit_doc_head'));
96 } else {
97 $this->setTitle($this->lng->txt('webdav_form_new_doc_head'));
98 }
99
100 $this->setFormAction($this->form_action);
101
102 $title = new ilTextInputGUI($this->lng->txt('webdav_form_document_title'), 'title');
103 $title->setInfo($this->lng->txt('webdav_form_document_title_info'));
104 $title->setRequired(true);
105 $title->setDisabled(!$this->is_editable);
106 $title->setValue($this->document->getTitle());
107 $title->setMaxLength(255);
108 $this->addItem($title);
109
110 if ($document_already_exists) {
111 $document_label = $this->lng->txt('webdav_form_document');
112 $document_by_line = $this->lng->txt('webdav_form_document_info');
113 } else {
114 $document_label = $this->lng->txt('webdav_form_document');
115 $document_by_line = $this->lng->txt('webdav_form_document_info');
116 }
117
118 $language_selection = new ilSelectInputGUI(
119 $this->lng->txt('language'),
120 'lng'
121 );
122 $language_selection->setRequired(true);
123
124 $options = [];
125 foreach ($this->lng->getInstalledLanguages() as $lng) {
126 $options[$lng] = $this->lng->txt('meta_l_' . $lng, 'meta');
127 }
128
129 asort($options);
130
131 $language_selection->setOptions(['' => $this->lng->txt('please_choose')] + $options);
132 $language_selection->setValue((string) ($this->document->getLanguage() ?? ''));
133
134 $this->addItem($language_selection);
135
136 if ($document_already_exists) {
137 $webdav_id = new ilHiddenInputGUI('webdav_id');
138 $webdav_id->setValue($this->document->getId());
139 $this->addItem($webdav_id);
140 } else {
141 $document_upload = new ilFileStandardDropzoneInputGUI($document_label, 'document');
142 $document_upload->setInfo($document_by_line);
143 $document_upload->setRequired($document_already_exists ? false : true);
144 $document_upload->setDisabled(!$this->is_editable);
145 $document_upload->setMaxFiles(1);
146 $document_upload->setSuffixes(['html', 'htm', 'txt']);
147 $this->addItem($document_upload);
148 }
149
150 if ($this->is_editable) {
151 $this->addCommandButton($this->save_command, $this->lng->txt('save'));
152 }
153 }
154
159 public function saveObject()
160 {
161 try {
162 $this->document = $this->createFilledObject($this->document);
163 $this->mount_instructions_repository->createMountInstructionsDocumentEntry($this->document);
164 } catch (InvalidArgumentException $e) {
165 $this->setValuesByPost();
166 $this->translated_error .= $e->getMessage();
167 return false;
168 }
169
170 return true;
171 }
172
176 public function updateObject()
177 {
178 try {
179 $this->document = $this->createFilledObject($this->document);
180 $this->mount_instructions_repository->updateMountInstructions($this->document);
181 } catch (InvalidArgumentException $e) {
182 $this->setValuesByPost();
183 $this->translated_error .= $e->getMessage();
184 return false;
185 }
186
187 return true;
188 }
189
193 public function hasTranslatedInfo()
194 {
195 return strlen($this->translated_info) > 0;
196 }
197
201 public function hasTranslatedError()
202 {
203 return strlen($this->translated_error) > 0;
204 }
205
209 public function getTranslatedInfo()
210 {
212 }
213
217 public function getTranslatedError()
218 {
220 }
221
230 {
231 // early exit for invalid input
232 if (!$this->checkInput()) {
233 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
234 }
235
236 // check if document already exists in db
237 $document_already_exists = $document->getId() > 0;
238
239 if (!$document_already_exists) {
241 $upload_result = $this->getFileUploadResult();
242 }
243
244 // Exit on failed file upload
245 if (!$document_already_exists && $upload_result->getStatus()->getCode() != ProcessingStatus::OK) {
246 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
247 }
248
249 // Get values for document
250 $title = $this->getInput('title');
251 $language = $this->getInput('lng');
252 $creation_ts = $document_already_exists ? $document->getCreationTs() : ilUtil::now();
253 $modification_ts = $document_already_exists ? ilUtil::now() : $creation_ts;
254 $owner_id = $document_already_exists ? $document->getOwnerUsrId() : $this->actor->getId();
255 $last_modified_usr_id = $this->actor->getId();
256 $sorting = $document_already_exists ? $document->getSorting() : $this->mount_instructions_repository->getHighestSortingNumber() + 1;
257
258 // On creating a new document -> check if language is already in use by another document
259 if (!$document_already_exists && $this->mount_instructions_repository->doMountInstructionsExistByLanguage($language)) {
260 throw new InvalidArgumentException($this->lng->txt("webdav_choosen_language_already_used"));
261 }
262
263 // On editing document -> check if language is changed and already is in use by another document
264 if ($document_already_exists && $document->getLanguage() != $language
265 && $this->mount_instructions_repository->doMountInstructionsExistByLanguage($language) != $document->getId()) {
266 throw new InvalidArgumentException($this->lng->txt("webdav_chosen_language_already_used"));
267 }
268
269 if ($document_already_exists) {
270 $raw_mount_instructions = '';
271 $processed_mount_instructions = '';
272 } else {
273 // Get and process mount instructions
274 $raw_mount_instructions = $this->getRawMountInstructionsFromFileUpload($upload_result);
275 $document_processor = $upload_result->getMimeType() == 'text/html'
276 ? new ilWebDAVMountInstructionsHtmlDocumentProcessor($this->document_purifier)
278 $processed_mount_instructions = $document_processor->processMountInstructions($raw_mount_instructions);
279 }
280 // Get or create new id for document
281 $id = $document_already_exists ? $document->getId()
282 : $this->mount_instructions_repository->getNextMountInstructionsDocumentId();
283
284 // Create document with new values (no setter methods -> object from this class are immutable)
286 $id,
287 $title,
288 $raw_mount_instructions,
289 json_encode($processed_mount_instructions),
290 $language,
291 $creation_ts,
292 $modification_ts,
293 $owner_id,
294 $last_modified_usr_id,
295 $sorting
296 );
297
298 return $document;
299 }
300
309 protected function getRawMountInstructionsFromFileUpload(UploadResult $upload_result) : string
310 {
311 // Check uploaded name
312 if ($upload_result->getName() === '') {
313 throw new InvalidArgumentException('uploaded file has no name');
314 }
315
316 // Check status
317 if ($upload_result->getStatus()->getCode() != ProcessingStatus::OK) {
318 $this->getItemByPostVar('document')->setAlert($upload_result->getStatus()->getMessage());
319 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
320 }
321
322 // Move uploaded file to a temporary directory to read it
323 $this->file_upload->moveOneFileTo(
324 $upload_result,
325 '/mount_instructions',
326 Location::TEMPORARY,
327 '',
328 true
329 );
330
331 $path_to_file = '/mount_instructions/' . $upload_result->getName();
332 if (!$this->tmp_filesystem->has($path_to_file)) {
333 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
334 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
335 }
336
337 // Get conetent of file
338 $raw_content = $content = $this->tmp_filesystem->read($path_to_file);
339
340 // Delete temporary file
341 $this->tmp_filesystem->delete($path_to_file);
342
343 return $raw_content;
344 }
345
350 protected function getFileUploadResult() : UploadResult
351 {
352 // Early exit if file upload has errors (no uploads or uploads already processed)
353 if (!$this->file_upload->hasUploads()) {
354 throw new InvalidArgumentException("webdav_error_no_upload");
355 } elseif ($this->file_upload->hasBeenProcessed()) {
356 throw new InvalidArgumentException("webdav_error_upload_already_processed");
357 }
358
359 $this->file_upload->process();
360
362 $upload_result = array_values($this->file_upload->getResults())[0];
363
364 if (!$upload_result) {
365 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
366 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
367 }
368
369 return $upload_result;
370 }
371}
An exception for terminatinating execution or to throw for unit testing.
Class ilFileStandardDropzoneInputGUI.
setFormAction($a_formaction)
Set FormAction.
This class represents a hidden form property in a property form.
This class represents a property form user interface.
addItem($a_item)
Add Item (Property, SectionHeader).
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
checkInput()
Check Post Input.
setValuesByPost()
Set form values from POST values.
getItemByPostVar($a_post_var)
Get Item by POST variable.
setTitle($a_title)
Set Title.
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.
getRawMountInstructionsFromFileUpload(UploadResult $upload_result)
Gets the content of the uploaded file.
__construct(ilWebDAVMountInstructionsDocument $a_document, ilWebDAVMountInstructionsRepository $mount_instructions_repository, ?ilHtmlPurifierInterface $a_document_purifier, ilObjUser $a_actor, FileSystem $a_tmp_filesystem, FileUpload $a_fileupload, string $a_form_action, string $a_save_command, string $a_cancel_command, bool $a_is_editable)
ilWebDAVMountInstructionsDocumentFormGUI constructor.
Interface Location.
Definition: Location.php:17
Interface for html sanitizing functionality.
Interface ilWebDAVMountInstructionsRepository.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc