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