ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilAccessibilityDocumentFormGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4use ILIAS\FileSystem\Filesystem;
9
14{
16 protected $document;
17
19 protected $actor;
20
22 protected $fileUpload;
23
25 protected $tmpFileSystem;
26
28 protected $formAction;
29
31 protected $saveCommand;
32
34 protected $cancelCommand;
35
37 protected $isEditable = false;
38
40 protected $translatedError = '';
41
43 protected $translatedInfo = '';
44
47
60 public function __construct(
66 string $formAction = '',
67 string $saveCommand = 'saveDocument',
68 string $cancelCommand = 'showDocuments',
69 bool $isEditable = false
70 ) {
71 $this->document = $document;
72 $this->documentPurifier = $documentPurifier;
73 $this->actor = $actor;
74 $this->tmpFileSystem = $tmpFileSystem;
75 $this->fileUpload = $fileUpload;
76 $this->formAction = $formAction;
77 $this->saveCommand = $saveCommand;
78 $this->cancelCommand = $cancelCommand;
79 $this->isEditable = $isEditable;
80
82
83 $this->initForm();
84 }
85
89 public function setCheckInputCalled(bool $status) : void
90 {
91 $this->check_input_called = $status;
92 }
93
97 protected function initForm() : void
98 {
99 if ($this->document->getId() > 0) {
100 $this->setTitle($this->lng->txt('acc_form_edit_doc_head'));
101 } else {
102 $this->setTitle($this->lng->txt('acc_form_new_doc_head'));
103 }
104
105 $this->setFormAction($this->formAction);
106
107 $title = new ilTextInputGUI($this->lng->txt('acc_form_document_title'), 'title');
108 $title->setInfo($this->lng->txt('acc_form_document_title_info'));
109 $title->setRequired(true);
110 $title->setDisabled(!$this->isEditable);
111 $title->setValue($this->document->getTitle());
112 $title->setMaxLength(255);
113 $this->addItem($title);
114
115 $documentLabel = $this->lng->txt('acc_form_document');
116 $documentByline = $this->lng->txt('acc_form_document_info');
117 if ($this->document->getId() > 0) {
118 $documentLabel = $this->lng->txt('acc_form_document_new');
119 $documentByline = $this->lng->txt('acc_form_document_new_info');
120 }
121
122 $document = new ilFileInputGUI($documentLabel, 'document');
123 $document->setInfo($documentByline);
124 if (!$this->document->getId()) {
125 $document->setRequired(true);
126 }
127 $document->setDisabled(!$this->isEditable);
128 $document->setSuffixes(['html', 'txt']);
129 $this->addItem($document);
130
131 if ($this->isEditable) {
132 $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
133 }
134
135 $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
136 }
137
141 public function hasTranslatedError() : bool
142 {
143 return strlen($this->translatedError) > 0;
144 }
145
149 public function getTranslatedError() : string
150 {
152 }
153
157 public function hasTranslatedInfo() : bool
158 {
159 return strlen($this->translatedInfo) > 0;
160 }
161
165 public function getTranslatedInfo() : string
166 {
168 }
169
173 public function saveObject() : bool
174 {
175 if (!$this->fillObject()) {
176 $this->setValuesByPost();
177 return false;
178 }
179
180 $this->document->save();
181
182 return true;
183 }
184
188 protected function fillObject() : bool
189 {
190 if (!$this->checkInput()) {
191 return false;
192 }
193
194 if ($this->fileUpload->hasUploads() && !$this->fileUpload->hasBeenProcessed()) {
195 try {
196 $this->fileUpload->process();
197
199 $uploadResult = array_values($this->fileUpload->getResults())[0];
200 if (!$uploadResult) {
201 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
202 throw new ilException($this->lng->txt('form_input_not_valid'));
203 }
204
205 if (!$this->document->getId() || $uploadResult->getName() !== '') {
206 if ($uploadResult->getStatus()->getCode() != ProcessingStatus::OK) {
207 $this->getItemByPostVar('document')->setAlert($uploadResult->getStatus()->getMessage());
208 throw new ilException($this->lng->txt('form_input_not_valid'));
209 }
210
211 $this->fileUpload->moveOneFileTo(
212 $uploadResult,
213 '/agreements',
214 Location::TEMPORARY,
215 '',
216 true
217 );
218
219 $pathToFile = '/agreements/' . $uploadResult->getName();
220 if (!$this->tmpFileSystem->has($pathToFile)) {
221 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
222 throw new ilException($this->lng->txt('form_input_not_valid'));
223 }
224
225 $originalContent = $content = $this->tmpFileSystem->read($pathToFile);
226
227 $purifiedHtmlContent = $this->documentPurifier->purify($content);
228
229 $htmlValidator = new ilAccessibilityDocumentsContainsHtmlValidator($purifiedHtmlContent);
230 if (!$htmlValidator->isValid()) {
231 $purifiedHtmlContent = nl2br($purifiedHtmlContent);
232 }
233
234 if (trim($purifiedHtmlContent) !== trim($originalContent)) {
235 $this->translatedInfo = $this->lng->txt('acc_form_document_content_changed');
236 }
237
238 $this->document->setText($purifiedHtmlContent);
239 $this->tmpFileSystem->delete($pathToFile);
240 }
241 } catch (Exception $e) {
242 $this->translatedError = $e->getMessage();
243 return false;
244 }
245 }
246
247 $this->document->setTitle($this->getInput('title'));
248
249 if ($this->document->getId() > 0) {
250 $this->document->setLastModifiedUsrId($this->actor->getId());
251 } else {
252 $this->document->setOwnerUsrId($this->actor->getId());
253
254 $documentWithMaxSorting = ilAccessibilityDocument::orderBy('sorting', 'DESC')->limit(0, 1)->first();
255 if ($documentWithMaxSorting instanceof ilAccessibilityDocument) {
256 $this->document->setSorting((int) $documentWithMaxSorting->getSorting() + 1);
257 } else {
258 $this->document->setSorting(1);
259 }
260 }
261
262 return true;
263 }
264}
static orderBy($orderBy, $orderDirection='ASC')
An exception for terminatinating execution or to throw for unit testing.
Class ilAccessibilityDocumentFormGUI.
__construct(ilAccessibilityDocument $document, ilHtmlPurifierInterface $documentPurifier, ilObjUser $actor, Filesystem $tmpFileSystem, FileUpload $fileUpload, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments', bool $isEditable=false)
ilAccessibilityDocumentFormGUI constructor.
Class ilAccessibilityDocument.
Base class for ILIAS Exception handling.
This class represents a file property in a property form.
setFormAction($a_formaction)
Set FormAction.
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 text property in a property form.
Interface Location.
Definition: Location.php:17
Interface for html sanitizing functionality.
Class FlySystemFileAccessTest.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc