ILIAS  release_7 Revision v7.30-3-g800a261c036
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 ilFileInputGUI($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->setSuffixes(['html', 'htm', 'txt']);
146 $this->addItem($document_upload);
147 }
148
149 if ($this->is_editable) {
150 $this->addCommandButton($this->save_command, $this->lng->txt('save'));
151 }
152 }
153
158 public function saveObject()
159 {
160 try {
161 $this->document = $this->createFilledObject($this->document);
162 $this->mount_instructions_repository->createMountInstructionsDocumentEntry($this->document);
163 } catch (InvalidArgumentException $e) {
164 $this->setValuesByPost();
165 $this->translated_error .= $e->getMessage();
166 return false;
167 }
168
169 return true;
170 }
171
175 public function updateObject()
176 {
177 try {
178 $this->document = $this->createFilledObject($this->document);
179 $this->mount_instructions_repository->updateMountInstructions($this->document);
180 } catch (InvalidArgumentException $e) {
181 $this->setValuesByPost();
182 $this->translated_error .= $e->getMessage();
183 return false;
184 }
185
186 return true;
187 }
188
192 public function hasTranslatedInfo()
193 {
194 return strlen($this->translated_info) > 0;
195 }
196
200 public function hasTranslatedError()
201 {
202 return strlen($this->translated_error) > 0;
203 }
204
208 public function getTranslatedInfo()
209 {
211 }
212
216 public function getTranslatedError()
217 {
219 }
220
229 {
230 // early exit for invalid input
231 if (!$this->checkInput()) {
232 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
233 }
234
235 // check if document already exists in db
236 $document_already_exists = $document->getId() > 0;
237
238 if (!$document_already_exists) {
240 $upload_result = $this->getFileUploadResult();
241 }
242
243 // Exit on failed file upload
244 if (!$document_already_exists && $upload_result->getStatus()->getCode() != ProcessingStatus::OK) {
245 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
246 }
247
248 // Get values for document
249 $title = $this->getInput('title');
250 $language = $this->getInput('lng');
251 $creation_ts = $document_already_exists ? $document->getCreationTs() : ilUtil::now();
252 $modification_ts = $document_already_exists ? ilUtil::now() : $creation_ts;
253 $owner_id = $document_already_exists ? $document->getOwnerUsrId() : $this->actor->getId();
254 $last_modified_usr_id = $this->actor->getId();
255 $sorting = $document_already_exists ? $document->getSorting() : $this->mount_instructions_repository->getHighestSortingNumber() + 1;
256
257 // On creating a new document -> check if language is already in use by another document
258 if (!$document_already_exists && $this->mount_instructions_repository->doMountInstructionsExistByLanguage($language)) {
259 throw new InvalidArgumentException($this->lng->txt("webdav_choosen_language_already_used"));
260 }
261
262 // On editing document -> check if language is changed and already is in use by another document
263 if ($document_already_exists && $document->getLanguage() != $language
264 && $this->mount_instructions_repository->doMountInstructionsExistByLanguage($language) != $document->getId()) {
265 throw new InvalidArgumentException($this->lng->txt("webdav_chosen_language_already_used"));
266 }
267
268 if ($document_already_exists) {
269 $raw_mount_instructions = '';
270 $processed_mount_instructions = '';
271 } else {
272 // Get and process mount instructions
273 $raw_mount_instructions = $this->getRawMountInstructionsFromFileUpload($upload_result);
274 $document_processor = $upload_result->getMimeType() == 'text/html'
275 ? new ilWebDAVMountInstructionsHtmlDocumentProcessor($this->document_purifier)
277 $processed_mount_instructions = $document_processor->processMountInstructions($raw_mount_instructions);
278 }
279 // Get or create new id for document
280 $id = $document_already_exists ? $document->getId()
281 : $this->mount_instructions_repository->getNextMountInstructionsDocumentId();
282
283 // Create document with new values (no setter methods -> object from this class are immutable)
285 $id,
286 $title,
287 $raw_mount_instructions,
288 json_encode($processed_mount_instructions),
289 $language,
290 $creation_ts,
291 $modification_ts,
292 $owner_id,
293 $last_modified_usr_id,
294 $sorting
295 );
296
297 return $document;
298 }
299
308 protected function getRawMountInstructionsFromFileUpload(UploadResult $upload_result) : string
309 {
310 // Check uploaded name
311 if ($upload_result->getName() === '') {
312 throw new InvalidArgumentException('uploaded file has no name');
313 }
314
315 // Check status
316 if ($upload_result->getStatus()->getCode() != ProcessingStatus::OK) {
317 $this->getItemByPostVar('document')->setAlert($upload_result->getStatus()->getMessage());
318 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
319 }
320
321 // Move uploaded file to a temporary directory to read it
322 $this->file_upload->moveOneFileTo(
323 $upload_result,
324 '/mount_instructions',
325 Location::TEMPORARY,
326 '',
327 true
328 );
329
330 $path_to_file = '/mount_instructions/' . $upload_result->getName();
331 if (!$this->tmp_filesystem->has($path_to_file)) {
332 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
333 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
334 }
335
336 // Get conetent of file
337 $raw_content = $content = $this->tmp_filesystem->read($path_to_file);
338
339 // Delete temporary file
340 $this->tmp_filesystem->delete($path_to_file);
341
342 return $raw_content;
343 }
344
349 protected function getFileUploadResult() : UploadResult
350 {
351 // Early exit if file upload has errors (no uploads or uploads already processed)
352 if (!$this->file_upload->hasUploads()) {
353 throw new InvalidArgumentException("webdav_error_no_upload");
354 } elseif ($this->file_upload->hasBeenProcessed()) {
355 throw new InvalidArgumentException("webdav_error_upload_already_processed");
356 }
357
358 $this->file_upload->process();
359
361 $upload_result = array_values($this->file_upload->getResults())[0];
362
363 if (!$upload_result) {
364 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
365 throw new InvalidArgumentException($this->lng->txt('form_input_not_valid'));
366 }
367
368 return $upload_result;
369 }
370}
An exception for terminatinating execution or to throw for unit testing.
This class represents a file property in a property form.
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