ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDclGenericMultiInputGUI.php
Go to the documentation of this file.
1<?php
2require_once("./Services/Form/classes/class.ilFormPropertyGUI.php");
3
4
12{
13 const HOOK_IS_LINE_REMOVABLE = "hook_is_line_removable";
14 const HOOK_IS_INPUT_DISABLED = "hook_is_disabled";
15 const HOOK_BEFORE_INPUT_RENDER = "hook_before_render";
19 protected $cust_attr = array();
23 protected $value;
27 protected $inputs = array();
31 protected $input_options = array();
35 protected $hooks = array();
39 protected $line_values = array();
43 protected $template_dir = '';
47 protected $post_var_cache = array();
51 protected $show_label = false;
55 protected $limit = 0;
59 protected $allow_empty_fields = false;
60
61
65 public function getLimit()
66 {
67 return $this->limit;
68 }
69
70
76 public function setLimit($limit)
77 {
78 $this->limit = $limit;
79 }
80
81
85 public function isAllowEmptyFields()
86 {
88 }
89
90
95 {
96 $this->allow_empty_fields = $allow_empty_fields;
97 }
98
99
106 public function __construct($a_title = "", $a_postvar = "")
107 {
108 parent::__construct($a_title, $a_postvar);
109 $this->setType("line_select");
110 $this->setMulti(true);
111 }
112
113
117 public function getHook($key)
118 {
119 if (isset($this->hooks[$key])) {
120 return $this->hooks[$key];
121 }
122
123 return false;
124 }
125
126
130 public function addHook($key, $options)
131 {
132 $this->hooks[$key] = $options;
133 }
134
135
141 public function removeHook($key)
142 {
143 if (isset($this->hooks[$key])) {
144 unset($this->hooks[$key]);
145
146 return true;
147 }
148
149 return false;
150 }
151
152
157 public function addInput(ilFormPropertyGUI $input, $options = array())
158 {
159 $input->setRequired(!$this->allow_empty_fields);
160 $this->inputs[$input->getPostVar()] = $input;
161 $this->input_options[$input->getPostVar()] = $options;
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 } elseif ($item instanceof ilDateTimeInputGUI) {
214 $item->setDate(new ilDate($a_value[$key], IL_CAL_DATE));
215 } elseif (method_exists($item, 'setValue')) {
216 $item->setValue($a_value[$key]);
217 }
218 }
219 $this->value = $a_value;
220 }
221
222
228 public function getValue()
229 {
230 $out = array();
231 foreach ($this->inputs as $key => $item) {
232 $out[$key] = $item->getValue();
233 }
234
235 return $out;
236 }
237
238
244 public function setValueByArray($a_values)
245 {
246 $data = $a_values[$this->getPostVar()];
247 if ($this->getMulti()) {
248 $this->line_values = $data;
249 } else {
250 $this->setValue($data);
251 }
252 }
253
254
260 public function checkInput()
261 {
262 global $lng;
263
264 $valid = true;
265
266 // escape data
267 $out_array = array();
268 foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
269 foreach ($this->inputs as $input_key => $input) {
270 if (isset($item[$input_key])) {
271 $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
272 }
273 }
274 }
275 $_POST[$this->getPostVar()] = $out_array;
276
277 if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
278 $valid = false;
279 }
280
281 // validate
282 foreach ($this->inputs as $input_key => $inputs) {
283 foreach ($out_array as $subitem) {
284 $_POST[$inputs->getPostVar()] = $subitem[$inputs->getPostVar()];
285 if (!$inputs->checkInput()) {
286 $valid = false;
287 }
288 }
289 }
290
291 if (!$valid) {
292 $this->setAlert($lng->txt("msg_input_is_required"));
293
294 return false;
295 }
296
297 return $valid;
298 }
299
300
306 public function addCustomAttribute($key, $value, $override = false)
307 {
308 if (isset($this->cust_attr[$key]) && !$override) {
309 $this->cust_attr[$key] .= ' ' . $value;
310 } else {
311 $this->cust_attr[$key] = $value;
312 }
313 }
314
315
319 public function getCustomAttributes()
320 {
321 return (array) $this->cust_attr;
322 }
323
324
331 protected function createInputPostVar($iterator_id, ilFormPropertyGUI $input)
332 {
333 if ($this->getMulti()) {
334 return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
335 } else {
336 return $this->getPostVar() . '[' . $input->getPostVar() . ']';
337 }
338 }
339
340
349 public function render($iterator_id = 0, $clean_render = false)
350 {
351 $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/DataCollection');
352
353 $class = 'multi_input_line';
354
355 $this->addCustomAttribute('class', $class, true);
356 foreach ($this->getCustomAttributes() as $key => $value) {
357 $tpl->setCurrentBlock('cust_attr');
358 $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
359 $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
360 $tpl->parseCurrentBlock();
361 }
362
364
365 foreach ($inputs as $key => $input) {
366 $input = clone $input;
367 if (!method_exists($input, 'render')) {
368 throw new ilException("Method " . get_class($input)
369 . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
370 }
371
372 $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
373 if ($is_disabled_hook !== false && !$clean_render) {
374 $input->setDisabled($is_disabled_hook($this->getValue()));
375 }
376
377 if ($this->getDisabled()) {
378 $input->setDisabled(true);
379 }
380
381 if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
382 $this->post_var_cache[$key] = $input->getPostVar();
383 } else {
384 // Reset post var
385 $input->setPostVar($this->post_var_cache[$key]);
386 }
387
388 $post_var = $this->createInputPostVar($iterator_id, $input);
389 $input->setPostVar($post_var);
390
391 $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
392 if ($before_render_hook !== false && !$clean_render) {
393 $input = $before_render_hook($this->getValue(), $key, $input);
394 }
395
396 //var_dump($input);
397
398 if ($this->isShowLabel()) {
399 $tpl->setCurrentBlock('input_label');
400 $tpl->setVariable('LABEL', $input->getTitle());
401 $tpl->setVariable('CONTENT', $input->render());
402 $tpl->parseCurrentBlock();
403 } else {
404 $tpl->setCurrentBlock('input');
405 $tpl->setVariable('CONTENT', $input->render());
406 $tpl->parseCurrentBlock();
407 }
408 }
409
410 if ($this->getMulti() && !$this->getDisabled()) {
411 $tpl->setVariable('IMAGE_MINUS', ilGlyphGUI::get(ilGlyphGUI::REMOVE));
412
413 $show_remove = true;
414 $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
415 if ($is_removeable_hook !== false && !$clean_render) {
416 $show_remove = $is_removeable_hook($this->getValue());
417 }
418
419 $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
420 $tpl->setCurrentBlock('multi_icons');
421 $tpl->setVariable('IMAGE_PLUS', ilGlyphGUI::get(ilGlyphGUI::ADD));
422 $tpl->setVariable('IMAGE_MINUS', $image_minus);
423 if ($this->multi_sortable) {
424 $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
425 $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
426 }
427 $tpl->parseCurrentBlock();
428 }
429
430 return $tpl->get();
431 }
432
433
439 public function insert(&$a_tpl)
440 {
441 global $DIC;
442 $tpl = $DIC['tpl'];
443
444 $output = "";
445 // $tpl->addCss($this->getTemplateDir() . '/templates/css/multi_line_input.css');
446
447 $output .= $this->render(0, true);
448
449 if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
450 $counter = 0;
451 foreach ($this->line_values as $i => $data) {
452 $object = $this;
453 $object->setValue($data);
454 $output .= $object->render($i);
455 $counter++;
456 }
457 } else {
458 $output .= $this->render(1, true);
459 }
460
461 if ($this->getMulti()) {
462 $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output . '</div>';
463 $tpl->addJavascript('Modules/DataCollection/js/generic_multi_line_input.js');
464 $output .= '<script type="text/javascript">$("#' . $this->getFieldId() . '").multi_line_input('
465 . json_encode($this->input_options) . ', '
466 . json_encode(array('limit' => $this->limit, 'sortable' => $this->multi_sortable, 'locale' => $DIC->language()->getLangKey()))
467 . ')</script>';
468 }
469
470 $a_tpl->setCurrentBlock("prop_generic");
471 $a_tpl->setVariable("PROP_GENERIC", $output);
472 $a_tpl->parseCurrentBlock();
473 }
474
475
479 public function getTableFilterHTML()
480 {
481 $html = $this->render();
482
483 return $html;
484 }
485
486
490 public function getToolbarHTML()
491 {
492 $html = $this->render("toolbar");
493
494 return $html;
495 }
496
497
498 // /**
499 // * @param bool|false $a_sortable
500 // *
501 // * @return string
502 // */
503 // public function getMultiIconsHTML($a_sortable = false) {
504//
505 // $id = $this->getFieldId();
506//
507 // if (file_exists(ilUtil::getImagePath('edit_add.png'))) {
508 // $html = '<a href="#" style="display: inline-block;" class="add_button"><img src="' . ilUtil::getImagePath('edit_add.png') . '" /></a>';
509 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><img src="' . ilUtil::getImagePath('edit_remove.png')
510 // . '" /></a>';
511 // } else {
512 // $html = '<a href="#" style="display: inline-block;" class="add_button"><span class="sr-only"></span><span class="glyphicon glyphicon-plus"></span></a>';
513 // $html .= '<a href="#" style="display: inline-block;" class="remove_button"><span class="sr-only"></span><span class="glyphicon glyphicon-minus"></span></a>';
514 // }
515//
516 // /*if($a_sortable)
517 // {
518 // $html .= '&nbsp;<input align="absmiddle" type="image" id="ilMultiDwn~'.$id.'~0"'.
519 // ' src="'.ilUtil::getImagePath('icon_down_s.png').'" alt="'.
520 // $lng->txt("down").'" title="'.$lng->txt("down").'" onclick="javascript: return false;" />'.
521 // '<input align="absmiddle" type="image" id="ilMultiUp~'.$id.'~0"'.
522 // ' src="'.ilUtil::getImagePath('icon_up_s.png').'" alt="'.$lng->txt("up").
523 // '" title="'.$lng->txt("up").'" onclick="javascript: return false;" />';
524 // }*/
525//
526 // return $html;
527 // }
528
529 public function getSubItems()
530 {
531 return array();
532 }
533}
$tpl
Definition: ilias.php:10
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
$_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.
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
$counter
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
$valid
$html
Definition: example_001.php:87
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\s+" &#(? foreach( $entity_files as $file) $output
global $DIC
Definition: saml.php:7