ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADTFormBridge.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
26abstract class ilADTFormBridge
27{
28 protected ilADT $adt;
33 protected $parent;
34 protected ?string $id = null;
35 protected string $title = '';
36 protected string $info = '';
37 protected $parent_element; // [string|array]
38 protected bool $required = false;
39 protected bool $disabled = false;
40
41 protected ilLanguage $lng;
42
43 public function __construct(ilADT $a_adt)
44 {
45 global $DIC;
46
47 $this->lng = $DIC->language();
48
49 $this->setADT($a_adt);
50 }
51
52 abstract protected function isValidADT(ilADT $a_adt): bool;
53
54 protected function setADT(ilADT $a_adt): void
55 {
56 if (!$this->isValidADT($a_adt)) {
57 throw new InvalidArgumentException('ADTFormBridge Type mismatch.');
58 }
59 $this->adt = $a_adt;
60 }
61
62 public function getADT(): ilADT
63 {
64 return $this->adt;
65 }
66
67 public function setForm(ilPropertyFormGUI $a_form): void
68 {
69 $this->form = $a_form;
70 }
71
72 public function getForm(): ?ilPropertyFormGUI
73 {
74 return $this->form;
75 }
76
81 public function setElementId(string $a_value): void
82 {
83 $this->id = $a_value;
84 }
85
86 public function getElementId(): ?string
87 {
88 return $this->id;
89 }
90
91 public function setTitle(string $a_value): void
92 {
93 $this->title = trim($a_value);
94 }
95
96 public function getTitle(): string
97 {
98 return $this->title;
99 }
100
101 public function setInfo(string $a_value): void
102 {
103 $this->info = trim($a_value);
104 }
105
106 public function getInfo(): string
107 {
108 return $this->info;
109 }
110
114 public function setParentElement($a_value): void
115 {
116 if (!is_array($a_value)) {
117 $a_value = (string) $a_value;
118 }
119 $this->parent = $a_value;
120 }
121
126 public function getParentElement()
127 {
128 return $this->parent;
129 }
130
131 public function setDisabled(bool $a_value): void
132 {
133 $this->disabled = $a_value;
134 }
135
136 public function isDisabled(): bool
137 {
138 return $this->disabled;
139 }
140
141 public function setRequired(bool $a_value): void
142 {
143 $this->required = $a_value;
144 }
145
146 public function isRequired(): bool
147 {
148 return $this->required;
149 }
150
156 protected function addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def): void
157 {
158 if ($this->isDisabled()) {
159 $a_field->setDisabled(true);
160 } elseif ($this->isRequired()) {
161 $a_field->setRequired(true);
162 }
163
164 $info = $this->getInfo();
165 if ($info) {
166 $a_field->setInfo($info);
167 }
168 }
169
171 {
172 $parent_def = $this->getParentElement();
173 $parent_option = '';
174 if ($parent_def) {
175 if (is_array($parent_def)) {
176 $parent_option = $parent_def[1];
177 $parent_def = $parent_def[0];
178 }
179 $parent_field = $this->getForm()->getItemByPostVar($parent_def);
180 if ($parent_field instanceof ilSubEnabledFormPropertyGUI) {
181 // radio/checkbox group
182 if ($parent_option && method_exists($parent_field, "getOptions")) {
183 foreach ($parent_field->getOptions() as $option) {
184 if ($option->getValue() == $parent_option) {
185 $parent_field = $option;
186 break;
187 }
188 }
189 }
190 }
191
192 if ($parent_field) {
193 return $parent_field;
194 }
195 }
196 return null;
197 }
198
199 protected function addToParentElement(ilFormPropertyGUI $a_field): void
200 {
201 $field = $this->findParentElementInForm();
202 if ($field instanceof ilSubEnabledFormPropertyGUI) {
203 $field->addSubItem($a_field);
204 } elseif ($this->getForm() instanceof ilPropertyFormGUI) {
205 $this->getForm()->addItem($a_field);
206 }
207 }
208
212 abstract public function addToForm(): void;
213
218 public function addJS(ilGlobalTemplateInterface $a_tpl): void
219 {
220 }
221
227 protected function isActiveForSubItems($a_parent_option = null): bool
228 {
229 return !$this->getADT()->isNull();
230 }
231
237 public function shouldBeImportedFromPost(?ilADTFormBridge $a_parent_adt = null): bool
238 {
239 if ($this->isDisabled()) {
240 return false;
241 }
242
243 // inactive parent elements disable importing
244 if ($a_parent_adt) {
245 $parent_option = null;
247 if (is_array($parent_element)) {
248 $parent_option = $parent_element[1];
249 }
250 return $a_parent_adt->isActiveForSubItems($parent_option);
251 }
252 return true;
253 }
254
258 abstract public function importFromPost(): void;
259
260 public function validate(): bool
261 {
262 // ilADTFormBridge->isRequired() != ilADT->allowNull()
263 if ($this->isRequired() && $this->getADT()->isNull()) {
264 $field = $this->getForm()->getItemByPostVar($this->getElementId());
265 $field->setAlert($this->lng->txt("msg_input_is_required"));
266 return false;
267 } // no need to further validate if no value given
268 elseif (!$this->getADT()->isValid()) {
269 $tmp = [];
270 $mess = $this->getADT()->getValidationErrors();
271 foreach ($mess as $error_code) {
272 $tmp[] = $this->getADT()->translateErrorCode($error_code);
273 }
274 $field = $this->getForm()->getItemByPostVar($this->getElementId());
275 $field->setAlert(implode("<br />", $tmp));
276 return false;
277 }
278 return true;
279 }
280
281 public function setExternalErrors(array $a_errors): void
282 {
283 $field = $this->getForm()->getItemByPostVar($this->getElementId());
284 $field->setAlert(implode("<br />", $a_errors));
285 }
286}
ADT definition base class.
ADT form bridge base class.
__construct(ilADT $a_adt)
isValidADT(ilADT $a_adt)
ilPropertyFormGUI $form
addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
Helper method to handle generic properties like setRequired(), setInfo()
shouldBeImportedFromPost(?ilADTFormBridge $a_parent_adt=null)
Check if incoming values should be imported at all.
setElementId(string $a_value)
Set element id (aka form field)
addToParentElement(ilFormPropertyGUI $a_field)
setRequired(bool $a_value)
addJS(ilGlobalTemplateInterface $a_tpl)
Add ADT-specific JS-files to template.
importFromPost()
Import values from form request POST data.
setTitle(string $a_value)
setExternalErrors(array $a_errors)
isActiveForSubItems($a_parent_option=null)
Check if element is currently active for subitem(s)
addToForm()
Add ADT-specific fields to form.
setInfo(string $a_value)
getParentElement()
Get parent element.
setForm(ilPropertyFormGUI $a_form)
setDisabled(bool $a_value)
ADT base class.
Definition: class.ilADT.php:26
This class represents a property in a property form.
setRequired(bool $a_required)
setDisabled(bool $a_disabled)
language handling
This class represents a property form user interface.
This class represents a property that may include a sub form.
form( $class_path, string $cmd, string $submit_caption="")
global $DIC
Definition: shib_login.php:26