ILIAS  release_8 Revision v8.24
class.ilMultiSelectInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected array $options = [];
29 protected array $value = [];
30 protected bool $select_all = false;
31 protected bool $selected_first = false;
32 private int $width = 160;
33 private int $height = 100;
34 protected string $widthUnit = 'px';
35 protected string $heightUnit = 'px';
36 protected array $custom_attributes = [];
37
38 public function __construct(
39 string $a_title = "",
40 string $a_postvar = ""
41 ) {
42 global $DIC;
43
44 $this->lng = $DIC->language();
45 parent::__construct($a_title, $a_postvar);
46 $this->setType("multi_select");
47 $this->setValue(array());
48 }
49
50 public function setWidth(int $a_width): void
51 {
52 $this->width = $a_width;
53 }
54
55 public function getWidth(): int
56 {
57 return $this->width;
58 }
59
60 public function setHeight(int $a_height): void
61 {
62 $this->height = $a_height;
63 }
64
65 public function getHeight(): int
66 {
67 return $this->height;
68 }
69
73 public function setOptions(array $a_options): void
74 {
75 $this->options = $a_options;
76 }
77
81 public function getOptions(): array
82 {
83 return $this->options;
84 }
85
89 public function setValue($a_array): void
90 {
91 $this->value = [];
92 if (is_array($a_array)) {
93 $this->value = $a_array;
94 }
95 }
96
100 public function getValue(): array
101 {
102 return is_array($this->value) ? $this->value : array();
103 }
104
108 public function setValueByArray(array $a_values): void
109 {
110 $this->setValue($a_values[$this->getPostVar()] ?? []);
111 }
112
113 public function enableSelectAll(bool $a_value): void
114 {
115 $this->select_all = $a_value;
116 }
117
118 public function enableSelectedFirst(bool $a_value): void
119 {
120 $this->selected_first = $a_value;
121 }
122
123 public function checkInput(): bool
124 {
126
127 $val = $this->getInput();
128 if ($this->getRequired() && count($val) == 0) {
129 $this->setAlert($lng->txt("msg_input_is_required"));
130 return false;
131 }
132 if (count($val) > 0) {
133 $options = array_map(function ($k) {
134 return (string) $k;
135 }, array_keys($this->getOptions()));
136 foreach ($val as $key => $val2) {
137 if ($key != 0 || $val2 != "") {
138 if (!in_array((string) $val2, $options)) {
139 $this->setAlert($lng->txt("msg_unknown_value"));
140 return false;
141 }
142 }
143 }
144 }
145 return true;
146 }
147
148 public function getInput(): array
149 {
150 return $this->strArray($this->getPostVar());
151 }
152
153 public function render(): string
154 {
156
157 $tpl = new ilTemplate("tpl.prop_multi_select.html", true, true, "Services/Form");
158 $values = $this->getValue();
159
160 $options = $this->getOptions();
161 if ($options) {
162 if ($this->select_all) {
163 // enable select all toggle
164 $tpl->setCurrentBlock("item");
165 $tpl->setVariable("VAL", "");
166 $tpl->setVariable("ID_VAL", ilLegacyFormElementsUtil::prepareFormOutput("all__toggle"));
167 $tpl->setVariable("IID", $this->getFieldId());
168 $tpl->setVariable("TXT_OPTION", "<em>" . $lng->txt("select_all") . "</em>");
169 $tpl->setVariable("POST_VAR", $this->getPostVar());
170 $tpl->parseCurrentBlock();
171
172 $tpl->setVariable("TOGGLE_FIELD_ID", $this->getFieldId());
173 $tpl->setVariable("TOGGLE_ALL_ID", $this->getFieldId() . "_all__toggle");
174 $tpl->setVariable("TOGGLE_ALL_CBOX_ID", $this->getFieldId() . "_");
175 }
176
177 if ($this->selected_first) {
178 // move selected values to top
179 $tmp_checked = $tmp_unchecked = array();
180 foreach ($options as $option_value => $option_text) {
181 if (in_array($option_value, $values)) {
182 $tmp_checked[$option_value] = $option_text;
183 } else {
184 $tmp_unchecked[$option_value] = $option_text;
185 }
186 }
187 $options = $tmp_checked + $tmp_unchecked;
188 unset($tmp_checked);
189 unset($tmp_unchecked);
190 }
191
192 foreach ($options as $option_value => $option_text) {
193 $tpl->setCurrentBlock("item");
194 if ($this->getDisabled()) {
195 $tpl->setVariable(
196 "DISABLED",
197 " disabled=\"disabled\""
198 );
199 }
200 if (in_array($option_value, $values)) {
201 $tpl->setVariable(
202 "CHECKED",
203 " checked=\"checked\""
204 );
205 }
206
207 $tpl->setVariable("VAL", ilLegacyFormElementsUtil::prepareFormOutput($option_value));
208 $tpl->setVariable("ID_VAL", ilLegacyFormElementsUtil::prepareFormOutput($option_value));
209 $tpl->setVariable("IID", $this->getFieldId());
210 $tpl->setVariable("TXT_OPTION", $option_text);
211 $tpl->setVariable("POST_VAR", $this->getPostVar());
212 $tpl->parseCurrentBlock();
213 }
214 }
215
216 $tpl->setVariable("ID", $this->getFieldId());
217 $tpl->setVariable("CUSTOM_ATTRIBUTES", implode(' ', $this->getCustomAttributes()));
218
219 if ($this->getWidth()) {
220 $tpl->setVariable("WIDTH", $this->getWidth() . ($this->getWidthUnit() ?: ''));
221 }
222 if ($this->getHeight()) {
223 $tpl->setVariable("HEIGHT", $this->getHeight() . ($this->getHeightUnit() ?: ''));
224 }
225
226 return $tpl->get();
227 }
228
229 public function insert(ilTemplate $a_tpl): void
230 {
231 $a_tpl->setCurrentBlock("prop_generic");
232 $a_tpl->setVariable("PROP_GENERIC", $this->render());
233 $a_tpl->parseCurrentBlock();
234 }
235
236 public function getTableFilterHTML(): string
237 {
238 $html = $this->render();
239 return $html;
240 }
241
242 public function getCustomAttributes(): array
243 {
245 }
246
247 public function setCustomAttributes(array $custom_attributes): void
248 {
249 $this->custom_attributes = $custom_attributes;
250 }
251
252
253 public function addCustomAttribute(string $custom_attribute): void
254 {
255 $this->custom_attributes[] = $custom_attribute;
256 }
257
258 public function getWidthUnit(): string
259 {
260 return $this->widthUnit;
261 }
262
263 public function setWidthUnit(string $widthUnit): void
264 {
265 $this->widthUnit = $widthUnit;
266 }
267
268 public function getHeightUnit(): string
269 {
270 return $this->heightUnit;
271 }
272
273 public function setHeightUnit(string $heightUnit): void
274 {
275 $this->heightUnit = $heightUnit;
276 }
277
278 public function unserializeData(string $a_data): void
279 {
280 $data = unserialize($a_data);
281
282 if (is_array($data)) {
283 $this->setValue($data);
284 } else {
285 $this->setValue([]);
286 }
287 }
288}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This class represents a property in a property form.
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...
static prepareFormOutput($a_str, bool $a_strip=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
addCustomAttribute(string $custom_attribute)
setCustomAttributes(array $custom_attributes)
checkInput()
Check input, strip slashes etc.
__construct(string $a_title="", string $a_postvar="")
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
string $key
Consumer key/client ID value.
Definition: System.php:193