ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTextInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
32  protected $value = null;
33  protected ?int $maxlength = 200;
34  protected int $size = 40;
35  protected string $validationRegexp = "";
36  protected string $validationFailureMessage = '';
37  protected string $suffix = "";
38  protected string $style_css = "";
39  protected string $css_class = "";
40  protected string $ajax_datasource = "";
41  protected ?string $ajax_datasource_delimiter = null;
42  protected bool $ajax_datasource_commit = false;
43  protected string $ajax_datasource_commit_url = "";
44  protected bool $submit_form_on_enter = false;
45  // Flag whether the html autocomplete attribute should be set to "off" or not
46  protected bool $autocomplete_disabled = false;
47  protected string $input_type = "";
48 
49  public function __construct(
50  string $a_title = "",
51  string $a_postvar = ""
52  ) {
53  global $DIC;
54  $this->lng = $DIC->language();
55  parent::__construct($a_title, $a_postvar);
56  $this->setInputType("text");
57  $this->setType("text");
58  $this->validationRegexp = "";
59  }
60 
64  public function setValue($a_value): void
65  {
66  if ($this->getMulti() && is_array($a_value)) {
67  $this->setMultiValues($a_value);
68  $a_value = array_shift($a_value);
69  }
70  $this->value = $a_value;
71  }
72 
76  public function getValue()
77  {
78  return $this->value;
79  }
80 
81  public function setValidationFailureMessage(string $a_msg): void
82  {
83  $this->validationFailureMessage = $a_msg;
84  }
85 
86  public function getValidationFailureMessage(): string
87  {
89  }
90 
91  public function setValidationRegexp(string $a_value): void
92  {
93  $this->validationRegexp = $a_value;
94  }
95 
96  public function getValidationRegexp(): string
97  {
99  }
100 
101  public function setMaxLength(?int $a_maxlength): void
102  {
103  $this->maxlength = $a_maxlength;
104  }
105 
106  public function getMaxLength(): ?int
107  {
108  return $this->maxlength;
109  }
110 
111  public function setSize(int $a_size): void
112  {
113  $this->size = $a_size;
114  }
115 
116  public function setInlineStyle(string $a_style): void
117  {
118  $this->style_css = $a_style;
119  }
120 
121  public function getInlineStyle(): string
122  {
123  return $this->style_css;
124  }
125 
126  public function setCssClass(string $a_class): void
127  {
128  $this->css_class = $a_class;
129  }
130 
131  public function getCssClass(): string
132  {
133  return $this->css_class;
134  }
135 
136  public function setValueByArray(array $a_values): void
137  {
138  $this->setValue($a_values[$this->getPostVar()] ?? "");
139  }
140 
141  public function getSize(): int
142  {
143  return $this->size;
144  }
145 
146  public function setSuffix(string $a_value): void
147  {
148  $this->suffix = $a_value;
149  }
150 
151  public function getSuffix(): string
152  {
153  return $this->suffix;
154  }
155 
160  public function setInputType(string $a_type): void
161  {
162  $this->input_type = $a_type;
163  }
164 
165  public function getInputType(): string
166  {
167  return $this->input_type;
168  }
169 
170  public function setSubmitFormOnEnter(bool $a_val): void
171  {
172  $this->submit_form_on_enter = $a_val;
173  }
174 
175  public function getSubmitFormOnEnter(): bool
176  {
178  }
179 
180  public function checkInput(): bool
181  {
182  $lng = $this->lng;
183 
184  if (!$this->getMulti()) {
185  if ($this->getRequired() && trim($this->str($this->getPostVar())) == "") {
186  $this->setAlert($lng->txt("msg_input_is_required"));
187  return false;
188  } elseif (strlen($this->getValidationRegexp())) {
189  if (!preg_match($this->getValidationRegexp(), $this->str($this->getPostVar()))) {
190  $this->setAlert(
191  $this->getValidationFailureMessage() ?:
192  $lng->txt('msg_wrong_format')
193  );
194  return false;
195  }
196  }
197  } else {
198  if ($this->getRequired() &&
199  !trim(implode("", $this->strArray($this->getPostVar())))) {
200  $this->setAlert($lng->txt("msg_input_is_required"));
201  return false;
202  } elseif (strlen($this->getValidationRegexp())) {
203  $reg_valid = true;
204  foreach ($this->strArray($this->getPostVar()) as $value) {
205  if (!preg_match($this->getValidationRegexp(), $value)) {
206  $reg_valid = false;
207  break;
208  }
209  }
210  if (!$reg_valid) {
211  $this->setAlert(
212  $this->getValidationFailureMessage() ?:
213  $lng->txt('msg_wrong_format')
214  );
215  return false;
216  }
217  }
218  }
219 
220  return $this->checkSubItemsInput();
221  }
222 
226  public function getInput()
227  {
228  if (!$this->getMulti()) {
229  return $this->str($this->getPostVar());
230  }
231  return $this->strArray($this->getPostVar());
232  }
233 
234  public function getDataSource(): string
235  {
236  return $this->ajax_datasource;
237  }
238 
239  public function setDataSource(
240  string $href,
241  ?string $a_delimiter = null
242  ): void {
243  $this->ajax_datasource = $href;
244  $this->ajax_datasource_delimiter = $a_delimiter;
245  }
246 
247  public function setDataSourceSubmitOnSelection(bool $a_stat): void
248  {
249  $this->ajax_datasource_commit = $a_stat;
250  }
251 
252  public function getDataSourceSubmitOnSelection(): bool
253  {
255  }
256 
257  public function setDataSourceSubmitUrl(string $a_url): void
258  {
259  $this->ajax_datasource_commit_url = $a_url;
260  }
261 
262  public function getDataSourceSubmitUrl(): string
263  {
265  }
266 
267  public function setMultiValues(array $a_values): void
268  {
269  foreach ($a_values as $idx => $value) {
270  $a_values[$idx] = trim($value);
271  if ($a_values[$idx] == "") {
272  unset($a_values[$idx]);
273  }
274  }
275  parent::setMultiValues($a_values);
276  }
277 
278  public function render(string $a_mode = ""): string
279  {
280  $lng = $this->lng;
281 
282  $tpl = new ilTemplate("tpl.prop_textinput.html", true, true, "components/ILIAS/Form");
283  if (strlen((string) $this->getValue())) {
284  $tpl->setCurrentBlock("prop_text_propval");
285  $tpl->setVariable(
286  "PROPERTY_VALUE",
288  );
289  $tpl->parseCurrentBlock();
290  }
291  if (strlen($this->getInlineStyle())) {
292  $tpl->setCurrentBlock("stylecss");
293  $tpl->setVariable(
294  "CSS_STYLE",
296  );
297  $tpl->parseCurrentBlock();
298  }
299  if (strlen($this->getCssClass())) {
300  $tpl->setCurrentBlock("classcss");
301  $tpl->setVariable('CLASS_CSS', ilLegacyFormElementsUtil::prepareFormOutput((string) $this->getCssClass()));
302  $tpl->parseCurrentBlock();
303  }
304  if ($this->getSubmitFormOnEnter()) {
305  $tpl->touchBlock("submit_form_on_enter");
306  }
307 
308  switch ($this->getInputType()) {
309  case 'password':
310  $tpl->setVariable('PROP_INPUT_TYPE', 'password');
311  break;
312  case 'hidden':
313  $tpl->setVariable('PROP_INPUT_TYPE', 'hidden');
314  break;
315  case 'text':
316  default:
317  $tpl->setVariable('PROP_INPUT_TYPE', 'text');
318  }
319  $tpl->setVariable("ID", $this->getFieldId());
320  $tpl->setVariable("SIZE", $this->getSize());
321  if ($this->getMaxLength() != null) {
322  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
323  }
324  if (strlen($this->getSuffix())) {
325  $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
326  }
327 
328  $postvar = $this->getPostVar();
329  if ($this->getMulti() && substr($postvar, -2) != "[]") {
330  $postvar .= "[]";
331  }
332 
333  $tpl->setVariable("POST_VAR", $postvar);
334  if ($this->getDisabled()) {
335  if ($this->getMulti()) {
336  $value = $this->getMultiValues();
337  $hidden = "";
338  if (is_array($value)) {
339  foreach ($value as $item) {
340  $hidden .= $this->getHiddenTag($postvar, $item);
341  }
342  }
343  } else {
344  $hidden = $this->getHiddenTag($postvar, $this->getValue());
345  }
346  if ($hidden) {
347  $tpl->setVariable("HIDDEN_INPUT", $hidden);
348  }
349  $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
350  }
351 
352  // use autocomplete feature?
353  if ($this->getDataSource()) {
354  $tpl->setVariable('AUTOCOMPLETE', 'autocomplete="off"');
355  $this->global_tpl->addJavaScript('assets/js/legacyAutocomplete.js', true, 3);
356  $config = json_encode([
357  'delimiter' => $this->ajax_datasource_delimiter,
358  'dataSource' => $this->ajax_datasource,
359  'submitOnSelection' => $this->ajax_datasource_commit,
360  'submitUrl' => $this->ajax_datasource_commit_url,
361  'autocompleteLength' => 3,
362  'moreText' => $this->lng->txt('autocomplete_more')
363  ]);
364 
365  if ($this->multi) {
366  $sel_auto = '[id^="' . $this->getFieldId() . '"]';
367  } else {
368  // use id for autocomplete selector
369  $sel_auto = "#" . $this->getFieldId();
370  }
371 
372  $this->global_tpl->addOnLoadCode(
373  "il.LegacyForm.autocomplete.init(document.querySelector(`{$sel_auto}`), {$config});"
374  );
375  }
376 
377  if ($a_mode == "toolbar") {
378  // block-inline hack, see: http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/
379  // -moz-inline-stack for FF2
380  // zoom 1; *display:inline for IE6 & 7
381  $tpl->setVariable(
382  "STYLE_PAR",
383  'display: -moz-inline-stack; display:inline-block; zoom: 1; *display:inline;'
384  );
385  } else {
386  $tpl->setVariable("STYLE_PAR", '');
387  }
388 
389  if ($this->isHtmlAutoCompleteDisabled()) {
390  $tpl->setVariable("AUTOCOMPLETE", "autocomplete=\"off\"");
391  }
392 
393  if ($this->getRequired()) {
394  $tpl->setVariable("REQUIRED", "required=\"required\"");
395  }
396 
397  // multi icons
398  if ($this->getMulti() && !$a_mode && !$this->getDisabled()) {
399  $tpl->touchBlock("inline_in_bl");
400  $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
401  }
402 
403  $tpl->setVariable("ARIA_LABEL", ilLegacyFormElementsUtil::prepareFormOutput($this->getTitle()));
404  if ($this->getInfo() !== '') {
405  $tpl->setVariable('DESCRIBED_BY_FIELD_ID', $this->getFieldId());
406  }
407 
408  return $tpl->get();
409  }
410 
411  public function insert(ilTemplate $a_tpl): void
412  {
413  $html = $this->render();
414 
415  $a_tpl->setCurrentBlock("prop_generic");
416  $a_tpl->setVariable("PROP_GENERIC", $html);
417  $a_tpl->parseCurrentBlock();
418  }
419 
420  public function getTableFilterHTML(): string
421  {
422  $html = $this->render();
423  return $html;
424  }
425 
426  public function getToolbarHTML(): string
427  {
428  $html = $this->render("toolbar");
429  return $html;
430  }
431 
432  public function setDisableHtmlAutoComplete(bool $autocomplete): void
433  {
434  $this->autocomplete_disabled = $autocomplete;
435  }
436 
437  public function isHtmlAutoCompleteDisabled(): bool
438  {
440  }
441 
442  public function getPostValueForComparison(): string
443  {
444  return $this->getInput();
445  }
446 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setSuffix(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...
setValueByArray(array $a_values)
setDataSourceSubmitOnSelection(bool $a_stat)
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...
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
static prepareFormOutput($a_str, bool $a_strip=false)
setCssClass(string $a_class)
setDataSource(string $href, ?string $a_delimiter=null)
setInputType(string $a_type)
set input type
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
string $ajax_datasource_commit_url
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
insert(ilTemplate $a_tpl)
setMultiValues(array $a_values)
global $DIC
Definition: shib_login.php:26
getHiddenTag(string $a_post_var, string $a_value)
setDataSourceSubmitUrl(string $a_url)
setDisableHtmlAutoComplete(bool $autocomplete)
render(string $a_mode="")
Interface for multi values support.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setMaxLength(?int $a_maxlength)
setSubmitFormOnEnter(bool $a_val)
__construct(Container $dic, ilPlugin $plugin)
This class represents a property that may include a sub form.
setValidationFailureMessage(string $a_msg)
setInlineStyle(string $a_style)
__construct(string $a_title="", string $a_postvar="")
setValidationRegexp(string $a_value)