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