ILIAS  release_8 Revision v8.24
class.ilSelectInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 protected array $cust_attr = array();
29 protected array $options = array();
33 protected $value;
34 protected bool $hide_sub = false;
35
36 public function __construct(
37 string $a_title = "",
38 string $a_postvar = ""
39 ) {
40 global $DIC;
41
42 $this->lng = $DIC->language();
43 parent::__construct($a_title, $a_postvar);
44 $this->setType("select");
45 }
46
47 public function setOptions(array $a_options): void
48 {
49 $this->options = $a_options;
50 }
51
52 public function getOptions(): array
53 {
54 return $this->options ?: array();
55 }
56
62 public function setValue($a_value): void
63 {
64 if ($this->getMulti() && is_array($a_value)) {
65 $this->setMultiValues($a_value);
66 $a_value = array_shift($a_value);
67 }
68 $this->value = $a_value;
69 }
70
76 public function getValue()
77 {
78 return $this->value;
79 }
80
81
82 public function setValueByArray(array $a_values): void
83 {
84 $this->setValue($a_values[$this->getPostVar()] ?? "");
85 foreach ($this->getSubItems() as $item) {
86 $item->setValueByArray($a_values);
87 }
88 }
89
90 public function checkInput(): bool
91 {
93
94 $valid = true;
95 if (!$this->getMulti()) {
96 if ($this->getRequired() && trim($this->str($this->getPostVar())) == "") {
97 $valid = false;
98 } elseif (!array_key_exists($this->str($this->getPostVar()), $this->getOptions())) {
99 $this->setAlert($lng->txt('msg_invalid_post_input'));
100 return false;
101 }
102 } else {
103 $values = $this->strArray($this->getPostVar());
104 foreach ($values as $value) {
105 if (!array_key_exists($value, $this->getOptions())) {
106 $this->setAlert($lng->txt('msg_invalid_post_input'));
107 return false;
108 }
109 }
110 if ($this->getRequired() && !trim(implode("", $values))) {
111 $valid = false;
112 }
113 }
114 if (!$valid) {
115 $this->setAlert($lng->txt("msg_input_is_required"));
116 return false;
117 }
118 return $this->checkSubItemsInput();
119 }
120
124 public function getInput()
125 {
126 if (!$this->getMulti()) {
127 return $this->str($this->getPostVar());
128 }
129 return $this->strArray($this->getPostVar());
130 }
131
132 public function addCustomAttribute(string $a_attr): void
133 {
134 $this->cust_attr[] = $a_attr;
135 }
136
137 public function getCustomAttributes(): array
138 {
139 return $this->cust_attr;
140 }
141
142 public function render($a_mode = ""): string
143 {
144 $sel_value = "";
145 $tpl = new ilTemplate("tpl.prop_select.html", true, true, "Services/Form");
146
147 foreach ($this->getCustomAttributes() as $attr) {
148 $tpl->setCurrentBlock('cust_attr');
149 $tpl->setVariable('CUSTOM_ATTR', $attr);
150 $tpl->parseCurrentBlock();
151 }
152
153 if ($this->getRequired()) {
154 $tpl->setCurrentBlock('required_attribute');
155 $tpl->setVariable('REQUIRED', 'required');
156 $tpl->parseCurrentBlock();
157 }
158
159 // determine value to select. Due to accessibility reasons we
160 // should always select a value (per default the first one)
161 $first = true;
162 foreach ($this->getOptions() as $option_value => $option_text) {
163 if ($first) {
164 $sel_value = $option_value;
165 }
166 $first = false;
167 if ((string) $option_value == (string) $this->getValue()) {
168 $sel_value = $option_value;
169 }
170 }
171 foreach ($this->getOptions() as $option_value => $option_text) {
172 $tpl->setCurrentBlock("prop_select_option");
173 $tpl->setVariable("VAL_SELECT_OPTION", ilLegacyFormElementsUtil::prepareFormOutput((string) $option_value));
174 if ((string) $sel_value == (string) $option_value) {
175 $tpl->setVariable(
176 "CHK_SEL_OPTION",
177 'selected="selected"'
178 );
179 }
180 $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
181 $tpl->parseCurrentBlock();
182 }
183 $tpl->setVariable("ID", $this->getFieldId());
184
185 $postvar = $this->getPostVar();
186 if ($this->getMulti() && substr($postvar, -2) != "[]") {
187 $postvar .= "[]";
188 }
189
190 $tpl->setVariable("POST_VAR", $postvar);
191 if ($this->getDisabled()) {
192 if ($this->getMulti()) {
193 $value = $this->getMultiValues();
194 $hidden = "";
195 if (is_array($value)) {
196 foreach ($value as $item) {
197 $hidden .= $this->getHiddenTag($postvar, $item);
198 }
199 }
200 } else {
201 $hidden = $this->getHiddenTag($postvar, (string) $this->getValue());
202 }
203 if ($hidden) {
204 $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
205 $tpl->setVariable("HIDDEN_INPUT", $hidden);
206 }
207 }
208
209 // multi icons
210 if ($this->getMulti() && !$a_mode && !$this->getDisabled()) {
211 $tpl->touchBlock("inline_in_bl");
212 $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
213 }
214
215 $tpl->setVariable("ARIA_LABEL", ilLegacyFormElementsUtil::prepareFormOutput($this->getTitle()));
216
217 return $tpl->get();
218 }
219
220 public function insert(ilTemplate $a_tpl): void
221 {
222 $a_tpl->setCurrentBlock("prop_generic");
223 $a_tpl->setVariable("PROP_GENERIC", $this->render());
224 $a_tpl->parseCurrentBlock();
225 }
226
227 public function getTableFilterHTML(): string
228 {
229 $html = $this->render();
230 return $html;
231 }
232
233 public function getToolbarHTML(): string
234 {
235 $html = $this->render("toolbar");
236 return $html;
237 }
238
242 public function setHideSubForm(
243 bool $a_value,
244 ?string $a_condition = null
245 ): void {
246 $this->hide_sub = $a_value;
247
248 if ($a_condition) {
249 $this->addCustomAttribute('onchange="if(this.value ' . $a_condition . ')' .
250 ' { il.Form.showSubForm(\'subform_' . $this->getFieldId() . '\', \'il_prop_cont_' . $this->getFieldId() . '\'); }' .
251 ' else { il.Form.hideSubForm(\'subform_' . $this->getFieldId() . '\'); };"');
252 }
253 }
254
255 public function hideSubForm(): bool
256 {
257 return $this->hide_sub;
258 }
259}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
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)
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 file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
$valid
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...
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