ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilFormPropertyGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 use ILIAS\HTTP;
23 use ILIAS\Refinery;
24 
31 {
32  protected array $set_params = [];
35  protected ilCtrl $ctrl;
36  protected ilLanguage $lng;
37  protected string $type = "";
38  protected string $title = "";
39  protected string $postvar = "";
40  protected string $info = "";
41  protected string $alert = "";
42  protected bool $required = false;
44  protected string $hidden_title = "";
45  protected bool $multi = false;
46  protected bool $multi_sortable = false;
47  protected bool $multi_addremove = true;
48  protected array $multi_values = [];
50  protected HTTP\Services $http;
51  protected ?Refinery\Factory $refinery = null;
52  protected bool $disabled = false;
53 
55 
56  public function __construct(
57  string $a_title = "",
58  string $a_postvar = ""
59  ) {
60  global $DIC;
61 
62  if (isset($DIC["http"])) {
63  $this->http = $DIC->http();
64  }
65 
66  if (isset($DIC["refinery"])) {
67  $this->refinery = $DIC->refinery();
68  }
69 
70  $this->ctrl = $DIC->ctrl();
71  $this->lng = $DIC->language();
72  $this->setTitle($a_title);
73  $this->setPostVar($a_postvar);
74  $this->setDisabled(false);
75  if (isset($DIC["http"])) { // some unit tests will fail otherwise
76  $this->request = $DIC->http()->request();
77  }
78  if (isset($DIC["tpl"])) { // some unit tests will fail otherwise
79  $this->global_tpl = $DIC['tpl'];
80  }
81  }
82 
86  public function executeCommand()
87  {
88  $cmd = $this->ctrl->getCmd();
89  return $this->$cmd();
90  }
91 
92  protected function symbol(): \ILIAS\Repository\Symbol\SymbolAdapterGUI
93  {
94  global $DIC;
95  return $DIC->repository()->internal()->gui()->symbol();
96  }
97 
98  protected function setType(string $a_type): void
99  {
100  $this->type = $a_type;
101  }
102 
103  public function getType(): string
104  {
105  return $this->type;
106  }
107 
108  public function setTitle(string $a_title): void
109  {
110  $this->title = $a_title;
111  }
112 
113  public function getTitle(): string
114  {
115  return $this->title;
116  }
117 
118  public function setPostVar(string $a_postvar): void
119  {
120  $this->postvar = $a_postvar;
121  }
122 
123  public function getPostVar(): string
124  {
125  return $this->postvar;
126  }
127 
128  public function getFieldId(): string
129  {
130  $id = str_replace("[", "__", $this->getPostVar());
131  $id = str_replace("]", "__", $id);
132  return $id;
133  }
134 
135  public function setInfo(string $a_info): void
136  {
137  $this->info = $a_info;
138  }
139 
140  public function getInfo(): string
141  {
142  return $this->info;
143  }
144 
145  public function setAlert(string $a_alert): void
146  {
147  $this->alert = $a_alert;
148  }
149 
150  public function getAlert(): string
151  {
152  return $this->alert;
153  }
154 
155  public function setRequired(bool $a_required): void
156  {
157  $this->required = $a_required;
158  }
159 
160  public function getRequired(): bool
161  {
162  return $this->required;
163  }
164 
165  public function setDisabled(bool $a_disabled): void
166  {
167  $this->disabled = $a_disabled;
168  }
169 
170  public function getDisabled(): bool
171  {
172  return $this->disabled;
173  }
174 
178  public function checkInput(): bool
179  {
180  return false; // please overwrite
181  }
182 
183  public function setParentForm(ilPropertyFormGUI $a_parentform): void
184  {
185  $this->parentform = $a_parentform;
186  }
187 
188  public function getParentForm(): ?ilPropertyFormGUI
189  {
190  return $this->parentform;
191  }
192 
193  // Set Parent GUI object.
194  public function setParent(ilFormPropertyGUI $a_val): void
195  {
196  $this->parent_gui = $a_val;
197  }
198 
199  public function getParent(): ?ilFormPropertyGUI
200  {
201  return $this->parent_gui;
202  }
203 
204  public function getSubForm(): ?ilPropertyFormGUI
205  {
206  return null;
207  }
208 
209  public function hideSubForm(): bool
210  {
211  return false;
212  }
213 
214  // Set hidden title (for screenreaders)
215  public function setHiddenTitle(string $a_val): void
216  {
217  $this->hidden_title = $a_val;
218  }
219 
220  public function getHiddenTitle(): string
221  {
222  return $this->hidden_title;
223  }
224 
228  public function getItemByPostVar(string $a_post_var): ?ilFormPropertyGUI
229  {
230  if ($this->getPostVar() == $a_post_var) {
231  return $this;
232  }
233  return null;
234  }
235 
236  public function serializeData(): string
237  {
238  return serialize($this->getValue());
239  }
240 
241  public function unserializeData(string $a_data): void
242  {
243  $data = unserialize($a_data);
244 
245  if ($data) {
246  $this->setValue($data);
247  } else {
248  $this->setValue("");
249  }
250  }
251 
256  public function setParentTable($a_val): void
257  {
258  $this->parent_table = $a_val;
259  }
260 
265  public function getParentTable(): ?ilTable2GUI
266  {
267  return $this->parent_table;
268  }
269 
270  protected function checkParentFormTable(): void
271  {
272  $parent = $this->getParentForm();
273  $parent_table = $this->getParentTable();
274  if (!is_object($parent) && !isset($parent_table)) {
275  throw new Exception("Parent form/table not set for " . get_class($this) . " to use serialize feature.");
276  }
277  }
278 
282  public function writeToSession(): void
283  {
284  $this->checkParentFormTable();
285  ilSession::set($this->getSessionKey(), $this->serializeData());
286  }
287 
288  protected function getSessionKey(): string
289  {
290  $parent = $this->getParentForm();
291  if (!is_object($parent)) {
292  $parent = $this->getParentTable();
293  }
294  return "form_" . $parent->getId() . "_" . $this->getFieldId();
295  }
296 
300  public function clearFromSession(): void
301  {
302  $this->checkParentFormTable();
304  }
305 
309  public function readFromSession(): void
310  {
311  $this->checkParentFormTable();
312  if (ilSession::has($this->getSessionKey())) {
313  $this->unserializeData(ilSession::get($this->getSessionKey()));
314  } else {
315  $this->unserializeData("");
316  }
317  }
318 
319  public function getHiddenTag(
320  string $a_post_var,
321  string $a_value
322  ): string {
323  return '<input type="hidden" name="' . $a_post_var . '" value="' . ilLegacyFormElementsUtil::prepareFormOutput(
324  $a_value
325  ) . '" />';
326  }
327 
328  public function setMulti(
329  bool $a_multi,
330  bool $a_sortable = false,
331  bool $a_addremove = true
332  ): void {
333  if (!$this instanceof ilMultiValuesItem) {
334  throw new ilFormException(sprintf(
335  "%s not supported for form property type %s",
336  __FUNCTION__,
337  get_class($this)
338  ));
339  }
340 
341  $this->multi = $a_multi;
342  $this->multi_sortable = $a_sortable;
343  $this->multi_addremove = $a_addremove;
344  }
345 
346  public function getMulti(): bool
347  {
348  return $this->multi;
349  }
350 
351  public function setMultiValues(array $a_values): void
352  {
353  $this->multi_values = array_unique($a_values);
354  }
355 
356  public function getMultiValues(): array
357  {
358  return $this->multi_values;
359  }
360 
361  // Get HTML for multiple value icons
362  protected function getMultiIconsHTML(): string
363  {
364  $lng = $this->lng;
365 
366  $id = $this->getFieldId();
367 
368  $tpl = new ilTemplate("tpl.multi_icons.html", true, true, "components/ILIAS/Form");
369 
370  $html = "";
371  if ($this->multi_addremove) {
372  $tpl->setCurrentBlock("addremove");
373  $tpl->setVariable("ID", $id);
374  $tpl->setVariable("TXT_ADD", $lng->txt("add"));
375  $tpl->setVariable("TXT_REMOVE", $lng->txt("remove"));
376  $tpl->setVariable("SRC_ADD", $this->symbol()->glyph("add")->render());
377  $tpl->setVariable("SRC_REMOVE", $this->symbol()->glyph("remove")->render());
378  $tpl->parseCurrentBlock();
379  }
380 
381  if ($this->multi_sortable) {
382  $tpl->setCurrentBlock("sortable");
383  $tpl->setVariable("ID", $id);
384  $tpl->setVariable("TXT_DOWN", $lng->txt("down"));
385  $tpl->setVariable("TXT_UP", $lng->txt("up"));
386  $tpl->setVariable("SRC_UP", $this->symbol()->glyph("up")->render());
387  $tpl->setVariable("SRC_DOWN", $this->symbol()->glyph("down")->render());
388  $tpl->parseCurrentBlock();
389  }
390 
391  return $tpl->get();
392  }
393 
397  public function getContentOutsideFormTag(): string
398  {
399  return "";
400  }
401 
406  public static function removeProhibitedCharacters(string $a_text): string
407  {
408  return str_replace("\x0B", "", $a_text);
409  }
410 
414  public function stripSlashesAddSpaceFallback(string $a_str): string
415  {
416  $str = ilUtil::stripSlashes($a_str);
417  if ($str != $a_str) {
418  $str = ilUtil::stripSlashes(str_replace("<", "< ", $a_str));
419  }
420  return $str;
421  }
422 
426  public function getTableFilterLabelFor(): string
427  {
428  return $this->getFieldId();
429  }
430 
434  public function getFormLabelFor(): string
435  {
436  return $this->getFieldId();
437  }
438 
439  // get integer parameter kindly
440  protected function int($key): int
441  {
442  if (is_null($this->refinery)) {
443  return 0;
444  }
445  $t = $this->refinery->kindlyTo()->int();
446  return (int) ($this->getRequestParam($key, $t) ?? 0);
447  }
448 
449  // get integer array kindly
450  protected function intArray($key): array
451  {
452  if (!$this->isRequestParamArray($key)) {
453  return [];
454  }
455  $t = $this->refinery->custom()->transformation(
456  function ($arr) {
457  // keep keys(!), transform all values to int
458  return array_column(
459  array_map(
460  function ($k, $v) {
461  return [$k, (int) $v];
462  },
463  array_keys($arr),
464  $arr
465  ),
466  1,
467  0
468  );
469  }
470  );
471  return (array) ($this->getRequestParam($key, $t) ?? []);
472  }
473 
474  // get string parameter kindly
475  protected function str($key): string
476  {
477  if (is_null($this->refinery)) {
478  return "";
479  }
480  $t = $this->refinery->kindlyTo()->string();
481  return $this->stripSlashesAddSpaceFallback(
482  (string) ($this->getRequestParam($key, $t) ?? "")
483  );
484  }
485 
486  // get raw parameter
487  protected function raw($key)
488  {
489  $t = $this->refinery->custom()->transformation(function ($v) {
490  return $v;
491  });
492  return $this->getRequestParam($key, $t);
493  }
494 
495  // get string array kindly
496  protected function strArray($key): array
497  {
498  if (!$this->isRequestParamArray($key)) {
499  return [];
500  }
501  $t = $this->refinery->custom()->transformation(
502  function ($arr) {
503  // keep keys(!), transform all values to string
504  return array_column(
505  array_map(
506  function ($k, $v) {
507  if (is_array($v)) {
508  $v = "";
509  }
510  return [$k, $this->stripSlashesAddSpaceFallback((string) $v)];
511  },
512  array_keys($arr),
513  $arr
514  ),
515  1,
516  0
517  );
518  }
519  );
520  return (array) ($this->getRequestParam($key, $t) ?? []);
521  }
522 
523  // get array of arrays kindly
524  protected function arrayArray($key): array
525  {
526  if (!$this->isRequestParamArray($key)) {
527  return [];
528  }
529  $t = $this->refinery->custom()->transformation(
530  function ($arr) {
531  // keep keys(!), transform all values to string
532  return array_column(
533  array_map(
534  function ($k, $v) {
535  return [$k, (array) $v];
536  },
537  array_keys($arr),
538  $arr
539  ),
540  1,
541  0
542  );
543  }
544  );
545  return (array) ($this->getRequestParam($key, $t) ?? []);
546  }
547 
548  protected function isRequestParamArray(string $key): bool
549  {
550  $no_transform = $this->refinery->identity();
551  $w = $this->http->wrapper();
552  if ($w->post()->has($key)) {
553  return is_array($w->post()->retrieve($key, $no_transform));
554  }
555  if ($w->query()->has($key)) {
556  return is_array($w->query()->retrieve($key, $no_transform));
557  }
558  return false;
559  }
560 
566  public function setRequestParam(string $key, $val): void
567  {
568  $this->set_params[$key] = $val;
569  }
570 
574  protected function getRequestParam(string $key, Refinery\Transformation $t)
575  {
576  if (isset($this->set_params[$key])) {
577  return $this->set_params[$key];
578  }
579  $w = $this->http->wrapper();
580  if ($w->post()->has($key)) {
581  return $w->post()->retrieve($key, $t);
582  }
583  if ($w->query()->has($key)) {
584  return $w->query()->retrieve($key, $t);
585  }
586  return null;
587  }
588 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRequestParam(string $key, Refinery\Transformation $t)
static get(string $a_var)
ilPropertyFormGUI $parentform
getTableFilterLabelFor()
Get label "for" attribute value for filter.
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...
__construct(string $a_title="", string $a_postvar="")
getFormLabelFor()
Get label "for" attribute value for form.
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
Interface Observer Contains several chained tasks and infos about them.
setRequestParam(string $key, $val)
This writes the request (aka post) values.
setParent(ilFormPropertyGUI $a_val)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
unserializeData(string $a_data)
checkInput()
Check input, strip slashes etc.
setMultiValues(array $a_values)
static prepareFormOutput($a_str, bool $a_strip=false)
setPostVar(string $a_postvar)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
info()
description: > Example for rendering a info message box.
Definition: info.php:34
setParentForm(ilPropertyFormGUI $a_parentform)
static http()
Fetches the global http state from ILIAS.
Refinery Factory $refinery
ilGlobalTemplateInterface $global_tpl
global $DIC
Definition: shib_login.php:22
getHiddenTag(string $a_post_var, string $a_value)
static has($a_var)
setRequired(bool $a_required)
stripSlashesAddSpaceFallback(string $a_str)
Strip slashes with add space fallback, see https://www.ilias.de/mantis/view.php?id=19727.
setParentTable($a_val)
Set parent table.
Interface for multi values support.
getContentOutsideFormTag()
Get content that has to reside outside of the parent form tag, e.g.
getItemByPostVar(string $a_post_var)
Get item by post var.
static removeProhibitedCharacters(string $a_text)
Remove prohibited characters see #19159.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
disabled()
description: > Example showing how to plug a disabled checkbox into a form
Definition: disabled.php:32
ilFormPropertyGUI $parent_gui
static clear(string $a_var)
setDisabled(bool $a_disabled)
static set(string $a_var, $a_val)
Set a value.
getParentTable()
Get parent table.
setMulti(bool $a_multi, bool $a_sortable=false, bool $a_addremove=true)