ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDclGenericMultiInputGUI.php
Go to the documentation of this file.
1<?php
2
10{
11 const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
12 const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
13 const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
17 protected $cust_attr = array();
21 protected $value;
25 protected $inputs = array();
29 protected $input_options = array();
33 protected $hooks = array();
37 protected $line_values = array();
41 protected $template_dir = '';
45 protected $post_var_cache = array();
49 protected $show_label = false;
53 protected $limit = 0;
57 protected $allow_empty_fields = false;
58
59
63 public function getLimit()
64 {
65 return $this->limit;
66 }
67
68
74 public function setLimit($limit)
75 {
76 $this->limit = $limit;
77 }
78
79
83 public function isAllowEmptyFields()
84 {
86 }
87
88
93 {
94 $this->allow_empty_fields = $allow_empty_fields;
95 }
96
97
104 public function __construct($a_title = "", $a_postvar = "")
105 {
106 parent::__construct($a_title, $a_postvar);
107 $this->setType("line_select");
108 $this->setMulti(true);
109 }
110
111
115 public function getHook($key)
116 {
117 if (isset($this->hooks[$key])) {
118 return $this->hooks[$key];
119 }
120
121 return false;
122 }
123
124
128 public function addHook($key, $options)
129 {
130 $this->hooks[$key] = $options;
131 }
132
133
139 public function removeHook($key)
140 {
141 if (isset($this->hooks[$key])) {
142 unset($this->hooks[$key]);
143
144 return true;
145 }
146
147 return false;
148 }
149
150
155 public function addInput(ilFormPropertyGUI $input, $options = array())
156 {
157 $input->setRequired(!$this->allow_empty_fields);
158 $this->inputs[$input->getPostVar()] = $input;
159 $this->input_options[$input->getPostVar()] = $options;
160 }
161
162
166 public function isShowLabel()
167 {
168 return $this->show_label;
169 }
170
171
175 public function setShowLabel($show_label)
176 {
177 $this->show_label = $show_label;
178 }
179
180
186 public function getInputs()
187 {
188 return $this->inputs;
189 }
190
191
195 public function setMulti($a_multi, $a_sortable = false, $a_addremove = true)
196 {
197 $this->multi = $a_multi;
198 $this->multi_sortable = $a_sortable;
199 }
200
201
207 public function setValue($a_value)
208 {
209 foreach ($this->inputs as $key => $item) {
210 if ($item instanceof ilCheckboxInputGUI) {
211 $item->setChecked((bool) $a_value[$key]);
212 } else {
213 if ($item instanceof ilDateTimeInputGUI) {
214 $item->setDate(new ilDate($a_value[$key], IL_CAL_DATE));
215 } else {
216 if (method_exists($item, 'setValue')) {
217 $item->setValue($a_value[$key]);
218 }
219 }
220 }
221 }
222 $this->value = $a_value;
223 }
224
225
231 public function getValue()
232 {
233 $out = array();
234 foreach ($this->inputs as $key => $item) {
235 $out[$key] = $item->getValue();
236 }
237
238 return $out;
239 }
240
241
247 public function setValueByArray($a_values)
248 {
249 $data = $a_values[$this->getPostVar()];
250 if ($this->getMulti()) {
251 $this->line_values = $data;
252 } else {
253 $this->setValue($data);
254 }
255 }
256
257
263 public function checkInput()
264 {
265 global $lng;
266
267 $valid = true;
268
269 // escape data
270 $out_array = array();
271 foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
272 foreach ($this->inputs as $input_key => $input) {
273 if (isset($item[$input_key])) {
274 $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
275 }
276 }
277 }
278 $_POST[$this->getPostVar()] = $out_array;
279
280 if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
281 $valid = false;
282 }
283
284 // validate
285 foreach ($this->inputs as $input_key => $inputs) {
286 foreach ($out_array as $subitem) {
287 $_POST[$inputs->getPostVar()] = $subitem[$inputs->getPostVar()];
288 if (!$inputs->checkInput()) {
289 $valid = false;
290 }
291 }
292 }
293
294 if (!$valid) {
295 $this->setAlert($lng->txt("msg_input_is_required"));
296
297 return false;
298 }
299
300 return $valid;
301 }
302
303
309 public function addCustomAttribute($key, $value, $override = false)
310 {
311 if (isset($this->cust_attr[$key]) && !$override) {
312 $this->cust_attr[$key] .= ' ' . $value;
313 } else {
314 $this->cust_attr[$key] = $value;
315 }
316 }
317
318
322 public function getCustomAttributes()
323 {
324 return (array) $this->cust_attr;
325 }
326
327
334 protected function createInputPostVar($iterator_id, ilFormPropertyGUI $input)
335 {
336 if ($this->getMulti()) {
337 return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
338 } else {
339 return $this->getPostVar() . '[' . $input->getPostVar() . ']';
340 }
341 }
342
343
352 public function render($iterator_id = 0, $clean_render = false)
353 {
354 $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/DataCollection');
355
356 $class = 'multi_input_line';
357
358 $this->addCustomAttribute('class', $class, true);
359 foreach ($this->getCustomAttributes() as $key => $value) {
360 $tpl->setCurrentBlock('cust_attr');
361 $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
362 $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
363 $tpl->parseCurrentBlock();
364 }
365
367
368 foreach ($inputs as $key => $input) {
369 $input = clone $input;
370 if (!method_exists($input, 'render')) {
371 throw new ilException(
372 "Method " . get_class($input)
373 . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI"
374 );
375 }
376
377 $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
378 if ($is_disabled_hook !== false && !$clean_render) {
379 $input->setDisabled($is_disabled_hook($this->getValue()));
380 }
381
382 if ($this->getDisabled()) {
383 $input->setDisabled(true);
384 }
385
386 if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
387 $this->post_var_cache[$key] = $input->getPostVar();
388 } else {
389 // Reset post var
390 $input->setPostVar($this->post_var_cache[$key]);
391 }
392
393 $post_var = $this->createInputPostVar($iterator_id, $input);
394 $input->setPostVar($post_var);
395
396 $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
397 if ($before_render_hook !== false && !$clean_render) {
398 $input = $before_render_hook($this->getValue(), $key, $input);
399 }
400
401 //var_dump($input);
402
403 if ($this->isShowLabel()) {
404 $tpl->setCurrentBlock('input_label');
405 $tpl->setVariable('LABEL', $input->getTitle());
406 $tpl->setVariable('CONTENT', $input->render());
407 $tpl->parseCurrentBlock();
408 } else {
409 $tpl->setCurrentBlock('input');
410 $tpl->setVariable('CONTENT', $input->render());
411 $tpl->parseCurrentBlock();
412 }
413 }
414
415 if ($this->getMulti() && !$this->getDisabled()) {
416 $tpl->setVariable('IMAGE_MINUS', ilGlyphGUI::get(ilGlyphGUI::REMOVE));
417
418 $show_remove = true;
419 $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
420 if ($is_removeable_hook !== false && !$clean_render) {
421 $show_remove = $is_removeable_hook($this->getValue());
422 }
423
424 $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
425 $tpl->setCurrentBlock('multi_icons');
426 $tpl->setVariable('IMAGE_PLUS', ilGlyphGUI::get(ilGlyphGUI::ADD));
427 $tpl->setVariable('IMAGE_MINUS', $image_minus);
428 if ($this->multi_sortable) {
429 $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
430 $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
431 }
432 $tpl->parseCurrentBlock();
433 }
434
435 return $tpl->get();
436 }
437
438
444 public function insert(&$a_tpl)
445 {
446 global $DIC;
447 $tpl = $DIC['tpl'];
448
449 $output = "";
450 // $tpl->addCss($this->getTemplateDir() . '/templates/css/multi_line_input.css');
451
452 $output .= $this->render(0, true);
453
454 if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
455 $counter = 0;
456 foreach ($this->line_values as $i => $data) {
457 $object = $this;
458 $object->setValue($data);
459 $output .= $object->render($i);
460 $counter++;
461 }
462 } else {
463 $output .= $this->render(1, true);
464 }
465
466 if ($this->getMulti()) {
467 $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output . '</div>';
468 $tpl->addJavascript('Modules/DataCollection/js/generic_multi_line_input.js');
469 $output .= '<script type="text/javascript">$("#' . $this->getFieldId() . '").multi_line_input('
470 . json_encode($this->input_options) . ', '
471 . json_encode(array('limit' => $this->limit, 'sortable' => $this->multi_sortable, 'locale' => $DIC->language()->getLangKey()))
472 . ')</script>';
473 }
474
475 $a_tpl->setCurrentBlock("prop_generic");
476 $a_tpl->setVariable("PROP_GENERIC", $output);
477 $a_tpl->parseCurrentBlock();
478 }
479
480
484 public function getTableFilterHTML()
485 {
486 $html = $this->render();
487
488 return $html;
489 }
490
491
495 public function getToolbarHTML()
496 {
497 $html = $this->render("toolbar");
498
499 return $html;
500 }
501
502
503 // /**
504 // * @param bool|false $a_sortable
505 // *
506 // * @return string
507 // */
508 // public function getMultiIconsHTML($a_sortable = false) {
509 //
510 // $id = $this->getFieldId();
511 //
512 // if (file_exists(ilUtil::getImagePath('edit_add.png'))) {
513 // $html = '<a href="#" style="display: inline-block;" class="add_button"><img src="' . ilUtil::getImagePath('edit_add.png') . '" /></a>';
514 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><img src="' . ilUtil::getImagePath('edit_remove.png')
515 // . '" /></a>';
516 // } else {
517 // $html = '<a href="#" style="display: inline-block;" class="add_button"><span class="sr-only"></span><span class="glyphicon glyphicon-plus"></span></a>';
518 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><span class="sr-only"></span><span class="glyphicon glyphicon-minus"></span></a>';
519 // }
520 //
521 // /*if($a_sortable)
522 // {
523 // $html .= '&nbsp;<input align="absmiddle" type="image" id="ilMultiDwn~'.$id.'~0"'.
524 // ' src="'.ilUtil::getImagePath('icon_down_s.png').'" alt="'.
525 // $lng->txt("down").'" title="'.$lng->txt("down").'" onclick="javascript: return false;" />'.
526 // '<input align="absmiddle" type="image" id="ilMultiUp~'.$id.'~0"'.
527 // ' src="'.ilUtil::getImagePath('icon_up_s.png').'" alt="'.$lng->txt("up").
528 // '" title="'.$lng->txt("up").'" onclick="javascript: return false;" />';
529 // }*/
530 //
531 // return $html;
532 // }
533
534 public function getSubItems()
535 {
536 return array();
537 }
538}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
This class represents a checkbox property in a property form.
This class represents a date/time property in a property form.
Class for single dates.
Class ilDclGenericMultiInputGUI.
setLimit($limit)
set a limit of possible lines, 0 = no limit
createInputPostVar($iterator_id, ilFormPropertyGUI $input)
insert(&$a_tpl)
Insert property html.
addInput(ilFormPropertyGUI $input, $options=array())
addCustomAttribute($key, $value, $override=false)
checkInput()
Check input, strip slashes etc.
setValueByArray($a_values)
Set value by array.
render($iterator_id=0, $clean_render=false)
Render item.
getTableFilterHTML()
Get HTML for table filter.
__construct($a_title="", $a_postvar="")
Constructor.
setMulti($a_multi, $a_sortable=false, $a_addremove=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property in a property form.
setType($a_type)
Set Type.
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
setRequired($a_required)
Set Required.
getFieldId()
Get Post Variable.
static get($a_glyph, $a_text="")
Get glyph html.
special template class to simplify handling of ITX/PEAR
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
$valid
global $DIC
Definition: goto.php:24
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$i
Definition: metadata.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$data
Definition: storeScorm.php:23