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