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