ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDclGenericMultiInputGUI.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  public const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
22  public const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
23  public const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
24 
25  protected array $cust_attr = [];
26  protected array $value = [];
27  protected array $inputs = [];
28  protected array $input_options = [];
29  protected array $hooks = [];
30  protected ?array $line_values = [];
31  protected string $template_dir = '';
32  protected array $post_var_cache = [];
33  protected bool $show_label = false;
34  protected int $limit = 999999;
35  protected bool $allow_empty_fields = false;
36 
37  public function getLimit(): int
38  {
39  return $this->limit;
40  }
41 
42  public function setLimit(int $limit): void
43  {
44  $this->limit = $limit;
45  }
46 
47  public function isAllowEmptyFields(): bool
48  {
50  }
51 
52  public function setAllowEmptyFields(bool $allow_empty_fields): void
53  {
54  $this->allow_empty_fields = $allow_empty_fields;
55  }
56 
60  public function __construct(string $a_title = "", string $a_postvar = "")
61  {
62  parent::__construct($a_title, $a_postvar);
63  $this->setType("line_select");
64  $this->setMulti(true);
65  }
66 
70  public function getHook(string $key)
71  {
72  if (isset($this->hooks[$key])) {
73  return $this->hooks[$key];
74  }
75 
76  return false;
77  }
78 
79  public function addHook(string $key, array $options)
80  {
81  $this->hooks[$key] = $options;
82  }
83 
84  public function removeHook(string $key): bool
85  {
86  if (isset($this->hooks[$key])) {
87  unset($this->hooks[$key]);
88 
89  return true;
90  }
91 
92  return false;
93  }
94 
95  public function addInput(ilFormPropertyGUI $input, array $options = [])
96  {
97  $input->setRequired(!$this->allow_empty_fields);
98  $this->inputs[$input->getPostVar()] = $input;
99  $this->input_options[$input->getPostVar()] = $options;
100  }
101 
102  public function isShowLabel(): bool
103  {
104  return $this->show_label;
105  }
106 
107  public function setShowLabel(bool $show_label): void
108  {
109  $this->show_label = $show_label;
110  }
111 
116  public function getInputs(): array
117  {
118  return $this->inputs;
119  }
120 
121  public function setMulti(
122  bool $a_multi,
123  bool $a_sortable = false,
124  bool $a_addremove = true
125  ): void {
126  $this->multi = $a_multi;
127  $this->multi_sortable = $a_sortable;
128  }
129 
133  public function setValue(array $value): void
134  {
135  $this->value = $value;
136 
137  foreach ($this->inputs as $key => $item) {
138  if (array_key_exists($key, $value)) {
139  if ($item instanceof ilCheckboxInputGUI) {
140  $item->setChecked((bool) $value[$key]);
141  } else {
142  if ($item instanceof ilDateTimeInputGUI) {
144  $item->setDate(new ilDate($value[$key], IL_CAL_DATE));
145  } else {
146  $item->setDate();
147  }
148  } else {
149  $item->setValue($value[$key]);
150  }
151  }
152  }
153  }
154  }
155 
159  public function getValue(): array
160  {
161  $out = [];
162  foreach ($this->inputs as $key => $item) {
163  $out[$key] = $item->getValue();
164  }
165 
166  return $out;
167  }
168 
172  public function setValueByArray(array $a_values): void
173  {
174  $data = $a_values[$this->getPostVar()] ?? [];
175  if ($this->getMulti()) {
176  $this->line_values = $data;
177  } else {
178  $this->setValue($data);
179  }
180  }
181 
186  public function checkInput(): bool
187  {
188  global $lng;
189 
190  $valid = true;
191 
192  $value = $this->arrayArray($this->getPostVar());
193  // escape data
194  $out_array = [];
195  foreach ($value as $item_num => $item) {
196  foreach ($this->inputs as $input_key => $input) {
197  if (isset($item[$input_key])) {
198  if ($input instanceof ilDateTimeInputGUI) {
199  $out = (is_string($item[$input_key])) ? ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
201  $out_array[$item_num][$input_key] = $out;
202  } else {
203  $valid = false;
204  $this->setAlert($this->lng->txt("form_msg_wrong_date"));
205  $out_array[$item_num][$input_key] = null;
206  }
207  }
208  }
209  }
210  }
211 
212  $this->setValue($out_array);
213 
214  if ($this->getRequired() && !trim(implode("", $this->getValue()))) {
215  $this->setAlert($lng->txt("msg_input_is_required"));
216  $valid = false;
217  }
218 
219  return $valid;
220  }
221 
222  public function addCustomAttribute(string $key, string $value, bool $override = false): void
223  {
224  if (isset($this->cust_attr[$key]) && !$override) {
225  $this->cust_attr[$key] .= ' ' . $value;
226  } else {
227  $this->cust_attr[$key] = $value;
228  }
229  }
230 
231  public function getCustomAttributes(): array
232  {
233  return $this->cust_attr;
234  }
235 
236  protected function createInputPostVar(int $iterator_id, ilFormPropertyGUI $input): string
237  {
238  if ($this->getMulti()) {
239  return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
240  } else {
241  return $this->getPostVar() . '[' . $input->getPostVar() . ']';
242  }
243  }
244 
249  public function render(int $iterator_id = 0, bool $clean_render = false): string
250  {
251  $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/DataCollection');
252 
253  $class = 'multi_input_line';
254 
255  $this->addCustomAttribute('class', $class, true);
256  foreach ($this->getCustomAttributes() as $key => $value) {
257  $tpl->setCurrentBlock('cust_attr');
258  $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
259  $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
260  $tpl->parseCurrentBlock();
261  }
262 
263  $inputs = $this->inputs;
264 
265  foreach ($inputs as $key => $input) {
266  $input = clone $input;
267  if (!method_exists($input, 'render')) {
268  throw new ilException(
269  "Method " . get_class($input)
270  . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI"
271  );
272  }
273 
274  $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
275  if ($is_disabled_hook !== false && !$clean_render) {
276  $input->setDisabled($is_disabled_hook($this->getValue()));
277  }
278 
279  if ($this->getDisabled()) {
280  $input->setDisabled(true);
281  }
282 
283  if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
284  $this->post_var_cache[$key] = $input->getPostVar();
285  } else {
286  // Reset post var
287  $input->setPostVar($this->post_var_cache[$key]);
288  }
289 
290  $post_var = $this->createInputPostVar($iterator_id, $input);
291  $input->setPostVar($post_var);
292 
293  $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
294  if ($before_render_hook !== false && !$clean_render) {
295  $input = $before_render_hook($this->getValue(), $key, $input);
296  }
297 
298  //var_dump($input);
299 
300  if ($this->isShowLabel()) {
301  $tpl->setCurrentBlock('input_label');
302  $tpl->setVariable('LABEL', $input->getTitle());
303  } else {
304  $tpl->setCurrentBlock('input');
305  }
306  $tpl->setVariable('CONTENT', $input->render());
307  $tpl->parseCurrentBlock();
308  }
309 
310  if ($this->getMulti() && !$this->getDisabled()) {
311  $tpl->setVariable('IMAGE_MINUS', ilGlyphGUI::get(ilGlyphGUI::REMOVE));
312 
313  $show_remove = true;
314  $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
315  if ($is_removeable_hook !== false && !$clean_render) {
316  $show_remove = $is_removeable_hook($this->getValue());
317  }
318 
319  $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
320  $tpl->setCurrentBlock('multi_icons');
321  $tpl->setVariable('IMAGE_PLUS', ilGlyphGUI::get(ilGlyphGUI::ADD));
322  $tpl->setVariable('IMAGE_MINUS', $image_minus);
323  if ($this->multi_sortable) {
324  $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
325  $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
326  }
327  $tpl->parseCurrentBlock();
328  }
329 
330  return $tpl->get();
331  }
332 
336  public function insert(ilTemplate $a_tpl): void
337  {
338  $output = "";
339 
340  $output .= $this->render(0, true);
341 
342  if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
343  $counter = 0;
344  foreach ($this->line_values as $i => $data) {
345  $object = $this;
346  $object->setValue($data);
347  $output .= $object->render($i);
348  $counter++;
349  }
350  } else {
351  $output .= $this->render(1, true);
352  }
353 
354  if ($this->getMulti()) {
355  $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output . '</div>';
356  $this->global_tpl->addJavaScript('Modules/DataCollection/js/generic_multi_line_input.js');
357  $id = $this->getFieldId();
358  $element_config = json_encode($this->input_options);
359  $options = json_encode(['limit' => $this->limit,
360  'sortable' => $this->multi_sortable,
361  'locale' => $this->lng->getLangKey()]);
362  $this->global_tpl->addOnLoadCode("il.DataCollection.genericMultiLineInit('$id',$element_config,$options);");
363  }
364 
365  $a_tpl->setCurrentBlock("prop_generic");
366  $a_tpl->setVariable("PROP_GENERIC", $output);
367  $a_tpl->parseCurrentBlock();
368  }
369 
373  public function getTableFilterHTML(): string
374  {
375  $html = $this->render();
376 
377  return $html;
378  }
379 
383  public function getToolbarHTML(): string
384  {
385  $html = $this->render("toolbar");
386 
387  return $html;
388  }
389 
390  public function getSubItems(): array
391  {
392  return [];
393  }
394 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
addHook(string $key, array $options)
checkInput()
Check input, strip slashes etc.
static get(string $a_glyph, string $a_text="")
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$valid
setAllowEmptyFields(bool $allow_empty_fields)
createInputPostVar(int $iterator_id, ilFormPropertyGUI $input)
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insert(ilTemplate $a_tpl)
Insert property html.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
__construct(string $a_title="", string $a_postvar="")
Constructor.
addCustomAttribute(string $key, string $value, bool $override=false)
$out
Definition: buildRTE.php:24
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addInput(ilFormPropertyGUI $input, array $options=[])
render(int $iterator_id=0, bool $clean_render=false)
Render item.
setRequired(bool $a_required)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
getTableFilterHTML()
Get HTML for table filter.
setMulti(bool $a_multi, bool $a_sortable=false, bool $a_addremove=true)
const IL_CAL_DATE
This class represents a property in a property form.
setValueByArray(array $a_values)
Set value by array.
__construct(Container $dic, ilPlugin $plugin)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static parseIncomingDate($a_value, bool $a_add_time=false)
Try to parse incoming value to date object.
$i
Definition: metadata.php:41