ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDclGenericMultiInputGUI.php
Go to the documentation of this file.
1<?php
2require_once("./Services/Form/classes/class.ilFormPropertyGUI.php");
3
11{
12 const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
13 const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
14 const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
18 protected $cust_attr = array();
22 protected $value;
26 protected $inputs = array();
30 protected $input_options = array();
34 protected $hooks = array();
38 protected $line_values = array();
42 protected $template_dir = '';
46 protected $post_var_cache = array();
50 protected $show_label = false;
54 protected $limit = 0;
58 protected $allow_empty_fields = false;
59
60
64 public function getLimit()
65 {
66 return $this->limit;
67 }
68
69
75 public function setLimit($limit)
76 {
77 $this->limit = $limit;
78 }
79
80
84 public function isAllowEmptyFields()
85 {
87 }
88
89
94 {
95 $this->allow_empty_fields = $allow_empty_fields;
96 }
97
98
105 public function __construct($a_title = "", $a_postvar = "")
106 {
107 parent::__construct($a_title, $a_postvar);
108 $this->setType("line_select");
109 $this->setMulti(true);
110 }
111
112
116 public function getHook($key)
117 {
118 if (isset($this->hooks[$key])) {
119 return $this->hooks[$key];
120 }
121
122 return false;
123 }
124
125
129 public function addHook($key, $options)
130 {
131 $this->hooks[$key] = $options;
132 }
133
134
140 public function removeHook($key)
141 {
142 if (isset($this->hooks[$key])) {
143 unset($this->hooks[$key]);
144
145 return true;
146 }
147
148 return false;
149 }
150
151
156 public function addInput(ilFormPropertyGUI $input, $options = array())
157 {
158 $input->setRequired(!$this->allow_empty_fields);
159 $this->inputs[$input->getPostVar()] = $input;
160 $this->input_options[$input->getPostVar()] = $options;
161 }
162
163
167 public function isShowLabel()
168 {
169 return $this->show_label;
170 }
171
172
176 public function setShowLabel($show_label)
177 {
178 $this->show_label = $show_label;
179 }
180
181
187 public function getInputs()
188 {
189 return $this->inputs;
190 }
191
192
196 public function setMulti($a_multi, $a_sortable = false, $a_addremove = true)
197 {
198 $this->multi = $a_multi;
199 $this->multi_sortable = $a_sortable;
200 }
201
202
208 public function setValue($a_value)
209 {
210 foreach ($this->inputs as $key => $item) {
211 if ($item instanceof ilCheckboxInputGUI) {
212 $item->setChecked((bool) $a_value[$key]);
213 } else {
214 if ($item instanceof ilDateTimeInputGUI) {
215 $item->setDate(new ilDate($a_value[$key], IL_CAL_DATE));
216 } else {
217 if (method_exists($item, 'setValue')) {
218 $item->setValue($a_value[$key]);
219 }
220 }
221 }
222 }
223 $this->value = $a_value;
224 }
225
226
232 public function getValue()
233 {
234 $out = array();
235 foreach ($this->inputs as $key => $item) {
236 $out[$key] = $item->getValue();
237 }
238
239 return $out;
240 }
241
242
248 public function setValueByArray($a_values)
249 {
250 $data = $a_values[$this->getPostVar()];
251 if ($this->getMulti()) {
252 $this->line_values = $data;
253 } else {
254 $this->setValue($data);
255 }
256 }
257
258
264 public function checkInput()
265 {
266 global $lng;
267
268 $valid = true;
269
270 // escape data
271 $out_array = array();
272 foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
273 foreach ($this->inputs as $input_key => $input) {
274 if (isset($item[$input_key])) {
275 $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
276 }
277 }
278 }
279 $_POST[$this->getPostVar()] = $out_array;
280
281 if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
282 $valid = false;
283 }
284
285 // validate
286 foreach ($this->inputs as $input_key => $inputs) {
287 foreach ($out_array as $subitem) {
288 $_POST[$inputs->getPostVar()] = $subitem[$inputs->getPostVar()];
289 if (!$inputs->checkInput()) {
290 $valid = false;
291 }
292 }
293 }
294
295 if (!$valid) {
296 $this->setAlert($lng->txt("msg_input_is_required"));
297
298 return false;
299 }
300
301 return $valid;
302 }
303
304
310 public function addCustomAttribute($key, $value, $override = false)
311 {
312 if (isset($this->cust_attr[$key]) && !$override) {
313 $this->cust_attr[$key] .= ' ' . $value;
314 } else {
315 $this->cust_attr[$key] = $value;
316 }
317 }
318
319
323 public function getCustomAttributes()
324 {
325 return (array) $this->cust_attr;
326 }
327
328
335 protected function createInputPostVar($iterator_id, ilFormPropertyGUI $input)
336 {
337 if ($this->getMulti()) {
338 return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
339 } else {
340 return $this->getPostVar() . '[' . $input->getPostVar() . ']';
341 }
342 }
343
344
353 public function render($iterator_id = 0, $clean_render = false)
354 {
355 $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/DataCollection');
356
357 $class = 'multi_input_line';
358
359 $this->addCustomAttribute('class', $class, true);
360 foreach ($this->getCustomAttributes() as $key => $value) {
361 $tpl->setCurrentBlock('cust_attr');
362 $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
363 $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
364 $tpl->parseCurrentBlock();
365 }
366
368
369 foreach ($inputs as $key => $input) {
370 $input = clone $input;
371 if (!method_exists($input, 'render')) {
372 throw new ilException(
373 "Method " . get_class($input)
374 . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI"
375 );
376 }
377
378 $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
379 if ($is_disabled_hook !== false && !$clean_render) {
380 $input->setDisabled($is_disabled_hook($this->getValue()));
381 }
382
383 if ($this->getDisabled()) {
384 $input->setDisabled(true);
385 }
386
387 if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
388 $this->post_var_cache[$key] = $input->getPostVar();
389 } else {
390 // Reset post var
391 $input->setPostVar($this->post_var_cache[$key]);
392 }
393
394 $post_var = $this->createInputPostVar($iterator_id, $input);
395 $input->setPostVar($post_var);
396
397 $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
398 if ($before_render_hook !== false && !$clean_render) {
399 $input = $before_render_hook($this->getValue(), $key, $input);
400 }
401
402 //var_dump($input);
403
404 if ($this->isShowLabel()) {
405 $tpl->setCurrentBlock('input_label');
406 $tpl->setVariable('LABEL', $input->getTitle());
407 $tpl->setVariable('CONTENT', $input->render());
408 $tpl->parseCurrentBlock();
409 } else {
410 $tpl->setCurrentBlock('input');
411 $tpl->setVariable('CONTENT', $input->render());
412 $tpl->parseCurrentBlock();
413 }
414 }
415
416 if ($this->getMulti() && !$this->getDisabled()) {
417 $tpl->setVariable('IMAGE_MINUS', ilGlyphGUI::get(ilGlyphGUI::REMOVE));
418
419 $show_remove = true;
420 $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
421 if ($is_removeable_hook !== false && !$clean_render) {
422 $show_remove = $is_removeable_hook($this->getValue());
423 }
424
425 $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
426 $tpl->setCurrentBlock('multi_icons');
427 $tpl->setVariable('IMAGE_PLUS', ilGlyphGUI::get(ilGlyphGUI::ADD));
428 $tpl->setVariable('IMAGE_MINUS', $image_minus);
429 if ($this->multi_sortable) {
430 $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
431 $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
432 }
433 $tpl->parseCurrentBlock();
434 }
435
436 return $tpl->get();
437 }
438
439
445 public function insert(&$a_tpl)
446 {
447 global $DIC;
448 $tpl = $DIC['tpl'];
449
450 $output = "";
451 // $tpl->addCss($this->getTemplateDir() . '/templates/css/multi_line_input.css');
452
453 $output .= $this->render(0, true);
454
455 if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
456 $counter = 0;
457 foreach ($this->line_values as $i => $data) {
458 $object = $this;
459 $object->setValue($data);
460 $output .= $object->render($i);
461 $counter++;
462 }
463 } else {
464 $output .= $this->render(1, true);
465 }
466
467 if ($this->getMulti()) {
468 $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output . '</div>';
469 $tpl->addJavascript('Modules/DataCollection/js/generic_multi_line_input.js');
470 $output .= '<script type="text/javascript">$("#' . $this->getFieldId() . '").multi_line_input('
471 . json_encode($this->input_options) . ', '
472 . json_encode(array('limit' => $this->limit, 'sortable' => $this->multi_sortable, 'locale' => $DIC->language()->getLangKey()))
473 . ')</script>';
474 }
475
476 $a_tpl->setCurrentBlock("prop_generic");
477 $a_tpl->setVariable("PROP_GENERIC", $output);
478 $a_tpl->parseCurrentBlock();
479 }
480
481
485 public function getTableFilterHTML()
486 {
487 $html = $this->render();
488
489 return $html;
490 }
491
492
496 public function getToolbarHTML()
497 {
498 $html = $this->render("toolbar");
499
500 return $html;
501 }
502
503
504 // /**
505 // * @param bool|false $a_sortable
506 // *
507 // * @return string
508 // */
509 // public function getMultiIconsHTML($a_sortable = false) {
510 //
511 // $id = $this->getFieldId();
512 //
513 // if (file_exists(ilUtil::getImagePath('edit_add.png'))) {
514 // $html = '<a href="#" style="display: inline-block;" class="add_button"><img src="' . ilUtil::getImagePath('edit_add.png') . '" /></a>';
515 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><img src="' . ilUtil::getImagePath('edit_remove.png')
516 // . '" /></a>';
517 // } else {
518 // $html = '<a href="#" style="display: inline-block;" class="add_button"><span class="sr-only"></span><span class="glyphicon glyphicon-plus"></span></a>';
519 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><span class="sr-only"></span><span class="glyphicon glyphicon-minus"></span></a>';
520 // }
521 //
522 // /*if($a_sortable)
523 // {
524 // $html .= '&nbsp;<input align="absmiddle" type="image" id="ilMultiDwn~'.$id.'~0"'.
525 // ' src="'.ilUtil::getImagePath('icon_down_s.png').'" alt="'.
526 // $lng->txt("down").'" title="'.$lng->txt("down").'" onclick="javascript: return false;" />'.
527 // '<input align="absmiddle" type="image" id="ilMultiUp~'.$id.'~0"'.
528 // ' src="'.ilUtil::getImagePath('icon_up_s.png').'" alt="'.$lng->txt("up").
529 // '" title="'.$lng->txt("up").'" onclick="javascript: return false;" />';
530 // }*/
531 //
532 // return $html;
533 // }
534
535 public function getSubItems()
536 {
537 return array();
538 }
539}
$tpl
Definition: ilias.php:10
$_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)
Base class for ILIAS Exception handling.
This class represents a property in a property form.
setType($a_type)
Set Type.
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
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
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
$valid
$html
Definition: example_001.php:87
global $DIC
Definition: saml.php:7
$data
Definition: bench.php:6