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