ILIAS  release_8 Revision v8.24
class.ilCheckboxGroupInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected array $options = array();
29 protected ?array $value = null;
30 protected bool $use_values_as_keys = false;
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("checkboxgroup");
41 }
42
46 public function setUseValuesAsKeys(bool $a_val): void
47 {
48 $this->use_values_as_keys = $a_val;
49 }
50
51 public function getUseValuesAsKeys(): bool
52 {
54 }
55
59 public function addOption($a_option): void
60 {
61 $this->options[] = $a_option;
62 }
63
69 public function setOptions(array $a_options): void
70 {
71 foreach ($a_options as $key => $label) {
72 if (is_string($label)) {
73 $chb = new ilCheckboxInputGUI($label, $key);
74 $this->options[] = $chb;
75 } elseif ($label instanceof ilCheckboxInputGUI) {
76 $this->options[] = $label;
77 }
78 }
79 }
80
81 public function getOptions(): array
82 {
83 return $this->options;
84 }
85
86 public function setValue(?array $a_value): void
87 {
88 $this->value = $a_value;
89 }
90
91 public function getValue(): ?array
92 {
93 return $this->value;
94 }
95
96 public function setValueByArray(array $a_values): void
97 {
98 $this->setValue($a_values[$this->getPostVar()] ?? null);
99 foreach ($this->getOptions() as $option) {
100 foreach ($option->getSubItems() as $item) {
101 $item->setValueByArray($a_values);
102 }
103 }
104 }
105
106 public function checkInput(): bool
107 {
109
110 $values = $this->strArray($this->getPostVar());
111 if ($this->getRequired() && count($values) === 0) {
112 $this->setAlert($lng->txt('msg_input_is_required'));
113 return false;
114 }
115
116 $ok = true;
117 foreach ($this->getOptions() as $option) {
118 foreach ($option->getSubItems() as $item) {
119 $item_ok = $item->checkInput();
120 if (!$item_ok && in_array($option->getValue(), $values)) {
121 $ok = false;
122 }
123 }
124 }
125 return $ok;
126 }
127
128 public function getInput(): array
129 {
130 return $this->strArray($this->getPostVar());
131 }
132
133 public function insert(ilTemplate $a_tpl): void
134 {
135 $a_tpl->setCurrentBlock("prop_generic");
136 $a_tpl->setVariable("PROP_GENERIC", $this->render());
137 $a_tpl->parseCurrentBlock();
138 }
139
140 public function getItemByPostVar(string $a_post_var): ?ilFormPropertyGUI
141 {
142 if ($this->getPostVar() == $a_post_var) {
143 return $this;
144 }
145
146 foreach ($this->getOptions() as $option) {
147 foreach ($option->getSubItems() as $item) {
148 if ($item->getType() != "section_header") {
149 $ret = $item->getItemByPostVar($a_post_var);
150 if (is_object($ret)) {
151 return $ret;
152 }
153 }
154 }
155 }
156
157 return null;
158 }
159
160 public function getTableFilterHTML(): string
161 {
162 return $this->render();
163 }
164
165 public function getToolbarHTML(): string
166 {
167 return $this->render('toolbar');
168 }
169
170 protected function render($a_mode = ''): string
171 {
172 $tpl = new ilTemplate("tpl.prop_checkbox_group.html", true, true, "Services/Form");
173
174 foreach ($this->getOptions() as $option) {
175 // information text for option
176 if ($option->getInfo() != "") {
177 $tpl->setCurrentBlock("checkbox_option_desc");
178 $tpl->setVariable("CHECKBOX_OPTION_DESC", $option->getInfo());
179 $tpl->parseCurrentBlock();
180 }
181
182
183 if (count($option->getSubItems()) > 0) {
184 $tpl->setCurrentBlock("checkbox_option_subform");
185 $pf = new ilPropertyFormGUI();
186 $pf->setMode("subform");
187 $pf->setItems($option->getSubItems());
188 $tpl->setVariable("SUB_FORM", $pf->getContent());
189 $tpl->setVariable("SOP_ID", $this->getFieldId() . "_" . $option->getValue());
190 if ($pf->getMultipart()) {
191 $this->getParentForm()->setMultipart(true);
192 }
193 $tpl->parseCurrentBlock();
194 if ($pf->getMultipart()) {
195 $this->getParentForm()->setMultipart(true);
196 }
197 }
198
199 $tpl->setCurrentBlock("prop_checkbox_option");
200
201 if (!$this->getUseValuesAsKeys()) {
202 $tpl->setVariable("POST_VAR", $this->getPostVar() . '[]');
203 $tpl->setVariable("VAL_CHECKBOX_OPTION", $option->getValue());
204 } else {
205 $tpl->setVariable("POST_VAR", $this->getPostVar() . '[' . $option->getValue() . ']');
206 $tpl->setVariable("VAL_CHECKBOX_OPTION", "1");
207 }
208
209 $tpl->setVariable("OP_ID", $this->getFieldId() . "_" . $option->getValue());
210 $tpl->setVariable("FID", $this->getFieldId());
211
212 if ($this->getDisabled() or $option->getDisabled()) {
213 $tpl->setVariable('DISABLED', 'disabled="disabled" ');
214 }
215
216 if (is_array($this->getValue())) {
217 if (!$this->getUseValuesAsKeys()) {
218 if (in_array($option->getValue(), $this->getValue())) {
219 $tpl->setVariable(
220 "CHK_CHECKBOX_OPTION",
221 'checked="checked"'
222 );
223 }
224 } else {
225 $cval = $this->getValue();
226 if (isset($cval[$option->getValue()]) && $cval[$option->getValue()] == 1) {
227 $tpl->setVariable(
228 "CHK_CHECKBOX_OPTION",
229 'checked="checked"'
230 );
231 }
232 }
233 }
234 $tpl->setVariable("TXT_CHECKBOX_OPTION", $option->getTitle());
235
236
237 $tpl->parseCurrentBlock();
238 }
239 $tpl->setVariable("ID", $this->getFieldId());
240
241 return $tpl->get();
242 }
243
244 public function getSubInputItemsRecursive(): array
245 {
246 $subInputItems = parent::getSubInputItemsRecursive();
247 foreach ($this->getOptions() as $option) {
251 $subInputItems = array_merge($subInputItems, $option->getSubInputItemsRecursive());
252 }
253
254 return $subInputItems;
255 }
256}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setUseValuesAsKeys(bool $a_val)
Set use values as keys.
getItemByPostVar(string $a_post_var)
Get item by post var.
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
__construct(string $a_title="", string $a_postvar="")
checkInput()
Check input, strip slashes etc.
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
setOptions(array $a_options)
Set Options.
This class represents a checkbox property in a property form.
This class represents a property in a property form.
getItemByPostVar(string $a_post_var)
Get item by 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...
This class represents a property form user interface.
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...
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
string $key
Consumer key/client ID value.
Definition: System.php:193