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