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