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