ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTermsOfServiceDocumentFormGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 
15 {
17  protected $document;
18 
20  protected $actor;
21 
23  protected $fileUpload;
24 
26  protected $tmpFileSystem;
27 
29  protected $formAction;
30 
32  protected $saveCommand;
33 
35  protected $cancelCommand;
36 
38  protected $isEditable = false;
39 
41  protected $translatedError = '';
42 
44  protected $translatedInfo = '';
45 
47  protected $documentPurifier;
48 
61  public function __construct(
67  string $formAction = '',
68  string $saveCommand = 'saveDocument',
69  string $cancelCommand = 'showDocuments',
70  bool $isEditable = false
71  ) {
72  $this->document = $document;
73  $this->documentPurifier = $documentPurifier;
74  $this->actor = $actor;
75  $this->tmpFileSystem = $tmpFileSystem;
76  $this->fileUpload = $fileUpload;
77  $this->formAction = $formAction;
78  $this->saveCommand = $saveCommand;
79  $this->cancelCommand = $cancelCommand;
80  $this->isEditable = $isEditable;
81 
82  parent::__construct();
83 
84  $this->initForm();
85  }
86 
90  public function setCheckInputCalled(bool $status)
91  {
92  $this->check_input_called = $status;
93  }
94 
98  protected function initForm()
99  {
100  if ($this->document->getId() > 0) {
101  $this->setTitle($this->lng->txt('tos_form_edit_doc_head'));
102  } else {
103  $this->setTitle($this->lng->txt('tos_form_new_doc_head'));
104  }
105 
106  $this->setFormAction($this->formAction);
107 
108  $title = new \ilTextInputGUI($this->lng->txt('tos_form_document_title'), 'title');
109  $title->setInfo($this->lng->txt('tos_form_document_title_info'));
110  $title->setRequired(true);
111  $title->setDisabled(!$this->isEditable);
112  $title->setValue($this->document->getTitle());
113  $title->setMaxLength(255);
114  $this->addItem($title);
115 
116  $documentLabel = $this->lng->txt('tos_form_document');
117  $documentByline = $this->lng->txt('tos_form_document_info');
118  if ($this->document->getId() > 0) {
119  $documentLabel = $this->lng->txt('tos_form_document_new');
120  $documentByline = $this->lng->txt('tos_form_document_new_info');
121  }
122 
123  $document = new ilFileInputGUI($documentLabel, 'document');
124  $document->setInfo($documentByline);
125  if (!$this->document->getId()) {
126  $document->setRequired(true);
127  }
128  $document->setDisabled(!$this->isEditable);
129  $document->setSuffixes(['html', 'txt']);
130  $this->addItem($document);
131 
132  if ($this->isEditable) {
133  $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
134  }
135 
136  $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
137  }
138 
142  public function hasTranslatedError() : bool
143  {
144  return strlen($this->translatedError);
145  }
146 
150  public function getTranslatedError() : string
151  {
152  return $this->translatedError;
153  }
154 
158  public function hasTranslatedInfo() : bool
159  {
160  return strlen($this->translatedInfo);
161  }
162 
166  public function getTranslatedInfo() : string
167  {
168  return $this->translatedInfo;
169  }
170 
174  public function saveObject() : bool
175  {
176  if (!$this->fillObject()) {
177  $this->setValuesByPost();
178  return false;
179  }
180 
181  $this->document->save();
182 
183  return true;
184  }
185 
189  protected function fillObject() : bool
190  {
191  if (!$this->checkInput()) {
192  return false;
193  }
194 
195  if ($this->fileUpload->hasUploads() && !$this->fileUpload->hasBeenProcessed()) {
196  try {
197  $this->fileUpload->process();
198 
200  $uploadResult = array_values($this->fileUpload->getResults())[0];
201  if (!$uploadResult) {
202  $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
203  throw new \ilException($this->lng->txt('form_input_not_valid'));
204  }
205 
206  if (!$this->document->getId() || $uploadResult->getName() !== '') {
207  if ($uploadResult->getStatus()->getCode() != ProcessingStatus::OK) {
208  $this->getItemByPostVar('document')->setAlert($uploadResult->getStatus()->getMessage());
209  throw new \ilException($this->lng->txt('form_input_not_valid'));
210  }
211 
212  $this->fileUpload->moveOneFileTo(
213  $uploadResult,
214  '/agreements',
215  Location::TEMPORARY,
216  '',
217  true
218  );
219 
220  $pathToFile = '/agreements/' . $uploadResult->getName();
221  if (!$this->tmpFileSystem->has($pathToFile)) {
222  $this->getItemByPostVar('document')->setAlert($this->lng->txt('form_msg_file_no_upload'));
223  throw new \ilException($this->lng->txt('form_input_not_valid'));
224  }
225 
226  $originalContent = $content = $this->tmpFileSystem->read($pathToFile);
227 
228  $purifiedHtmlContent = $this->documentPurifier->purify($content);
229 
230  $htmlValidator = new ilTermsOfServiceDocumentsContainsHtmlValidator($purifiedHtmlContent);
231  if (!$htmlValidator->isValid()) {
232  $purifiedHtmlContent = nl2br($purifiedHtmlContent);
233  }
234 
235  if (trim($purifiedHtmlContent) !== trim($originalContent)) {
236  $this->translatedInfo = $this->lng->txt('tos_form_document_content_changed');
237  }
238 
239  $this->document->setText($purifiedHtmlContent);
240  $this->tmpFileSystem->delete($pathToFile);
241  }
242  } catch (Exception $e) {
243  $this->translatedError = $e->getMessage();
244  return false;
245  }
246  }
247 
248  $this->document->setTitle($this->getInput('title'));
249 
250  if ($this->document->getId() > 0) {
251  $this->document->setLastModifiedUsrId($this->actor->getId());
252  } else {
253  $this->document->setOwnerUsrId($this->actor->getId());
254 
255  $documentWithMaxSorting = \ilTermsOfServiceDocument::orderBy('sorting', 'DESC')->limit(0, 1)->first();
256  if ($documentWithMaxSorting instanceof \ilTermsOfServiceDocument) {
257  $this->document->setSorting((int) $documentWithMaxSorting->getSorting() + 1);
258  } else {
259  $this->document->setSorting(1);
260  }
261  }
262 
263  return true;
264  }
265 }
Class ilTermsOfServiceDocumentFormGUI.
getItemByPostVar($a_post_var)
Get Item by POST variable.
This class represents a property form user interface.
This class represents a file property in a property form.
__construct(\ilTermsOfServiceDocument $document, \ilHtmlPurifierInterface $documentPurifier, \ilObjUser $actor, Filesystem $tmpFileSystem, FileUpload $fileUpload, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments', bool $isEditable=false)
ilTermsOfServiceDocumentFormGUI constructor.
setFormAction($a_formaction)
Set FormAction.
addItem($a_item)
Add Item (Property, SectionHeader).
Interface for html sanitizing functionality.
setTitle($a_title)
Set Title.
Class ilTermsOfServiceDocument.
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
checkInput()
Check Post Input.
Class FileUpload.
Definition: FileUpload.php:21
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
setValuesByPost()
Set form values from POST values.
static orderBy($orderBy, $orderDirection='ASC')
Class FlySystemFileAccessTest.