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