ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSelectInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected array $cust_attr = array();
30 protected array $options = array();
31 protected ?\Closure $langresolve = null;
35 protected $value;
36 protected bool $hide_sub = false;
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("select");
47 }
48
49 public function setOptions(array $a_options): void
50 {
51 $this->options = $a_options;
52 }
53
54 public function getOptions(): array
55 {
56 return $this->options ?: array();
57 }
58
59 public function setOptionsLangAttribute(\Closure $langresolve): void
60 {
61 $this->langresolve = $langresolve;
62 }
63
64 public function getOptionsLangAttribute($key): string
65 {
66 return $this->langresolve($this->getOptions(), $key);
67 }
68
74 public function setValue($a_value): void
75 {
76 if ($this->getMulti() && is_array($a_value)) {
77 $this->setMultiValues($a_value);
78 $a_value = array_shift($a_value);
79 }
80 $this->value = $a_value;
81 }
82
88 public function getValue()
89 {
90 return $this->value;
91 }
92
93
94 public function setValueByArray(array $a_values): void
95 {
96 $this->setValue($a_values[$this->getPostVar()] ?? "");
97 foreach ($this->getSubItems() as $item) {
98 $item->setValueByArray($a_values);
99 }
100 }
101
102 public function checkInput(): bool
103 {
105
106 $valid = true;
107 if (!$this->getMulti()) {
108 if ($this->getRequired() && trim($this->str($this->getPostVar())) == "") {
109 $valid = false;
110 } elseif (!array_key_exists($this->str($this->getPostVar()), $this->getOptions())) {
111 $this->setAlert($lng->txt('msg_invalid_post_input'));
112 return false;
113 }
114 } else {
115 $values = $this->strArray($this->getPostVar());
116 foreach ($values as $value) {
117 if (!array_key_exists($value, $this->getOptions())) {
118 $this->setAlert($lng->txt('msg_invalid_post_input'));
119 return false;
120 }
121 }
122 if ($this->getRequired() && !trim(implode("", $values))) {
123 $valid = false;
124 }
125 }
126 if (!$valid) {
127 $this->setAlert($lng->txt("msg_input_is_required"));
128 return false;
129 }
130 return $this->checkSubItemsInput();
131 }
132
136 public function getInput()
137 {
138 if (!$this->getMulti()) {
139 return $this->str($this->getPostVar());
140 }
141 return $this->strArray($this->getPostVar());
142 }
143
144 public function addCustomAttribute(string $a_attr): void
145 {
146 $this->cust_attr[] = $a_attr;
147 }
148
149 public function getCustomAttributes(): array
150 {
151 return $this->cust_attr;
152 }
153
154 public function render($a_mode = ""): string
155 {
156 $sel_value = "";
157 $tpl = new ilTemplate("tpl.prop_select.html", true, true, "components/ILIAS/Form");
158
159 foreach ($this->getCustomAttributes() as $attr) {
160 $tpl->setCurrentBlock('cust_attr');
161 $tpl->setVariable('CUSTOM_ATTR', $attr);
162 $tpl->parseCurrentBlock();
163 }
164
165 if ($this->getRequired()) {
166 $tpl->setCurrentBlock('required_attribute');
167 $tpl->setVariable('REQUIRED', 'required');
168 $tpl->parseCurrentBlock();
169 }
170
171 // determine value to select. Due to accessibility reasons we
172 // should always select a value (per default the first one)
173 $first = true;
174 foreach ($this->getOptions() as $option_value => $option_text) {
175 if ($first) {
176 $sel_value = $option_value;
177 }
178 $first = false;
179 if ((string) $option_value == (string) $this->getValue()) {
180 $sel_value = $option_value;
181 }
182 }
183 foreach ($this->getOptions() as $option_value => $option_text) {
184 $tpl->setCurrentBlock("prop_select_option");
185 $tpl->setVariable("VAL_SELECT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput((string) $option_value));
186 if ((string) $sel_value == (string) $option_value) {
187 $tpl->setVariable(
188 "CHK_SEL_OPTION",
189 ' selected="selected"'
190 );
191 }
192 $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
193
194 if ($this->langresolve) {
196 $lang = $f($this->getOptions(), $option_value);
197 $tpl->setVariable("OPTION_LANG", ' lang="' . $lang . '"');
198 }
199
200 $tpl->parseCurrentBlock();
201 }
202 $tpl->setVariable("ID", $this->getFieldId());
203
204 $postvar = $this->getPostVar();
205 if ($this->getMulti() && substr($postvar, -2) != "[]") {
206 $postvar .= "[]";
207 }
208
209 $tpl->setVariable("POST_VAR", $postvar);
210 if ($this->getDisabled()) {
211 if ($this->getMulti()) {
212 $value = $this->getMultiValues();
213 $hidden = "";
214 if (is_array($value)) {
215 foreach ($value as $item) {
216 $hidden .= $this->getHiddenTag($postvar, $item);
217 }
218 }
219 } else {
220 $hidden = $this->getHiddenTag($postvar, (string) $this->getValue());
221 }
222 if ($hidden) {
223 $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
224 $tpl->setVariable("HIDDEN_INPUT", $hidden);
225 }
226 }
227
228 // multi icons
229 if ($this->getMulti() && !$a_mode && !$this->getDisabled()) {
230 $tpl->touchBlock("inline_in_bl");
231 $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
232 }
233
234 $tpl->setVariable("ARIA_LABEL", ilLegacyFormElementsUtil::prepareFormOutput($this->getTitle()));
235
236 return $tpl->get();
237 }
238
239 public function insert(ilTemplate $a_tpl): void
240 {
241 $a_tpl->setCurrentBlock("prop_generic");
242 $a_tpl->setVariable("PROP_GENERIC", $this->render());
243 $a_tpl->parseCurrentBlock();
244 }
245
246 public function getTableFilterHTML(): string
247 {
248 $html = $this->render();
249 return $html;
250 }
251
252 public function getToolbarHTML(): string
253 {
254 $html = $this->render("toolbar");
255 return $html;
256 }
257
261 public function setHideSubForm(
262 bool $a_value,
263 ?string $a_condition = null
264 ): void {
265 $this->hide_sub = $a_value;
266
267 if ($a_condition) {
268 $this->addCustomAttribute('onchange="if(this.value ' . $a_condition . ')' .
269 ' { il.Form.showSubForm(\'subform_' . $this->getFieldId() . '\', \'il_prop_cont_' . $this->getFieldId() . '\'); }' .
270 ' else { il.Form.hideSubForm(\'subform_' . $this->getFieldId() . '\'); };"');
271 }
272 }
273
274 public function hideSubForm(): bool
275 {
276 return $this->hide_sub;
277 }
278}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
setMultiValues(array $a_values)
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...
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a selection list property in a property form.
insert(ilTemplate $a_tpl)
setOptionsLangAttribute(\Closure $langresolve)
setValue($a_value)
Set Value.
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
checkInput()
Check input, strip slashes etc.
setHideSubForm(bool $a_value, ?string $a_condition=null)
Set initial sub form visibility, optionally add dynamic value-based condition.
addCustomAttribute(string $a_attr)
setOptions(array $a_options)
setValueByArray(array $a_values)
__construct(string $a_title="", string $a_postvar="")
getToolbarHTML()
Get input item HTML to be inserted into ilToolbarGUI.
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
$valid
Interface for multi values support.
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
$lang
Definition: xapiexit.php:25