ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilADTFormBridge.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
12 abstract class ilADTFormBridge
13 {
14  protected ilADT $adt;
19  protected $parent;
20  protected ?string $id = null;
21  protected string $title = '';
22  protected string $info = '';
23  protected $parent_element; // [string|array]
24  protected bool $required = false;
25  protected bool $disabled = false;
26 
27  protected ilLanguage $lng;
28 
29  public function __construct(ilADT $a_adt)
30  {
31  global $DIC;
32 
33  $this->lng = $DIC->language();
34 
35  $this->setADT($a_adt);
36  }
37 
38  abstract protected function isValidADT(ilADT $a_adt): bool;
39 
40  protected function setADT(ilADT $a_adt): void
41  {
42  if (!$this->isValidADT($a_adt)) {
43  throw new InvalidArgumentException('ADTFormBridge Type mismatch.');
44  }
45  $this->adt = $a_adt;
46  }
47 
48  public function getADT(): ilADT
49  {
50  return $this->adt;
51  }
52 
53  public function setForm(ilPropertyFormGUI $a_form): void
54  {
55  $this->form = $a_form;
56  }
57 
58  public function getForm(): ?ilPropertyFormGUI
59  {
60  return $this->form;
61  }
62 
67  public function setElementId(string $a_value): void
68  {
69  $this->id = $a_value;
70  }
71 
72  public function getElementId(): ?string
73  {
74  return $this->id;
75  }
76 
77  public function setTitle(string $a_value): void
78  {
79  $this->title = trim($a_value);
80  }
81 
82  public function getTitle(): string
83  {
84  return $this->title;
85  }
86 
87  public function setInfo(string $a_value): void
88  {
89  $this->info = trim($a_value);
90  }
91 
92  public function getInfo(): string
93  {
94  return $this->info;
95  }
96 
100  public function setParentElement($a_value): void
101  {
102  if (!is_array($a_value)) {
103  $a_value = (string) $a_value;
104  }
105  $this->parent = $a_value;
106  }
107 
112  public function getParentElement()
113  {
114  return $this->parent;
115  }
116 
117  public function setDisabled(bool $a_value): void
118  {
119  $this->disabled = $a_value;
120  }
121 
122  public function isDisabled(): bool
123  {
124  return $this->disabled;
125  }
126 
127  public function setRequired(bool $a_value): void
128  {
129  $this->required = $a_value;
130  }
131 
132  public function isRequired(): bool
133  {
134  return $this->required;
135  }
136 
142  protected function addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def): void
143  {
144  if ($this->isDisabled()) {
145  $a_field->setDisabled(true);
146  } elseif ($this->isRequired()) {
147  $a_field->setRequired(true);
148  }
149 
150  $info = $this->getInfo();
151  if ($info) {
152  $a_field->setInfo($info);
153  }
154  }
155 
157  {
158  $parent_def = $this->getParentElement();
159  $parent_option = '';
160  if ($parent_def) {
161  if (is_array($parent_def)) {
162  $parent_option = $parent_def[1];
163  $parent_def = $parent_def[0];
164  }
165  $parent_field = $this->getForm()->getItemByPostVar($parent_def);
166  if ($parent_field instanceof ilSubEnabledFormPropertyGUI) {
167  // radio/checkbox group
168  if ($parent_option && method_exists($parent_field, "getOptions")) {
169  foreach ($parent_field->getOptions() as $option) {
170  if ($option->getValue() == $parent_option) {
171  $parent_field = $option;
172  break;
173  }
174  }
175  }
176  }
177 
178  if ($parent_field) {
179  return $parent_field;
180  }
181  }
182  return null;
183  }
184 
185  protected function addToParentElement(ilFormPropertyGUI $a_field): void
186  {
187  $field = $this->findParentElementInForm();
188  if ($field instanceof ilSubEnabledFormPropertyGUI) {
189  $field->addSubItem($a_field);
190  } elseif ($this->getForm() instanceof ilPropertyFormGUI) {
191  $this->getForm()->addItem($a_field);
192  }
193  }
194 
198  abstract public function addToForm(): void;
199 
204  public function addJS(ilGlobalTemplateInterface $a_tpl): void
205  {
206  }
207 
213  protected function isActiveForSubItems($a_parent_option = null): bool
214  {
215  return !$this->getADT()->isNull();
216  }
217 
223  public function shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt = null): bool
224  {
225  if ($this->isDisabled()) {
226  return false;
227  }
228 
229  // inactive parent elements disable importing
230  if ($a_parent_adt) {
231  $parent_option = null;
233  if (is_array($parent_element)) {
234  $parent_option = $parent_element[1];
235  }
236  return $a_parent_adt->isActiveForSubItems($parent_option);
237  }
238  return true;
239  }
240 
244  abstract public function importFromPost(): void;
245 
246  public function validate(): bool
247  {
248  // ilADTFormBridge->isRequired() != ilADT->allowNull()
249  if ($this->isRequired() && $this->getADT()->isNull()) {
250  $field = $this->getForm()->getItemByPostVar($this->getElementId());
251  $field->setAlert($this->lng->txt("msg_input_is_required"));
252  return false;
253  } // no need to further validate if no value given
254  elseif (!$this->getADT()->isValid()) {
255  $tmp = [];
256  $mess = $this->getADT()->getValidationErrors();
257  foreach ($mess as $error_code) {
258  $tmp[] = $this->getADT()->translateErrorCode($error_code);
259  }
260  $field = $this->getForm()->getItemByPostVar($this->getElementId());
261  $field->setAlert(implode("<br />", $tmp));
262  return false;
263  }
264  return true;
265  }
266 
267  public function setExternalErrors(array $a_errors): void
268  {
269  $field = $this->getForm()->getItemByPostVar($this->getElementId());
270  $field->setAlert(implode("<br />", $a_errors));
271  }
272 }
addJS(ilGlobalTemplateInterface $a_tpl)
Add ADT-specific JS-files to template.
addToParentElement(ilFormPropertyGUI $a_field)
setElementId(string $a_value)
Set element id (aka form field)
setForm(ilPropertyFormGUI $a_form)
ADT form bridge base class.
setTitle(string $a_value)
setInfo(string $a_value)
__construct(ilADT $a_adt)
ADT base class.
Definition: class.ilADT.php:11
addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
Helper method to handle generic properties like setRequired(), setInfo()
importFromPost()
Import values from form request POST data.
global $DIC
Definition: feed.php:28
isActiveForSubItems($a_parent_option=null)
Check if element is currently active for subitem(s)
addToForm()
Add ADT-specific fields to form.
setExternalErrors(array $a_errors)
ilPropertyFormGUI $form
shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt=null)
Check if incoming values should be imported at all.
isValidADT(ilADT $a_adt)
setDisabled(bool $a_value)
setRequired(bool $a_required)
form( $class_path, string $cmd)
getParentElement()
Get parent element.
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
disabled()
Example showing how to plug a disabled checkbox into a form.
Definition: disabled.php:10
ADT definition base class.
setDisabled(bool $a_disabled)
setRequired(bool $a_value)