ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilFormPropertyGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 use ILIAS\HTTP;
23 use ILIAS\Refinery;
24 
31 {
32  protected array $set_params = [];
33  protected ?ilTable2GUI $parent_table = null;
34  protected ?ilFormPropertyGUI $parent_gui = null;
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;
43  protected ?ilPropertyFormGUI $parentform = null;
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 setType(string $a_type): void
93  {
94  $this->type = $a_type;
95  }
96 
97  public function getType(): string
98  {
99  return $this->type;
100  }
101 
102  public function setTitle(string $a_title): void
103  {
104  $this->title = $a_title;
105  }
106 
107  public function getTitle(): string
108  {
109  return $this->title;
110  }
111 
112  public function setPostVar(string $a_postvar): void
113  {
114  $this->postvar = $a_postvar;
115  }
116 
117  public function getPostVar(): string
118  {
119  return $this->postvar;
120  }
121 
122  public function getFieldId(): string
123  {
124  $id = str_replace("[", "__", $this->getPostVar());
125  $id = str_replace("]", "__", $id);
126  return $id;
127  }
128 
129  public function setInfo(string $a_info): void
130  {
131  $this->info = $a_info;
132  }
133 
134  public function getInfo(): string
135  {
136  return $this->info;
137  }
138 
139  public function setAlert(string $a_alert): void
140  {
141  $this->alert = $a_alert;
142  }
143 
144  public function getAlert(): string
145  {
146  return $this->alert;
147  }
148 
149  public function setRequired(bool $a_required): void
150  {
151  $this->required = $a_required;
152  }
153 
154  public function getRequired(): bool
155  {
156  return $this->required;
157  }
158 
159  public function setDisabled(bool $a_disabled): void
160  {
161  $this->disabled = $a_disabled;
162  }
163 
164  public function getDisabled(): bool
165  {
166  return $this->disabled;
167  }
168 
172  public function checkInput(): bool
173  {
174  return false; // please overwrite
175  }
176 
177  public function setParentForm(ilPropertyFormGUI $a_parentform): void
178  {
179  $this->parentform = $a_parentform;
180  }
181 
182  public function getParentForm(): ?ilPropertyFormGUI
183  {
184  return $this->parentform;
185  }
186 
187  // Set Parent GUI object.
188  public function setParent(ilFormPropertyGUI $a_val): void
189  {
190  $this->parent_gui = $a_val;
191  }
192 
193  public function getParent(): ?ilFormPropertyGUI
194  {
195  return $this->parent_gui;
196  }
197 
198  public function getSubForm(): ?ilPropertyFormGUI
199  {
200  return null;
201  }
202 
203  public function hideSubForm(): bool
204  {
205  return false;
206  }
207 
208  // Set hidden title (for screenreaders)
209  public function setHiddenTitle(string $a_val): void
210  {
211  $this->hidden_title = $a_val;
212  }
213 
214  public function getHiddenTitle(): string
215  {
216  return $this->hidden_title;
217  }
218 
222  public function getItemByPostVar(string $a_post_var): ?ilFormPropertyGUI
223  {
224  if ($this->getPostVar() == $a_post_var) {
225  return $this;
226  }
227  return null;
228  }
229 
230  public function serializeData(): string
231  {
232  return serialize($this->getValue());
233  }
234 
235  public function unserializeData(string $a_data): void
236  {
237  $data = unserialize($a_data);
238 
239  if ($data) {
240  $this->setValue($data);
241  } else {
242  $this->setValue("");
243  }
244  }
245 
250  public function setParentTable($a_val): void
251  {
252  $this->parent_table = $a_val;
253  }
254 
259  public function getParentTable(): ?ilTable2GUI
260  {
261  return $this->parent_table;
262  }
263 
264  protected function checkParentFormTable(): void
265  {
266  $parent = $this->getParentForm();
267  $parent_table = $this->getParentTable();
268  if (!is_object($parent) && !isset($parent_table)) {
269  throw new Exception("Parent form/table not set for " . get_class($this) . " to use serialize feature.");
270  }
271  }
272 
276  public function writeToSession(): void
277  {
278  $this->checkParentFormTable();
279  ilSession::set($this->getSessionKey(), $this->serializeData());
280  }
281 
282  protected function getSessionKey(): string
283  {
284  $parent = $this->getParentForm();
285  if (!is_object($parent)) {
286  $parent = $this->getParentTable();
287  }
288  return "form_" . $parent->getId() . "_" . $this->getFieldId();
289  }
290 
294  public function clearFromSession(): void
295  {
296  $this->checkParentFormTable();
298  }
299 
303  public function readFromSession(): void
304  {
305  $this->checkParentFormTable();
306  if (ilSession::has($this->getSessionKey())) {
307  $this->unserializeData(ilSession::get($this->getSessionKey()));
308  } else {
309  $this->unserializeData("");
310  }
311  }
312 
313  public function getHiddenTag(
314  string $a_post_var,
315  string $a_value
316  ): string {
317  return '<input type="hidden" name="' . $a_post_var . '" value="' . ilLegacyFormElementsUtil::prepareFormOutput(
318  $a_value
319  ) . '" />';
320  }
321 
322  public function setMulti(
323  bool $a_multi,
324  bool $a_sortable = false,
325  bool $a_addremove = true
326  ): void {
327  if (!$this instanceof ilMultiValuesItem) {
328  throw new ilFormException(sprintf(
329  "%s not supported for form property type %s",
330  __FUNCTION__,
331  get_class($this)
332  ));
333  }
334 
335  $this->multi = $a_multi;
336  $this->multi_sortable = $a_sortable;
337  $this->multi_addremove = $a_addremove;
338  }
339 
340  public function getMulti(): bool
341  {
342  return $this->multi;
343  }
344 
345  public function setMultiValues(array $a_values): void
346  {
347  $this->multi_values = array_unique($a_values);
348  }
349 
350  public function getMultiValues(): array
351  {
352  return $this->multi_values;
353  }
354 
355  // Get HTML for multiple value icons
356  protected function getMultiIconsHTML(): string
357  {
358  $lng = $this->lng;
359 
360  $id = $this->getFieldId();
361 
362  $tpl = new ilTemplate("tpl.multi_icons.html", true, true, "Services/Form");
363 
364  $html = "";
365  if ($this->multi_addremove) {
366  $tpl->setCurrentBlock("addremove");
367  $tpl->setVariable("ID", $id);
368  $tpl->setVariable("TXT_ADD", $lng->txt("add"));
369  $tpl->setVariable("TXT_REMOVE", $lng->txt("remove"));
370  $tpl->setVariable("SRC_ADD", ilGlyphGUI::get(ilGlyphGUI::ADD));
371  $tpl->setVariable("SRC_REMOVE", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
372  $tpl->parseCurrentBlock();
373  }
374 
375  if ($this->multi_sortable) {
376  $tpl->setCurrentBlock("sortable");
377  $tpl->setVariable("ID", $id);
378  $tpl->setVariable("TXT_DOWN", $lng->txt("down"));
379  $tpl->setVariable("TXT_UP", $lng->txt("up"));
380  $tpl->setVariable("SRC_UP", ilGlyphGUI::get(ilGlyphGUI::UP));
381  $tpl->setVariable("SRC_DOWN", ilGlyphGUI::get(ilGlyphGUI::DOWN));
382  $tpl->parseCurrentBlock();
383  }
384 
385  return $tpl->get();
386  }
387 
391  public function getContentOutsideFormTag(): string
392  {
393  return "";
394  }
395 
400  public static function removeProhibitedCharacters(string $a_text): string
401  {
402  return str_replace("\x0B", "", $a_text);
403  }
404 
408  public function stripSlashesAddSpaceFallback(string $a_str): string
409  {
410  $str = ilUtil::stripSlashes($a_str);
411  if ($str != $a_str) {
412  $str = ilUtil::stripSlashes(str_replace("<", "< ", $a_str));
413  }
414  return $str;
415  }
416 
420  public function getTableFilterLabelFor(): string
421  {
422  return $this->getFieldId();
423  }
424 
428  public function getFormLabelFor(): string
429  {
430  return $this->getFieldId();
431  }
432 
433  // get integer parameter kindly
434  protected function int($key): int
435  {
436  if (is_null($this->refinery)) {
437  return 0;
438  }
439  $t = $this->refinery->kindlyTo()->int();
440  return (int) ($this->getRequestParam($key, $t) ?? 0);
441  }
442 
443  // get integer array kindly
444  protected function intArray($key): array
445  {
446  if (!$this->isRequestParamArray($key)) {
447  return [];
448  }
449  $t = $this->refinery->custom()->transformation(
450  function ($arr) {
451  // keep keys(!), transform all values to int
452  return array_column(
453  array_map(
454  function ($k, $v) {
455  return [$k, (int) $v];
456  },
457  array_keys($arr),
458  $arr
459  ),
460  1,
461  0
462  );
463  }
464  );
465  return (array) ($this->getRequestParam($key, $t) ?? []);
466  }
467 
468  // get string parameter kindly
469  protected function str($key): string
470  {
471  if (is_null($this->refinery)) {
472  return "";
473  }
474  $t = $this->refinery->kindlyTo()->string();
475  return $this->stripSlashesAddSpaceFallback(
476  (string) ($this->getRequestParam($key, $t) ?? "")
477  );
478  }
479 
480  // get raw parameter
481  protected function raw($key)
482  {
483  $t = $this->refinery->custom()->transformation(function ($v) {
484  return $v;
485  });
486  return $this->getRequestParam($key, $t);
487  }
488 
489  // get string array kindly
490  protected function strArray($key): array
491  {
492  if (!$this->isRequestParamArray($key)) {
493  return [];
494  }
495  $t = $this->refinery->custom()->transformation(
496  function ($arr) {
497  // keep keys(!), transform all values to string
498  return array_column(
499  array_map(
500  function ($k, $v) {
501  if (is_array($v)) {
502  $v = "";
503  }
504  return [$k, $this->stripSlashesAddSpaceFallback((string) $v)];
505  },
506  array_keys($arr),
507  $arr
508  ),
509  1,
510  0
511  );
512  }
513  );
514  return (array) ($this->getRequestParam($key, $t) ?? []);
515  }
516 
517  // get array of arrays kindly
518  protected function arrayArray($key): array
519  {
520  if (!$this->isRequestParamArray($key)) {
521  return [];
522  }
523  $t = $this->refinery->custom()->transformation(
524  function ($arr) {
525  // keep keys(!), transform all values to string
526  return array_column(
527  array_map(
528  function ($k, $v) {
529  return [$k, (array) $v];
530  },
531  array_keys($arr),
532  $arr
533  ),
534  1,
535  0
536  );
537  }
538  );
539  return (array) ($this->getRequestParam($key, $t) ?? []);
540  }
541 
542  protected function isRequestParamArray(string $key): bool
543  {
544  $no_transform = $this->refinery->identity();
545  $w = $this->http->wrapper();
546  if ($w->post()->has($key)) {
547  return is_array($w->post()->retrieve($key, $no_transform));
548  }
549  if ($w->query()->has($key)) {
550  return is_array($w->query()->retrieve($key, $no_transform));
551  }
552  return false;
553  }
554 
560  public function setRequestParam(string $key, $val): void
561  {
562  $this->set_params[$key] = $val;
563  }
564 
568  protected function getRequestParam(string $key, Refinery\Transformation $t)
569  {
570  if (isset($this->set_params[$key])) {
571  return $this->set_params[$key];
572  }
573  $w = $this->http->wrapper();
574  if ($w->post()->has($key)) {
575  return $w->post()->retrieve($key, $t);
576  }
577  if ($w->query()->has($key)) {
578  return $w->query()->retrieve($key, $t);
579  }
580  return null;
581  }
582 }
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
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 get(string $a_glyph, string $a_text="")
__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:47
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setPostVar(string $a_postvar)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setParentForm(ilPropertyFormGUI $a_parentform)
static http()
Fetches the global http state from ILIAS.
Refinery Factory $refinery
ilGlobalTemplateInterface $global_tpl
string $key
Consumer key/client ID value.
Definition: System.php:193
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
This class represents a property in a property form.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
A transformation is a function from one datatype to another.
disabled()
Example showing how to plug a disabled checkbox into a form.
Definition: disabled.php:10
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
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)