ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilADTFormBridge.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 abstract class ilADTFormBridge
12 {
13  protected $adt; // [ilADT]
14  protected $form; // [ilPropertyFormGUI]
15  protected $id; // [string]
16  protected $title; // [string]
17  protected $info; // [string]
18  protected $parent_element; // [string|array]
19  protected $required; // [bool]
20  protected $disabled; // [bool]
21 
28  public function __construct(ilADT $a_adt)
29  {
30  $this->setADT($a_adt);
31  }
32 
33 
34  //
35  // properties
36  //
37 
46  abstract protected function isValidADT(ilADT $a_adt);
47 
54  protected function setADT(ilADT $a_adt)
55  {
56  if (!$this->isValidADT($a_adt)) {
57  throw new Exception('ADTFormBridge Type mismatch.');
58  }
59 
60  $this->adt = $a_adt;
61  }
62 
68  public function getADT()
69  {
70  return $this->adt;
71  }
72 
78  public function setForm(ilPropertyFormGUI $a_form)
79  {
80  $this->form = $a_form;
81  }
82 
88  public function getForm()
89  {
90  return $this->form;
91  }
92 
98  public function setElementId($a_value)
99  {
100  $this->id = (string) $a_value;
101  }
102 
108  public function getElementId()
109  {
110  return $this->id;
111  }
112 
118  public function setTitle($a_value)
119  {
120  $this->title = trim($a_value);
121  }
122 
128  public function getTitle()
129  {
130  return $this->title;
131  }
132 
138  public function setInfo($a_value)
139  {
140  $this->info = trim($a_value);
141  }
142 
148  public function getInfo()
149  {
150  return $this->info;
151  }
152 
158  public function setParentElement($a_value)
159  {
160  if (!is_array($a_value)) {
161  $a_value = (string) $a_value;
162  }
163  $this->parent = $a_value;
164  }
165 
171  public function getParentElement()
172  {
173  return $this->parent;
174  }
175 
181  public function setDisabled($a_value)
182  {
183  $this->disabled = (bool) $a_value;
184  }
185 
191  public function isDisabled()
192  {
193  return $this->disabled;
194  }
195 
201  public function setRequired($a_value)
202  {
203  $this->required = (bool) $a_value;
204  }
205 
211  public function isRequired()
212  {
213  return $this->required;
214  }
215 
216 
217  //
218  // form
219  //
220 
227  protected function addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
228  {
229  if ((bool) $this->isDisabled()) {
230  $a_field->setDisabled(true);
231  } elseif ($this->isRequired()) {
232  $a_field->setRequired(true);
233  }
234 
235  $info = $this->getInfo();
236  if ($info) {
237  $a_field->setInfo($info);
238  }
239  }
240 
246  protected function findParentElementInForm()
247  {
248  $parent_def = $this->getParentElement();
249  if ($parent_def) {
250  if (is_array($parent_def)) {
251  $parent_option = $parent_def[1];
252  $parent_def = $parent_def[0];
253  }
254 
255  // :TODO: throw exception on invalid definition ?!
256 
257  $parent_field = $this->getForm()->getItemByPostVar($parent_def);
258  if ($parent_field instanceof ilSubEnabledFormPropertyGUI) {
259  // radio/checkbox group
260  if ($parent_option && method_exists($parent_field, "getOptions")) {
261  foreach ($parent_field->getOptions() as $option) {
262  if ($option->getValue() == $parent_option) {
263  $parent_field = $option;
264  break;
265  }
266  }
267  }
268  }
269 
270  if ($parent_field) {
271  return $parent_field;
272  }
273  }
274  }
275 
281  protected function addToParentElement(ilFormPropertyGUI $a_field)
282  {
283  $field = $this->findParentElementInForm();
284  if ($field) {
285  $field->addSubItem($a_field);
286  } elseif ($this->getForm() instanceof ilPropertyFormGUI) {
287  $this->getForm()->addItem($a_field);
288  }
289  }
290 
294  abstract public function addToForm();
295 
301  public function addJS(ilTemplate $a_tpl)
302  {
303  }
304 
311  protected function isActiveForSubItems($a_parent_option = null)
312  {
313  return !$this->getADT()->isNull();
314  }
315 
322  public function shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt = null)
323  {
324  if ($this->isDisabled()) {
325  return false;
326  }
327 
328  // inactive parent elements disable importing
329  if ($a_parent_adt) {
330  $parent_option = null;
332  if (is_array($parent_element)) {
333  $parent_option = $parent_element[1];
334  }
335  return $a_parent_adt->isActiveForSubItems($parent_option);
336  }
337 
338  return true;
339  }
340 
344  abstract public function importFromPost();
345 
351  public function validate()
352  {
353  global $DIC;
354 
355  $lng = $DIC['lng'];
356 
357  // ilADTFormBridge->isRequired() != ilADT->allowNull()
358  if ($this->isRequired() && $this->getADT()->isNull()) {
359  $field = $this->getForm()->getItemByPostvar($this->getElementId());
360  $field->setAlert($lng->txt("msg_input_is_required"));
361  return false;
362  }
363  // no need to further validate if no value given
364  elseif (!$this->getADT()->isValid()) {
365  $tmp = array();
366 
367  $mess = $this->getADT()->getValidationErrors();
368  foreach ($mess as $error_code) {
369  $tmp[] = $this->getADT()->translateErrorCode($error_code);
370  }
371 
372  $field = $this->getForm()->getItemByPostvar($this->getElementId());
373  $field->setAlert(implode("<br />", $tmp));
374 
375  return false;
376  }
377 
378  return true;
379  }
380 
381  public function setExternalErrors($a_errors)
382  {
383  $field = $this->getForm()->getItemByPostvar($this->getElementId());
384  $field->setAlert(implode("<br />", $a_errors));
385  }
386 }
validate()
Validate ADT and parse error codes.
findParentElementInForm()
Try to find parent element in form (could be option)
addToParentElement(ilFormPropertyGUI $a_field)
Add form field to parent element.
setForm(ilPropertyFormGUI $a_form)
Set form.
This class represents a property form user interface.
setDisabled($a_value)
Set disabled.
global $DIC
Definition: saml.php:7
ADT form bridge base class.
setParentElement($a_value)
Set parent element.
isRequired()
Get required.
isDisabled()
Get disabled.
setTitle($a_value)
Set title (aka form field caption)
setInfo($a_value)
Set info (aka form field info text)
__construct(ilADT $a_adt)
Constructor.
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.
setInfo($a_info)
Set Information Text.
isActiveForSubItems($a_parent_option=null)
Check if element is currently active for subitem(s)
addToForm()
Add ADT-specific fields to form.
$lng
special template class to simplify handling of ITX/PEAR
shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt=null)
Check if incoming values should be imported at all.
isValidADT(ilADT $a_adt)
Check if given ADT is valid.
setADT(ilADT $a_adt)
Set ADT.
setRequired($a_value)
Set required.
getParentElement()
Get parent element.
getElementId()
Get element id.
This class represents a property in a property form.
This class represents a property that may include a sub form.
addJS(ilTemplate $a_tpl)
Add ADT-specific JS-files to template.
info()
Definition: info.php:2
ADT definition base class.
setElementId($a_value)
Set element id (aka form field)
setDisabled($a_disabled)
Set Disabled.
setRequired($a_required)
Set Required.