ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitMultiLineInputGUI.php
Go to the documentation of this file.
1 <?php
19 declare(strict_types=1);
20 
27 {
28  public const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
29  public const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
30  public const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
31  protected array $cust_attr = array();
32  protected $value;
33  protected array $inputs = array();
34  protected array $input_options = array();
36  protected array $hooks = array();
37  protected array $line_values = array();
38  protected string $template_dir = '';
39  protected array $post_var_cache = array();
40  protected bool $show_label = false;
41  protected bool $show_label_once = false;
42  protected array $hidden_inputs = array();
43  protected bool $position_movable = false;
44  protected int $counter = 0;
45  protected bool $show_info = false;
46 
52  public function __construct(string $a_title = "", string $a_postvar = "")
53  {
54  parent::__construct($a_title, $a_postvar);
55  $this->setType("line_select");
56  $this->setMulti(true);
57  $this->initCSSandJS();
58  }
59 
60  public function getHook(string $key): ?string
61  {
62  if (isset($this->hooks[$key])) {
63  return $this->hooks[$key];
64  }
65 
66  return false;
67  }
68 
69  public function addHook(string $key, array $options): void
70  {
71  $this->hooks[$key] = $options;
72  }
73 
74  public function removeHook(string $key): void
75  {
76  if (isset($this->hooks[$key])) {
77  unset($this->hooks[$key]);
78  }
79  }
80 
81  public function addInput(\ilFormPropertyGUI $input, array $options = array()): void
82  {
83  $this->inputs[$input->getPostVar()] = $input;
84  $this->input_options[$input->getPostVar()] = $options;
85  $this->counter++;
86  }
87 
88  public function getTemplateDir(): string
89  {
90  return $this->template_dir;
91  }
92 
93  public function setTemplateDir(string $template_dir): void
94  {
95  $this->template_dir = $template_dir;
96  }
97 
98  public function isShowLabel(): bool
99  {
100  return $this->show_label;
101  }
102 
103  public function setShowLabel(bool $show_label)
104  {
105  $this->show_label = $show_label;
106  }
107 
108  public function getInputs(): array
109  {
110  return $this->inputs;
111  }
112 
113  public function setMulti(bool $a_multi, bool $a_sortable = false, bool $a_addremove = true): void
114  {
115  $this->multi = $a_multi;
116  }
117 
118  public function setValue(string $a_value): void
119  {
120  foreach ($this->inputs as $key => $item) {
121  if (method_exists($item, 'setValue')) {
122  $item->setValue($a_value[$key]);
123  } elseif ($item instanceof \ilDateTimeInputGUI) {
124  $item->setDate(new \ilDate($a_value[$key]['date'], IL_CAL_DATE));
125  }
126  }
127  $this->value = $a_value;
128  }
129 
130  public function getValue(): array
131  {
132  $out = array();
133  foreach ($this->inputs as $key => $item) {
134  $out[$key] = $item->getValue();
135  }
136 
137  return $out;
138  }
139 
144  public function setValueByArray(array $a_values): void
145  {
146  $data = $a_values[$this->getPostVar()];
147  if ($this->getMulti()) {
148  $this->line_values = $data;
149  } else {
150  $this->setValue($data);
151  }
152  }
153 
158  public function checkInput(): bool
159  {
160  global $lng;
161  $valid = true;
162  // escape data
163  $out_array = array();
164  foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
165  foreach ($this->inputs as $input_key => $input) {
166  if (isset($item[$input_key])) {
167  $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? \ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
168  }
169  }
170  }
171  $_POST[$this->getPostVar()] = $out_array;
172  if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
173  $valid = false;
174  }
175  // validate
176  foreach ($this->inputs as $input_key => $inputs) {
177  if (!$inputs->checkInput()) {
178  $valid = false;
179  }
180  }
181  if (!$valid) {
182  $this->setAlert($lng->txt("msg_input_is_required"));
183 
184  return false;
185  }
186 
187  return $valid;
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 
217  public function render(int $iterator_id = 0, bool $clean_render = false): string
218  {
219  $first_label = true;
220  $tpl = new \ilTemplate(
221  "tpl.multi_line_input.html",
222  true,
223  true,
224  'Customizing/global/plugins/Services/Repository/RepositoryObject/LiveVoting'
225  );
226  $class = 'multi_input_line';
227  $this->addCustomAttribute('class', $class, true);
228  foreach ($this->getCustomAttributes() as $key => $value) {
229  $tpl->setCurrentBlock('cust_attr');
230  $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
231  $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
232  $tpl->parseCurrentBlock();
233  }
234  $inputs = $this->inputs;
235  foreach ($inputs as $key => $input) {
236  $input = clone $input;
237  $is_hidden = false;
238  $is_ta = false;
239  if (!method_exists($input, 'render')) {
240  switch (true) {
241  case ($input instanceof \ilHiddenInputGUI):
242  $is_hidden = true;
243  break;
244  case ($input instanceof \ilTextAreaInputGUI):
245  $is_ta = true;
246  break;
247  default:
248  throw new \ilException("Method " . get_class($input)
249  . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
250  }
251  }
252 
253  $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
254  if ($is_disabled_hook !== false && !$clean_render) {
255  $input->setDisabled($is_disabled_hook($this->getValue()));
256  }
257  if ($this->getDisabled()) {
258  $input->setDisabled(true);
259  }
260  if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
261  $this->post_var_cache[$key] = $input->getPostVar();
262  } else {
263  // Reset post var
264  $input->setPostVar($this->post_var_cache[$key]);
265  }
266  $post_var = $this->createInputPostVar($iterator_id, $input);
267  $input->setPostVar($post_var);
268  $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
269  if ($before_render_hook !== false && !$clean_render) {
270  $input = $before_render_hook($this->getValue(), $key, $input);
271  }
272  switch (true) {
273  case $is_hidden:
274  $tpl->setCurrentBlock('hidden');
275  $tpl->setVariable('NAME', $post_var);
276  $tpl->setVariable('VALUE', ilLegacyFormElementsUtil::prepareFormOutput($input->getValue()));
277  break;
278  case $is_ta:
279  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
280  $tpl->setCurrentBlock('input_label');
281  $tpl->setVariable('LABEL', $input->getTitle());
282  $tpl->setVariable('CONTENT', $input->getHTML());
283  $tpl->parseCurrentBlock();
284  $first_label = false;
285  } else {
286  $tpl->setCurrentBlock('input');
287  $tpl->setVariable('CONTENT', $input->getHTML());
288  }
289  break;
290  default:
291  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
292  $tpl->setCurrentBlock('input_label');
293  $tpl->setVariable('LABEL', $input->getTitle());
294  $tpl->setVariable('CONTENT', $input->render());
295  $first_label = false;
296  } else {
297  $tpl->setCurrentBlock('input');
298  $tpl->setVariable('CONTENT', $input->render());
299  }
300  break;
301  }
302  if ($this->isShowInfo()) {
303  if ($this->isShowLabel()) {
304  $tpl->setCurrentBlock('input_info_label');
305  $tpl->setVariable('INFO_LABEL', $input->getInfo());
306  $tpl->parseCurrentBlock();
307  } else {
308  $tpl->setCurrentBlock('input_info');
309  $tpl->setVariable('INFO', $input->getInfo());
310  $tpl->parseCurrentBlock();
311  }
312  }
313  $tpl->parseCurrentBlock();
314  }
315  if ($this->getMulti() && !$this->getDisabled()) {
316  $image_plus = xlvoGlyphGUI::get('plus');
317  $show_remove = true;
318  $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
319  if ($is_removeable_hook !== false && !$clean_render) {
320  $show_remove = $is_removeable_hook($this->getValue());
321  }
322  $show_remove = true;
323  $image_minus = ($show_remove) ? xlvoGlyphGUI::get('minus') : '<span class="glyphicon glyphicon-minus hide"></span>';
324  $tpl->setCurrentBlock('multi_icons');
325  $tpl->setVariable('IMAGE_PLUS', $image_plus);
326  $tpl->setVariable('IMAGE_MINUS', $image_minus);
327  $tpl->parseCurrentBlock();
328  if ($this->isPositionMovable()) {
329  $tpl->setCurrentBlock('multi_icons_move');
330  $tpl->setVariable('IMAGE_UP', xlvoGlyphGUI::get(xlvoGlyphGUI::UP));
331  $tpl->setVariable('IMAGE_DOWN', xlvoGlyphGUI::get(xlvoGlyphGUI::DOWN));
332  $tpl->parseCurrentBlock();
333  }
334  }
335 
336  return $tpl->get();
337  }
338 
339  public function initCSSandJS(): void
340  {
341  global $tpl;
342  $tpl->addJavascript('./Modules/OrgUnit/templates/default/multi_line_input.js');
343  }
344 
345  public function insert(ilTemplate $a_tpl): int
346  {
347  $output = "";
348 
349  $output .= $this->render(0, true);
350  if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
351  foreach ($this->line_values as $run => $data) {
352  $object = $this;
353  $object->setValue($data);
354  $output .= $object->render($run);
355  }
356  } else {
357  $output .= $this->render(0, true);
358  }
359  if ($this->getMulti()) {
360  $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output
361  . '</div>';
362  $output .= '<script type="text/javascript">$("#' . $this->getFieldId()
363  . '").multi_line_input(' . json_encode($this->input_options) . ')</script>';
364  }
365  $a_tpl->setCurrentBlock("prop_generic");
366  $a_tpl->setVariable("PROP_GENERIC", $output);
367  $a_tpl->parseCurrentBlock();
368  }
369 
373  public function getTableFilterHTML()
374  {
375  return $this->render();
376  }
377 
381  public function getToolbarHTML(): string
382  {
383  return $this->render("toolbar");
384  }
385 
386  public function isPositionMovable(): bool
387  {
389  }
390 
391  public function setPositionMovable(bool $position_movable)
392  {
393  $this->position_movable = $position_movable;
394  }
395 
396  public function isShowLabelOnce(): bool
397  {
398  return $this->show_label_once;
399  }
400 
401  public function setShowLabelOnce(bool $show_label_once): void
402  {
403  $this->setShowLabel(false);
404  $this->show_label_once = $show_label_once;
405  }
406 
407  public function isShowInfo(): bool
408  {
409  return $this->show_info;
410  }
411 
412  public function setShowInfo(bool $show_info): void
413  {
414  $this->show_info = $show_info;
415  }
416 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
addInput(\ilFormPropertyGUI $input, array $options=array())
getTableFilterHTML()
Get HTML for table filter.
addCustomAttribute(string $key, string $value, bool $override=false)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$valid
static prepareFormOutput($a_str, bool $a_strip=false)
setMulti(bool $a_multi, bool $a_sortable=false, bool $a_addremove=true)
This class represents a date/time property in a property form.
checkInput()
Check input, strip slashes etc.
__construct(string $a_title="", string $a_postvar="")
Constructor.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createInputPostVar(string $iterator_id, \ilFormPropertyGUI $input)
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
$out
Definition: buildRTE.php:24
string $key
Consumer key/client ID value.
Definition: System.php:193
get(string $key, Refinery\Transformation $t)
Get passed parameter, if not data passed, get key from http request.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
const IL_CAL_DATE
This class represents a property in a property form.
render(int $iterator_id=0, bool $clean_render=false)
This class represents a text area property in a property form.
Class ilOrgUnitMultiLineInputGUI.
setValueByArray(array $a_values)
Set value by array.