ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitGenericMultiInputGUI.php
Go to the documentation of this file.
1 <?php
19 declare(strict_types=1);
20 require_once("./Services/Form/classes/class.ilFormPropertyGUI.php");
21 
28 {
29  public const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
30  public const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
31  public const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
32 
33  public const MULTI_FIELD_ID = "id";
34  public const MULTI_FIELD_OVER = "over";
35  public const MULTI_FIELD_SCOPE = "scope";
36 
37  protected array $cust_attr = [];
38  protected $value;
39  protected array $inputs = [];
40  protected array $input_options = [];
41  protected array $hooks = [];
42  protected array $line_values = [];
43  protected string $template_dir = '';
44  protected array $post_var_cache = [];
45  protected bool $show_label = false;
46  protected bool $show_label_once = false;
47  protected array $hidden_inputs = [];
48  protected bool $position_movable = false;
49  protected int $counter = 0;
50  protected bool $show_info = false;
51  protected bool $render_one_for_empty_value = true;
52 
53  public function __construct(string $a_title = "", string $a_postvar = "")
54  {
55  parent::__construct($a_title, $a_postvar);
56  $this->setType("line_select");
57  $this->setMulti(true);
58  $this->initCSSandJS();
59  }
60 
61  public function getHook(string $key)
62  {
63  if (isset($this->hooks[$key])) {
64  return $this->hooks[$key];
65  }
66 
67  return false;
68  }
69 
70  public function addHook(string $key, array $options)
71  {
72  $this->hooks[$key] = $options;
73  }
74 
75  public function removeHook(string $key): bool
76  {
77  if (isset($this->hooks[$key])) {
78  unset($this->hooks[$key]);
79 
80  return true;
81  }
82 
83  return false;
84  }
85 
86  public function addInput(\ilFormPropertyGUI $input, array $options = []): void
87  {
88  $this->inputs[$input->getPostVar()] = $input;
89  $this->input_options[$input->getPostVar()] = $options;
90  $this->counter++;
91  }
92 
93  public function getTemplateDir(): string
94  {
95  return $this->template_dir;
96  }
97 
98  public function setTemplateDir(string $template_dir)
99  {
100  $this->template_dir = $template_dir;
101  }
102 
103  public function isShowLabel(): bool
104  {
105  return $this->show_label;
106  }
107 
108  public function setShowLabel(bool $show_label)
109  {
110  $this->show_label = $show_label;
111  }
112 
117  public function getInputs(): array
118  {
119  return $this->inputs;
120  }
121 
122  public function setMulti(bool $a_multi, bool $a_sortable = false, bool $a_addremove = true): void
123  {
124  $this->multi = $a_multi;
125  }
126 
127  public function setValue(array $value)
128  {
129  $this->value = $value;
130 
131  foreach ($this->inputs as $key => $item) {
132  if ($item instanceof \ilDateTimeInputGUI) {
133  $item->setDate(new \ilDate($value[$key]['date'], IL_CAL_DATE));
134  } else {
135  if (array_key_exists($key, $value)) {
136  $item->setValue((string) $value[$key]);
137  }
138  }
139  }
140  }
141 
142  public function getValue(): array
143  {
144  $out = [];
145  foreach ($this->inputs as $key => $item) {
146  $out[$key] = $item->getValue();
147  }
148 
149  return $out;
150  }
151 
152  public function setValueByArray(array $a_values): void
153  {
154  $data = $a_values[$this->getPostVar()] ?? [];
155  if ($this->getMulti()) {
156  $this->line_values = $data;
157  } else {
158  $this->setValue($data);
159  }
160  }
161 
166  public function checkInput(): bool
167  {
168  $internal_fields = array_keys($this->inputs);
169  $key = $this->getPostVar();
170  $post = $this->raw($key);
171 
172  if (is_array($post)) {
173  foreach ($post as $authority) {
174  if (!(
175  array_key_exists(self::MULTI_FIELD_ID, $authority) &&
176  array_key_exists(self::MULTI_FIELD_OVER, $authority) &&
177  array_key_exists(self::MULTI_FIELD_SCOPE, $authority) &&
178  trim($authority[self::MULTI_FIELD_OVER]) !== '' &&
179  trim($authority[self::MULTI_FIELD_SCOPE]) !== ''
180  )) {
181  $this->setAlert($this->lng->txt("msg_input_is_required"));
182  return false;
183  }
184  }
185  }
186 
187  return true;
188  }
189 
190  public function addCustomAttribute(string $key, string $value, bool $override = false): void
191  {
192  if (isset($this->cust_attr[$key]) && !$override) {
193  $this->cust_attr[$key] .= ' ' . $value;
194  } else {
195  $this->cust_attr[$key] = $value;
196  }
197  }
198 
199  public function getCustomAttributes(): array
200  {
201  return (array) $this->cust_attr;
202  }
203 
204  private function createInputPostVar(string $iterator_id, \ilFormPropertyGUI $input): string
205  {
206  if ($this->getMulti()) {
207  return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
208  } else {
209  return $this->getPostVar() . '[' . $input->getPostVar() . ']';
210  }
211  }
212 
213 
214  public function render(int|string $iterator_id = 0, bool $clean_render = false): string
215  {
216  $first_label = true;
217  // $tpl = new \ilTemplate("tpl.multi_line_input.html", true, true, 'Customizing/global/plugins/Services/Repository/RepositoryObject/LiveVoting');
218  $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/OrgUnit');
219 
220  $class = 'multi_input_line';
221  $this->addCustomAttribute('class', $class, true);
222  foreach ($this->getCustomAttributes() as $key => $value) {
223  $tpl->setCurrentBlock('cust_attr');
224  $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
225  $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
226  $tpl->parseCurrentBlock();
227  }
228  $inputs = $this->inputs;
229  foreach ($inputs as $key => $input) {
230  $input = clone $input;
231  $is_hidden = false;
232  $is_ta = false;
233  if (!method_exists($input, 'render')) {
234  switch (true) {
235  case ($input instanceof \ilHiddenInputGUI):
236  $is_hidden = true;
237  break;
238  case ($input instanceof \ilTextAreaInputGUI):
239  $is_ta = true;
240  break;
241  default:
242  throw new \ilException("Method " . get_class($input)
243  . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
244  }
245  }
246 
247  $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
248  if ($is_disabled_hook !== false && !$clean_render) {
249  $input->setDisabled($is_disabled_hook($this->getValue()));
250  }
251  if ($this->getDisabled()) {
252  $input->setDisabled(true);
253  }
254  if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
255  $this->post_var_cache[$key] = $input->getPostVar();
256  } else {
257  // Reset post var
258  $input->setPostVar($this->post_var_cache[$key]);
259  }
260  $post_var = $this->createInputPostVar((string) $iterator_id, $input);
261  $input->setPostVar($post_var);
262  $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
263  if ($before_render_hook !== false && !$clean_render) {
264  $input = $before_render_hook($this->getValue(), $key, $input);
265  }
266  switch (true) {
267  case $is_hidden:
268  $tpl->setCurrentBlock('hidden');
269  $tpl->setVariable('NAME', $post_var);
270  $tpl->setVariable('VALUE', ilLegacyFormElementsUtil::prepareFormOutput($input->getValue()));
271  break;
272  case $is_ta:
273  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
274  $tpl->setCurrentBlock('input_label');
275  $tpl->setVariable('LABEL', $input->getTitle());
276  $tpl->setVariable('CONTENT', $input->getHTML());
277  $tpl->parseCurrentBlock();
278  $first_label = false;
279  } else {
280  $tpl->setCurrentBlock('input');
281  $tpl->setVariable('CONTENT', $input->getHTML());
282  }
283  break;
284  default:
285  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
286  $tpl->setCurrentBlock('input_label');
287  $tpl->setVariable('LABEL', $input->getTitle());
288  $tpl->setVariable('CONTENT', $input->render());
289  $first_label = false;
290  } else {
291  $tpl->setCurrentBlock('input');
292  $tpl->setVariable('CONTENT', $input->render());
293  }
294  break;
295  }
296  if ($this->isShowInfo()) {
297  if ($this->isShowLabel()) {
298  $tpl->setCurrentBlock('input_info_label');
299  $tpl->setVariable('INFO_LABEL', $input->getInfo());
300  $tpl->parseCurrentBlock();
301  } else {
302  $tpl->setCurrentBlock('input_info');
303  $tpl->setVariable('INFO', $input->getInfo());
304  $tpl->parseCurrentBlock();
305  }
306  }
307  $tpl->parseCurrentBlock();
308  }
309  if ($this->getMulti() && !$this->getDisabled()) {
310  $image_plus = ilGlyphGUI::get(ilGlyphGUI::ADD);
311  $show_remove = true;
312  $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
313  if ($is_removeable_hook !== false && !$clean_render) {
314  $show_remove = $is_removeable_hook($this->getValue());
315  }
316  $show_remove = true;
317  $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
318  $tpl->setCurrentBlock('multi_icons');
319  $tpl->setVariable('IMAGE_PLUS', $image_plus);
320  $tpl->setVariable('IMAGE_MINUS', $image_minus);
321  $tpl->parseCurrentBlock();
322  if ($this->isPositionMovable()) {
323  $tpl->setCurrentBlock('multi_icons_move');
324  $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
325  $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
326  $tpl->parseCurrentBlock();
327  }
328  }
329 
330  return $tpl->get();
331  }
332 
333  public function initCSSandJS()
334  {
335  global $tpl;
336  $tpl->addJavascript('Modules/OrgUnit/js/generic_multi_line_input.js');
337  }
338 
343  public function insert(\ilTemplate $a_tpl): void
344  {
345  $output = "";
346 
347  $output .= $this->render(0, true);
348  if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
349  $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/OrgUnit');
350  $image_plus = ilGlyphGUI::get(ilGlyphGUI::ADD);
351  $image_minus = '<span class="glyphicon glyphicon-minus hide"></span>';
352 
353  $tpl->setVariable('ADDITIONAL_ATTRS', "id='multi_line_add_button' style='display:none'");
354  $tpl->setCurrentBlock('multi_icons');
355  $tpl->setVariable('IMAGE_PLUS', $image_plus);
356  $tpl->setVariable('IMAGE_MINUS', $image_minus);
357  $tpl->parseCurrentBlock();
358  $output .= $tpl->get();
359 
360  foreach ($this->line_values as $run => $data) {
361  $object = $this;
362  $object->setValue($data);
363  $output .= $object->render($run);
364  }
365  } else {
366  if ($this->render_one_for_empty_value) {
367  $output .= $this->render(0, true);
368  } else {
369  $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/OrgUnit');
370  $image_plus = ilGlyphGUI::get(ilGlyphGUI::ADD);
371  $image_minus = '<span class="glyphicon glyphicon-minus hide"></span>';
372 
373  $tpl->setVariable('ADDITIONAL_ATTRS', "id='multi_line_add_button'");
374  $tpl->setCurrentBlock('multi_icons');
375  $tpl->setVariable('IMAGE_PLUS', $image_plus);
376  $tpl->setVariable('IMAGE_MINUS', $image_minus);
377  $tpl->parseCurrentBlock();
378  $output .= $tpl->get();
379  }
380  }
381  if ($this->getMulti()) {
382  $output = "<div id='{$this->getFieldId()}' class='multi_line_input'>{$output}</div>";
383 
384  global $tpl;
385  $options = json_encode($this->input_options);
386  $tpl->addOnLoadCode("$('#{$this->getFieldId()}').multi_line_input({$this->getFieldId()}, '{$options}')");
387  }
388 
389  $a_tpl->setCurrentBlock("prop_generic");
390  $a_tpl->setVariable("PROP_GENERIC", $output);
391  $a_tpl->parseCurrentBlock();
392  }
393 
397  public function getTableFilterHTML(): string
398  {
399  return $this->render();
400  }
401 
405  public function getToolbarHTML(): string
406  {
407  return $this->render("toolbar");
408  }
409 
410  public function isPositionMovable(): bool
411  {
413  }
414 
415  public function setPositionMovable(bool $position_movable): void
416  {
417  $this->position_movable = $position_movable;
418  }
419 
420  public function isShowLabelOnce(): bool
421  {
422  return $this->show_label_once;
423  }
424 
425  public function setShowLabelOnce(bool $show_label_once): void
426  {
427  $this->setShowLabel(false);
428  $this->show_label_once = $show_label_once;
429  }
430 
431  public function isShowInfo(): bool
432  {
433  return $this->show_info;
434  }
435 
436  public function setShowInfo(bool $show_info): void
437  {
438  $this->show_info = $show_info;
439  }
440 
441  public function isRenderOneForEmptyValue(): bool
442  {
444  }
445 
446  public function setRenderOneForEmptyValue(bool $render_one_for_empty_value): void
447  {
448  $this->render_one_for_empty_value = $render_one_for_empty_value;
449  }
450 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
addInput(\ilFormPropertyGUI $input, array $options=[])
checkInput()
Check input, strip slashes etc.
insert(\ilTemplate $a_tpl)
Insert property html.
setMulti(bool $a_multi, bool $a_sortable=false, bool $a_addremove=true)
static get(string $a_glyph, string $a_text="")
render(int|string $iterator_id=0, bool $clean_render=false)
addCustomAttribute(string $key, string $value, bool $override=false)
static prepareFormOutput($a_str, bool $a_strip=false)
setRenderOneForEmptyValue(bool $render_one_for_empty_value)
This class represents a date/time property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
__construct(string $a_title="", string $a_postvar="")
$out
Definition: buildRTE.php:24
string $key
Consumer key/client ID value.
Definition: System.php:193
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
const IL_CAL_DATE
createInputPostVar(string $iterator_id, \ilFormPropertyGUI $input)
This class represents a text area property in a property form.
$post
Definition: ltitoken.php:49