ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAccessibilityDocumentFormGUI.php
Go to the documentation of this file.
1<?php
2
24
29{
31 protected ilObjUser $actor;
34 protected string $formAction;
35 protected string $saveCommand;
36 protected string $cancelCommand;
37 protected bool $isEditable = false;
38 protected string $translatedError = '';
39 protected string $translatedInfo = '';
41
42 public function __construct(
48 string $formAction = '',
49 string $saveCommand = 'saveDocument',
50 string $cancelCommand = 'showDocuments',
51 bool $isEditable = false
52 ) {
53 $this->document = $document;
54 $this->documentPurifier = $documentPurifier;
55 $this->actor = $actor;
56 $this->tmpFileSystem = $tmpFileSystem;
57 $this->fileUpload = $fileUpload;
58 $this->formAction = $formAction;
59 $this->saveCommand = $saveCommand;
60 $this->cancelCommand = $cancelCommand;
61 $this->isEditable = $isEditable;
62
64
65 $this->initForm();
66 }
67
68 public function setCheckInputCalled(bool $status): void
69 {
70 $this->check_input_called = $status;
71 }
72
73 protected function initForm(): void
74 {
75 if ($this->document->getId() > 0) {
76 $this->setTitle($this->lng->txt('acc_form_edit_doc_head'));
77 } else {
78 $this->setTitle($this->lng->txt('acc_form_new_doc_head'));
79 }
80
81 $this->setFormAction($this->formAction);
82
83 $title = new ilTextInputGUI($this->lng->txt('acc_form_document_title'), 'title');
84 $title->setInfo($this->lng->txt('acc_form_document_title_info'));
85 $title->setRequired(true);
86 $title->setDisabled(!$this->isEditable);
87 $title->setValue($this->document->getTitle());
88 $title->setMaxLength(255);
89 $this->addItem($title);
90
91 $documentLabel = $this->lng->txt('acc_form_document');
92 $documentByline = $this->lng->txt('acc_form_document_info');
93 if ($this->document->getId() > 0) {
94 $documentLabel = $this->lng->txt('acc_form_document_new');
95 $documentByline = $this->lng->txt('acc_form_document_new_info');
96 }
97
98 $document = new ilFileInputGUI($documentLabel, 'document');
99 $document->setInfo($documentByline);
100 if (!$this->document->getId()) {
101 $document->setRequired(true);
102 }
103 $document->setDisabled(!$this->isEditable);
104 $document->setSuffixes(['html', 'txt']);
105 $this->addItem($document);
106
107 if ($this->isEditable) {
108 $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
109 }
110
111 $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
112 }
113
114 public function hasTranslatedError(): bool
115 {
116 return strlen($this->translatedError) > 0;
117 }
118
119 public function getTranslatedError(): string
120 {
122 }
123
124 public function hasTranslatedInfo(): bool
125 {
126 return strlen($this->translatedInfo) > 0;
127 }
128
129 public function getTranslatedInfo(): string
130 {
132 }
133
134 public function saveObject(): bool
135 {
136 if (!$this->fillObject()) {
137 $this->setValuesByPost();
138 return false;
139 }
140
141 $this->document->save();
142
143 return true;
144 }
145
146 protected function fillObject(): bool
147 {
148 if (!$this->checkInput()) {
149 return false;
150 }
151
152 if ($this->fileUpload->hasUploads() && !$this->fileUpload->hasBeenProcessed()) {
153 $this->fileUpload->process();
154 }
155
156 if ($this->fileUpload->hasUploads()) {
157 try {
159 $uploadResult = array_values($this->fileUpload->getResults())[0];
160 if (!$uploadResult) {
161 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
162 throw new ilException($this->lng->txt('form_input_not_valid'));
163 }
164
165 if (!$this->document->getId() || $uploadResult->getName() !== '') {
166 if (!$uploadResult->isOK()) {
167 $this->getItemByPostVar('document')->setAlert($uploadResult->getStatus()->getMessage());
168 throw new ilException($this->lng->txt('form_input_not_valid'));
169 }
170
171 $this->fileUpload->moveOneFileTo(
172 $uploadResult,
173 '/agreements',
174 Location::TEMPORARY,
175 '',
176 true
177 );
178
179 $pathToFile = '/agreements/' . $uploadResult->getName();
180 if (!$this->tmpFileSystem->has($pathToFile)) {
181 $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
182 throw new ilException($this->lng->txt('form_input_not_valid'));
183 }
184
185 $originalContent = $content = $this->tmpFileSystem->read($pathToFile);
186
187 $purifiedHtmlContent = $this->documentPurifier->purify($content);
188
189 $htmlValidator = new ilAccessibilityDocumentsContainsHtmlValidator($purifiedHtmlContent);
190 if (!$htmlValidator->isValid()) {
191 $purifiedHtmlContent = nl2br($purifiedHtmlContent);
192 }
193
194 if (trim($purifiedHtmlContent) !== trim($originalContent)) {
195 $this->translatedInfo = $this->lng->txt('acc_form_document_content_changed');
196 }
197
198 $this->document->setText($purifiedHtmlContent);
199 $this->tmpFileSystem->delete($pathToFile);
200 }
201 } catch (Exception $e) {
202 $this->translatedError = $e->getMessage();
203 return false;
204 }
205 }
206
207 $this->document->setTitle($this->getInput('title'));
208
209 if ($this->document->getId() > 0) {
210 $this->document->setLastModifiedUsrId($this->actor->getId());
211 } else {
212 $this->document->setOwnerUsrId($this->actor->getId());
213
214 $documentWithMaxSorting = ilAccessibilityDocument::orderBy('sorting', 'DESC')->limit(0, 1)->first();
215 if ($documentWithMaxSorting instanceof ilAccessibilityDocument) {
216 $this->document->setSorting((int) $documentWithMaxSorting->getSorting() + 1);
217 } else {
218 $this->document->setSorting(1);
219 }
220 }
221
222 return true;
223 }
224}
static orderBy(string $orderBy, string $orderDirection='ASC')
Class ilAccessibilityDocumentFormGUI.
__construct(ilAccessibilityDocument $document, ilHtmlPurifierInterface $documentPurifier, ilObjUser $actor, Filesystem $tmpFileSystem, FileUpload $fileUpload, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments', bool $isEditable=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Base class for ILIAS Exception handling.
This class represents a file property in a property form.
setFormAction(string $a_formaction)
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 text property in a property form.
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