ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTermsOfServiceDocumentFormGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
31 {
33  protected ilObjUser $actor;
36  protected string $formAction;
37  protected string $saveCommand;
38  protected string $cancelCommand;
39  protected bool $isEditable = false;
40  protected string $translatedError = '';
41  protected string $translatedInfo = '';
43 
44  public function __construct(
45  ilTermsOfServiceDocument $document,
46  ilHtmlPurifierInterface $documentPurifier,
47  ilObjUser $actor,
48  Filesystem $tmpFileSystem,
49  FileUpload $fileUpload,
50  string $formAction = '',
51  string $saveCommand = 'saveDocument',
52  string $cancelCommand = 'showDocuments',
53  bool $isEditable = false
54  ) {
55  $this->document = $document;
56  $this->documentPurifier = $documentPurifier;
57  $this->actor = $actor;
58  $this->tmpFileSystem = $tmpFileSystem;
59  $this->fileUpload = $fileUpload;
60  $this->formAction = $formAction;
61  $this->saveCommand = $saveCommand;
62  $this->cancelCommand = $cancelCommand;
63  $this->isEditable = $isEditable;
64 
66 
67  $this->initForm();
68  }
69 
70  public function setCheckInputCalled(bool $status): void
71  {
72  $this->check_input_called = $status;
73  }
74 
75  protected function initForm(): void
76  {
77  if ($this->document->getId() > 0) {
78  $this->setTitle($this->lng->txt('tos_form_edit_doc_head'));
79  } else {
80  $this->setTitle($this->lng->txt('tos_form_new_doc_head'));
81  }
82 
83  $this->setFormAction($this->formAction);
84 
85  $title = new ilTextInputGUI($this->lng->txt('tos_form_document_title'), 'title');
86  $title->setInfo($this->lng->txt('tos_form_document_title_info'));
87  $title->setRequired(true);
88  $title->setDisabled(!$this->isEditable);
89  $title->setValue($this->document->getTitle());
90  $title->setMaxLength(255);
91  $this->addItem($title);
92 
93  $documentLabel = $this->lng->txt('tos_form_document');
94  $documentByline = $this->lng->txt('tos_form_document_info');
95  if ($this->document->getId() > 0) {
96  $documentLabel = $this->lng->txt('tos_form_document_new');
97  $documentByline = $this->lng->txt('tos_form_document_new_info');
98  }
99 
100  $document = new ilFileInputGUI($documentLabel, 'document');
101  $document->setInfo($documentByline);
102  if (!$this->document->getId()) {
103  $document->setRequired(true);
104  }
105  $document->setDisabled(!$this->isEditable);
106  $document->setSuffixes(['html', 'txt']);
107  $this->addItem($document);
108 
109  if ($this->isEditable) {
110  $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
111  }
112 
113  $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
114  }
115 
116  public function hasTranslatedError(): bool
117  {
118  return $this->translatedError !== '';
119  }
120 
121  public function getTranslatedError(): string
122  {
123  return $this->translatedError;
124  }
125 
126  public function hasTranslatedInfo(): bool
127  {
128  return $this->translatedInfo !== '';
129  }
130 
131  public function getTranslatedInfo(): string
132  {
133  return $this->translatedInfo;
134  }
135 
136  public function saveObject(): bool
137  {
138  if (!$this->fillObject()) {
139  $this->setValuesByPost();
140  return false;
141  }
142 
143  $this->document->save();
144 
145  return true;
146  }
147 
148  protected function fillObject(): bool
149  {
150  if (!$this->checkInput()) {
151  return false;
152  }
153 
154  if ($this->fileUpload->hasUploads() && !$this->fileUpload->hasBeenProcessed()) {
155  $this->fileUpload->process();
156  }
157 
158  if ($this->fileUpload->hasUploads()) {
159  try {
161  $uploadResult = array_values($this->fileUpload->getResults())[0];
162  if (!($uploadResult instanceof UploadResult)) {
163  $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
164  throw new ilException($this->lng->txt('form_input_not_valid'));
165  }
166 
167  if (!$this->document->getId() || $uploadResult->getName() !== '') {
168  if (!$uploadResult->isOK()) {
169  $this->getItemByPostVar('document')->setAlert($uploadResult->getStatus()->getMessage());
170  throw new ilException($this->lng->txt('form_input_not_valid'));
171  }
172 
173  $this->fileUpload->moveOneFileTo(
174  $uploadResult,
175  '/agreements',
176  Location::TEMPORARY,
177  '',
178  true
179  );
180 
181  $pathToFile = '/agreements/' . $uploadResult->getName();
182  if (!$this->tmpFileSystem->has($pathToFile)) {
183  $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
184  throw new ilException($this->lng->txt('form_input_not_valid'));
185  }
186 
187  $originalContent = $content = $this->tmpFileSystem->read($pathToFile);
188 
189  $purifiedHtmlContent = $this->documentPurifier->purify($content);
190 
191  $htmlValidator = new ilTermsOfServiceDocumentsContainsHtmlValidator($purifiedHtmlContent);
192  if (!$htmlValidator->isValid()) {
193  $purifiedHtmlContent = nl2br($purifiedHtmlContent);
194  }
195 
196  if (trim($purifiedHtmlContent) !== trim($originalContent)) {
197  $this->translatedInfo = $this->lng->txt('tos_form_document_content_changed');
198  }
199 
200  $this->document->setText($purifiedHtmlContent);
201  $this->tmpFileSystem->delete($pathToFile);
202  }
203  } catch (Exception $e) {
204  $this->translatedError = $e->getMessage();
205  return false;
206  }
207  }
208 
209  $this->document->setTitle($this->getInput('title'));
210 
211  if ($this->document->getId() > 0) {
212  $this->document->setLastModifiedUsrId($this->actor->getId());
213  } else {
214  $this->document->setOwnerUsrId($this->actor->getId());
215 
216  $documentWithMaxSorting = ilTermsOfServiceDocument::orderBy('sorting', 'DESC')->limit(0, 1)->first();
217  if ($documentWithMaxSorting instanceof ilTermsOfServiceDocument) {
218  $this->document->setSorting((int) $documentWithMaxSorting->getSorting() + 1);
219  } else {
220  $this->document->setSorting(1);
221  }
222  }
223 
224  return true;
225  }
226 }
Class ilTermsOfServiceDocumentFormGUI.
static orderBy($orderBy, string $orderDirection='ASC')
getItemByPostVar(string $a_post_var)
This class represents a file property in a property form.
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-...
Interface for html sanitizing functionality.
Class ilTermsOfServiceDocument.
setFormAction(string $a_formaction)
Class FileUpload.
Definition: FileUpload.php:34
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
__construct(Container $dic, ilPlugin $plugin)
__construct(ilTermsOfServiceDocument $document, ilHtmlPurifierInterface $documentPurifier, ilObjUser $actor, Filesystem $tmpFileSystem, FileUpload $fileUpload, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments', bool $isEditable=false)
Class FlySystemFileAccessTest disabled disabled disabled.