ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADTEnumFormBridge.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 protected bool $force_radio = false;
24 protected array $option_infos = [];
25 protected bool $auto_sort = true;
26
27 protected function isValidADT(ilADT $a_adt): bool
28 {
29 return ($a_adt instanceof ilADTEnum);
30 }
31
32 public function setAutoSort($a_value)
33 {
34 $this->auto_sort = (bool) $a_value;
35 }
36
37 public function forceRadio($a_value, ?array $a_info = null)
38 {
39 $this->force_radio = (bool) $a_value;
40 if ($this->force_radio) {
41 $this->option_infos = (array) $a_info;
42 }
43 }
44
45 public function addToForm(): void
46 {
47 $def = $this->getADT()->getCopyOfDefinition();
48 $selection = $this->getADT()->getSelection();
49
50 $options = $def->getOptions();
51
52 if ($this->auto_sort) {
53 asort($options);
54 }
55
56 if (!$this->isRequired()) {
57 $options = array("" => "-") + $options;
58 } elseif ($this->getADT()->isNull()) {
59 $options = array("" => $this->lng->txt("please_select")) + $options;
60 }
61
62 if (!$this->force_radio) {
63 $select = new ilSelectInputGUI($this->getTitle(), $this->getElementId());
64
65 $select->setOptions($options);
66 } else {
67 $select = new ilRadioGroupInputGUI($this->getTitle(), $this->getElementId());
68
69 foreach ($options as $value => $caption) {
70 $option = new ilRadioOption($caption, $value);
71 if (is_array($this->option_infos) && array_key_exists($value, $this->option_infos)) {
72 $option->setInfo($this->option_infos[$value]);
73 }
74 $select->addOption($option);
75 }
76 }
77
78 $this->addBasicFieldProperties($select, $def);
79
80 $select->setValue($selection);
81
82 $this->addToParentElement($select);
83 }
84
85 public function importFromPost(): void
86 {
87 // ilPropertyFormGUI::checkInput() is pre-requisite
88 $this->getADT()->setSelection($this->getForm()->getInput($this->getElementId()));
89
90 $field = $this->getForm()->getItemByPostVar($this->getElementId());
91 $field->setValue($this->getADT()->getSelection());
92 }
93
94 protected function isActiveForSubItems($a_parent_option = null): bool
95 {
96 return ($this->getADT()->getSelection() == $a_parent_option);
97 }
98}
forceRadio($a_value, ?array $a_info=null)
isActiveForSubItems($a_parent_option=null)
Check if element is currently active for subitem(s)
importFromPost()
Import values from form request POST data.
addToForm()
Add ADT-specific fields to form.
ADT form bridge base class.
addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
Helper method to handle generic properties like setRequired(), setInfo()
addToParentElement(ilFormPropertyGUI $a_field)
ADT base class.
Definition: class.ilADT.php:26
This class represents a property in a property form.
This class represents an option in a radio group.
This class represents a selection list property in a property form.