ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilTermsOfServiceCriterionFormGUI.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
5
11{
13 protected $document;
14
16 protected $assignment;
17
19 protected $formAction;
20
22 protected $actor;
23
25 protected $saveCommand;
26
28 protected $cancelCommand;
29
31 protected $translatedError = '';
32
35
46 public function __construct(
51 string $formAction = '',
52 string $saveCommand = 'saveDocument',
53 string $cancelCommand = 'showDocuments'
54 ) {
55 $this->document = $document;
56 $this->assignment = $assignment;
57 $this->criterionTypeFactory = $criterionTypeFactory;
58 $this->actor = $actor;
59 $this->formAction = $formAction;
60 $this->saveCommand = $saveCommand;
61 $this->cancelCommand = $cancelCommand;
62
64
65 $this->initForm();
66 }
67
71 public function setCheckInputCalled(bool $status) : void
72 {
73 $this->check_input_called = $status;
74 }
75
79 protected function initForm() : void
80 {
81 if ($this->assignment->getId() > 0) {
82 $this->setTitle($this->lng->txt('tos_form_edit_criterion_head'));
83 } else {
84 $this->setTitle($this->lng->txt('tos_form_attach_criterion_head'));
85 }
86
87 $this->setFormAction($this->formAction);
88
89 $document = new ilNonEditableValueGUI($this->lng->txt('tos_document'));
90 $document->setValue($this->document->getTitle());
91 $this->addItem($document);
92
93 $criteriaSelection = new ilRadioGroupInputGUI($this->lng->txt('tos_form_criterion'), 'criterion');
94 $criteriaSelection->setRequired(true);
95 $criteriaSelection->setValue($this->assignment->getCriterionId());
96
97 $first = true;
98 foreach ($this->criterionTypeFactory->getTypesByIdentMap() as $criterion) {
100 if (!$this->assignment->getId() && $first) {
101 $criteriaSelection->setValue($criterion->getTypeIdent());
102 }
103 $first = false;
104
105 $criterionGui = $criterion->ui($this->lng);
106 if ($this->assignment->getCriterionId() == $criterion->getTypeIdent()) {
107 $criterionGui->appendOption(
108 $criteriaSelection,
109 $this->assignment->getCriterionValue()
110 );
111 } else {
112 $criterionGui->appendOption($criteriaSelection, new ilTermsOfServiceCriterionConfig());
113 }
114 }
115 $this->addItem($criteriaSelection);
116
117 $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
118 $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
119 }
120
124 public function hasTranslatedError() : bool
125 {
126 return strlen($this->translatedError) > 0;
127 }
128
132 public function getTranslatedError() : string
133 {
135 }
136
141 public function saveObject() : bool
142 {
143 if (!$this->fillObject()) {
144 $this->setValuesByPost();
145 return false;
146 }
147
148 $uniqueAssignmentConstraint = new ilTermsOfServiceDocumentCriterionAssignmentConstraint(
149 $this->criterionTypeFactory,
150 $this->document,
151 new Factory(),
152 $this->lng
153 );
154
155 if (!$uniqueAssignmentConstraint->accepts($this->assignment)) {
156 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_insert'));
157 if ($this->assignment->getId() > 0) {
158 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_update'));
159 }
160
161 $this->translatedError = $this->lng->txt('form_input_not_valid');
162 $this->setValuesByPost();
163 return false;
164 }
165
166 if (!$this->assignment->getId()) {
167 $this->document->attachCriterion($this->assignment);
168 }
169 $this->document->save();
170
171 return true;
172 }
173
177 protected function fillObject() : bool
178 {
179 if (!$this->checkInput()) {
180 return false;
181 }
182
183 try {
184 $criterionType = $this->criterionTypeFactory->findByTypeIdent($this->getInput('criterion'));
185 $criterionGui = $criterionType->ui($this->lng);
186
187 $this->assignment->setCriterionId($criterionType->getTypeIdent());
188 $this->assignment->setCriterionValue($criterionGui->getConfigByForm($this));
189
190 if ($this->assignment->getId() > 0) {
191 $this->assignment->setLastModifiedUsrId($this->actor->getId());
192 } else {
193 $this->assignment->setOwnerUsrId($this->actor->getId());
194 }
195 } catch (Exception $e) {
196 $this->getItemByPostVar('criterion')->setAlert($e->getMessage());
197 $this->translatedError = $this->lng->txt('form_input_not_valid');
198 return false;
199 }
200
201 return true;
202 }
203}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
setFormAction($a_formaction)
Set FormAction.
This class represents a non editable value in a property form.
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 property in a property form.
Class ilTermsOfServiceCriterionConfig.
__construct(ilTermsOfServiceDocument $document, ilTermsOfServiceDocumentCriterionAssignment $assignment, ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, ilObjUser $actor, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments')
ilTermsOfServiceCriterionFormGUI constructor.
Class ilTermsOfServiceDocument.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc