ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilRadioGroupInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  protected array $options = array();
30  protected string $value = "";
31 
32  public function __construct(
33  string $a_title = "",
34  string $a_postvar = ""
35  ) {
36  global $DIC;
37 
38  $this->lng = $DIC->language();
39  parent::__construct($a_title, $a_postvar);
40  $this->setType("radio");
41  }
42 
43  public function addOption(ilRadioOption $a_option): void
44  {
45  $this->options[] = $a_option;
46  }
47 
48  public function getOptions(): array
49  {
50  return $this->options;
51  }
52 
53  public function setValue(string $a_value): void
54  {
55  $this->value = $a_value;
56  }
57 
58  public function getValue(): string
59  {
60  return $this->value;
61  }
62 
63  public function setValueByArray(array $a_values): void
64  {
65  $this->setValue((string) ($a_values[$this->getPostVar()] ?? ""));
66  foreach ($this->getOptions() as $option) {
67  foreach ($option->getSubItems() as $item) {
68  $item->setValueByArray($a_values);
69  }
70  }
71  }
72 
73  public function checkInput(): bool
74  {
75  $lng = $this->lng;
76 
77  $val = $this->getInput();
78  if ($this->getRequired() && trim($val) == "") {
79  $this->setAlert($lng->txt("msg_input_is_required"));
80  return false;
81  }
82 
83  $ok = true;
84  $value = $this->getInput();
85  foreach ($this->getOptions() as $option) {
86  foreach ($option->getSubItems() as $item) {
87  if ($value == $option->getValue()) {
88  if (!$item->checkInput()) {
89  $ok = false;
90  }
91  }
92  }
93  }
94  return $ok;
95  }
96 
97  public function getInput(): string
98  {
99  return $this->str($this->getPostVar());
100  }
101 
102  public function insert(ilTemplate $a_tpl): void
103  {
104  $html = $this->render();
105 
106  $a_tpl->setCurrentBlock("prop_generic");
107  $a_tpl->setVariable("PROP_GENERIC", $html);
108  $a_tpl->parseCurrentBlock();
109  }
110 
111  public function render(): string
112  {
113  $tpl = new ilTemplate("tpl.prop_radio.html", true, true, "components/ILIAS/Form");
114 
115  foreach ($this->getOptions() as $option) {
116  // information text for option
117  if ($option->getInfo() != "") {
118  $tpl->setCurrentBlock("radio_option_desc");
119  $tpl->setVariable("RADIO_OPTION_DESC", $option->getInfo());
120  if ($option->getInfo() !== '') {
121  $tpl->setVariable('DESCRIPTION_FOR_ID', $this->getFieldId() . "_" . $option->getValue());
122  }
123  $tpl->parseCurrentBlock();
124  }
125 
126 
127  if (count($option->getSubItems()) > 0) {
128  if ($option->getValue() != $this->getValue()) {
129  // #10930
130  if ($this->global_tpl) {
131  $hop_id = $this->getFieldId() . "_" . $option->getValue();
132  $this->global_tpl->addOnloadCode(
133  "il.Form.hideSubForm('subform_$hop_id');"
134  );
135  }
136  if ($this->getParentForm()) {
137  $this->getParentForm()->addAsyncOnloadCode(
138  "il.Form.hideSubForm('subform_$hop_id');"
139  );
140  }
141  }
142  $tpl->setCurrentBlock("radio_option_subform");
143  $pf = new ilPropertyFormGUI();
144  $pf->setMode("subform");
145  $pf->setItems($option->getSubItems());
146  $tpl->setVariable("SUB_FORM", $pf->getContent());
147  $tpl->setVariable("SOP_ID", $this->getFieldId() . "_" . $option->getValue());
148  if ($pf->getMultipart()) {
149  $this->getParentForm()->setMultipart(true);
150  }
151  $tpl->parseCurrentBlock();
152  if ($pf->getMultipart()) {
153  $this->getParentForm()->setMultipart(true);
154  }
155  }
156 
157  $tpl->setCurrentBlock("prop_radio_option");
158  $tpl->setVariable("POST_VAR", $this->getPostVar());
159  $tpl->setVariable("VAL_RADIO_OPTION", $option->getValue());
160  $tpl->setVariable("OP_ID", $this->getFieldId() . "_" . $option->getValue());
161  if ($option->getInfo() !== '') {
162  $tpl->setVariable('DESCRIBED_BY_FIELD_ID', $this->getFieldId() . "_" . $option->getValue());
163  }
164  $tpl->setVariable("FID", $this->getFieldId());
165  if ($this->getDisabled() or $option->getDisabled()) {
166  $tpl->setVariable('DISABLED', 'disabled="disabled" ');
167  }
168  if ($option->getValue() == $this->getValue()) {
169  $tpl->setVariable(
170  "CHK_RADIO_OPTION",
171  'checked="checked"'
172  );
173  }
174  $tpl->setVariable("TXT_RADIO_OPTION", $option->getTitle());
175 
176 
177  $tpl->parseCurrentBlock();
178  }
179  $tpl->setVariable("ID", $this->getFieldId());
180 
181  if ($this->getDisabled()) {
182  $tpl->setVariable(
183  "HIDDEN_INPUT",
184  $this->getHiddenTag($this->getPostVar(), $this->getValue())
185  );
186  }
187 
188  return $tpl->get();
189  }
190 
191  public function getItemByPostVar(string $a_post_var): ?ilFormPropertyGUI
192  {
193  if ($this->getPostVar() == $a_post_var) {
194  return $this;
195  }
196 
197  foreach ($this->getOptions() as $option) {
198  foreach ($option->getSubItems() as $item) {
199  if ($item->getType() != "section_header") {
200  $ret = $item->getItemByPostVar($a_post_var);
201  if (is_object($ret)) {
202  return $ret;
203  }
204  }
205  }
206  }
207 
208  return null;
209  }
210 
211  public function getTableFilterHTML(): string
212  {
213  return $this->render();
214  }
215 
216  public function getSubInputItemsRecursive(): array
217  {
218  $subInputItems = parent::getSubInputItemsRecursive();
219  foreach ($this->getOptions() as $option) {
223  $subInputItems = array_merge($subInputItems, $option->getSubInputItemsRecursive());
224  }
225 
226  return $subInputItems;
227  }
228 
229  public function getFormLabelFor(): string
230  {
231  return "";
232  }
233 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents an option in a radio group.
getItemByPostVar(string $a_post_var)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
getSubInputItemsRecursive()
returns a flat array of possibly existing subitems recursively
addOption(ilRadioOption $a_option)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This class represents a property in a property form.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
__construct(string $a_title="", string $a_postvar="")
global $DIC
Definition: shib_login.php:26
getHiddenTag(string $a_post_var, string $a_value)
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(Container $dic, ilPlugin $plugin)
This class represents a property that may include a sub form.