ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilOrgUnitGenericMultiInputGUI.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 $show_label_once = false;
58 protected $hidden_inputs = array();
62 protected $position_movable = false;
66 protected $counter = 0;
70 protected $show_info = false;
75
76
83 public function __construct($a_title = "", $a_postvar = "")
84 {
85 parent::__construct($a_title, $a_postvar);
86 $this->setType("line_select");
87 $this->setMulti(true);
88 $this->initCSSandJS();
89 }
90
91
95 public function getHook($key)
96 {
97 if (isset($this->hooks[$key])) {
98 return $this->hooks[$key];
99 }
100
101 return false;
102 }
103
104
108 public function addHook($key, $options)
109 {
110 $this->hooks[$key] = $options;
111 }
112
113
119 public function removeHook($key)
120 {
121 if (isset($this->hooks[$key])) {
122 unset($this->hooks[$key]);
123
124 return true;
125 }
126
127 return false;
128 }
129
130
135 public function addInput(\ilFormPropertyGUI $input, $options = array())
136 {
137 $this->inputs[$input->getPostVar()] = $input;
138 $this->input_options[$input->getPostVar()] = $options;
139 $this->counter++;
140 }
141
142
146 public function getTemplateDir()
147 {
148 return $this->template_dir;
149 }
150
151
156 {
157 $this->template_dir = $template_dir;
158 }
159
160
164 public function isShowLabel()
165 {
166 return $this->show_label;
167 }
168
169
173 public function setShowLabel($show_label)
174 {
175 $this->show_label = $show_label;
176 }
177
178
184 public function getInputs()
185 {
186 return $this->inputs;
187 }
188
189
193 public function setMulti($a_multi, $a_sortable = false, $a_addremove = true)
194 {
195 $this->multi = $a_multi;
196 }
197
198
204 public function setValue($a_value)
205 {
206 foreach ($this->inputs as $key => $item) {
207 if (method_exists($item, 'setValue')) {
208 $item->setValue($a_value[$key]);
209 } elseif ($item instanceof \ilDateTimeInputGUI) {
210 $item->setDate(new \ilDate($a_value[$key]['date'], IL_CAL_DATE));
211 }
212 }
213 $this->value = $a_value;
214 }
215
216
222 public function getValue()
223 {
224 $out = array();
225 foreach ($this->inputs as $key => $item) {
226 $out[$key] = $item->getValue();
227 }
228
229 return $out;
230 }
231
232
238 public function setValueByArray($a_values)
239 {
240 $data = $a_values[$this->getPostVar()];
241 if ($this->getMulti()) {
242 $this->line_values = $data;
243 } else {
244 $this->setValue($data);
245 }
246 }
247
248
254 public function checkInput()
255 {
256 global $lng;
257 $valid = true;
258 // escape data
259 $out_array = array();
260 if (is_array($_POST[$this->getPostVar()])) {
261 foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
262 foreach ($this->inputs as $input_key => $input) {
263 if (isset($item[$input_key])) {
264 $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? \ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
265 }
266 }
267 }
268 }
269 $_POST[$this->getPostVar()] = $out_array;
270 if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
271 $valid = false;
272 }
273 // validate
274
275 foreach ($this->inputs as $input_key => $inputs) {
276 foreach ($out_array as $subitem) {
277 $_POST[$inputs->getPostVar()] = $subitem[$inputs->getPostVar()];
278 if (!$inputs->checkInput()) {
279 $valid = false;
280 }
281 }
282 }
283 if (!$valid) {
284 $this->setAlert($lng->txt("msg_input_is_required"));
285
286 return false;
287 }
288
289 return $valid;
290 }
291
292
298 public function addCustomAttribute($key, $value, $override = false)
299 {
300 if (isset($this->cust_attr[$key]) && !$override) {
301 $this->cust_attr[$key] .= ' ' . $value;
302 } else {
303 $this->cust_attr[$key] = $value;
304 }
305 }
306
307
311 public function getCustomAttributes()
312 {
313 return (array) $this->cust_attr;
314 }
315
316
323 protected function createInputPostVar($iterator_id, \ilFormPropertyGUI $input)
324 {
325 if ($this->getMulti()) {
326 return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
327 } else {
328 return $this->getPostVar() . '[' . $input->getPostVar() . ']';
329 }
330 }
331
332
341 public function render($iterator_id = 0, $clean_render = false)
342 {
343 $first_label = true;
344 // $tpl = new \ilTemplate("tpl.multi_line_input.html", true, true, 'Customizing/global/plugins/Services/Repository/RepositoryObject/LiveVoting');
345 $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/OrgUnit');
346
347 $class = 'multi_input_line';
348 $this->addCustomAttribute('class', $class, true);
349 foreach ($this->getCustomAttributes() as $key => $value) {
350 $tpl->setCurrentBlock('cust_attr');
351 $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
352 $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
353 $tpl->parseCurrentBlock();
354 }
356 foreach ($inputs as $key => $input) {
357 $input = clone $input;
358 $is_hidden = false;
359 $is_ta = false;
360 if (!method_exists($input, 'render')) {
361 switch (true) {
362 case ($input instanceof \ilHiddenInputGUI):
363 $is_hidden = true;
364 break;
365 case ($input instanceof \ilTextAreaInputGUI):
366 $is_ta = true;
367 break;
368 default:
369 throw new \ilException("Method " . get_class($input)
370 . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
371 }
372 }
373
374 $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
375 if ($is_disabled_hook !== false && !$clean_render) {
376 $input->setDisabled($is_disabled_hook($this->getValue()));
377 }
378 if ($this->getDisabled()) {
379 $input->setDisabled(true);
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 $post_var = $this->createInputPostVar($iterator_id, $input);
388 $input->setPostVar($post_var);
389 $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
390 if ($before_render_hook !== false && !$clean_render) {
391 $input = $before_render_hook($this->getValue(), $key, $input);
392 }
393 switch (true) {
394 case $is_hidden:
395 $tpl->setCurrentBlock('hidden');
396 $tpl->setVariable('NAME', $post_var);
397 $tpl->setVariable('VALUE', \ilUtil::prepareFormOutput($input->getValue()));
398 break;
399 case $is_ta:
400 if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
401 $tpl->setCurrentBlock('input_label');
402 $tpl->setVariable('LABEL', $input->getTitle());
403 $tpl->setVariable('CONTENT', $input->getHTML());
404 $tpl->parseCurrentBlock();
405 $first_label = false;
406 } else {
407 $tpl->setCurrentBlock('input');
408 $tpl->setVariable('CONTENT', $input->getHTML());
409 }
410 break;
411 default:
412 if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
413 $tpl->setCurrentBlock('input_label');
414 $tpl->setVariable('LABEL', $input->getTitle());
415 $tpl->setVariable('CONTENT', $input->render());
416 $first_label = false;
417 } else {
418 $tpl->setCurrentBlock('input');
419 $tpl->setVariable('CONTENT', $input->render());
420 }
421 break;
422 }
423 if ($this->isShowInfo()) {
424 if ($this->isShowLabel()) {
425 $tpl->setCurrentBlock('input_info_label');
426 $tpl->setVariable('INFO_LABEL', $input->getInfo());
427 $tpl->parseCurrentBlock();
428 } else {
429 $tpl->setCurrentBlock('input_info');
430 $tpl->setVariable('INFO', $input->getInfo());
431 $tpl->parseCurrentBlock();
432 }
433 }
434 $tpl->parseCurrentBlock();
435 }
436 if ($this->getMulti() && !$this->getDisabled()) {
437 $image_plus = ilGlyphGUI::get(ilGlyphGUI::ADD);
438 $show_remove = true;
439 $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
440 if ($is_removeable_hook !== false && !$clean_render) {
441 $show_remove = $is_removeable_hook($this->getValue());
442 }
443 $show_remove = true;
444 $image_minus = ($show_remove) ? ilGlyphGUI::get(ilGlyphGUI::REMOVE) : '<span class="glyphicon glyphicon-minus hide"></span>';
445 $tpl->setCurrentBlock('multi_icons');
446 $tpl->setVariable('IMAGE_PLUS', $image_plus);
447 $tpl->setVariable('IMAGE_MINUS', $image_minus);
448 $tpl->parseCurrentBlock();
449 if ($this->isPositionMovable()) {
450 $tpl->setCurrentBlock('multi_icons_move');
451 $tpl->setVariable('IMAGE_UP', ilGlyphGUI::get(ilGlyphGUI::UP));
452 $tpl->setVariable('IMAGE_DOWN', ilGlyphGUI::get(ilGlyphGUI::DOWN));
453 $tpl->parseCurrentBlock();
454 }
455 }
456
457 return $tpl->get();
458 }
459
460
461 public function initCSSandJS()
462 {
463 global $tpl;
464 $tpl->addJavascript('Modules/OrgUnit/js/generic_multi_line_input.js');
465 }
466
467
473 public function insert(&$a_tpl)
474 {
475 $output = "";
476
477 $output .= $this->render(0, true);
478 if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
479 foreach ($this->line_values as $run => $data) {
480 $object = $this;
481 $object->setValue($data);
482 $output .= $object->render($run);
483 }
484 } else {
485 if ($this->render_one_for_empty_value) {
486 $output .= $this->render(0, true);
487 } else {
488 $tpl = new ilTemplate("tpl.prop_generic_multi_line.html", true, true, 'Modules/OrgUnit');
489 $image_plus = ilGlyphGUI::get(ilGlyphGUI::ADD);
490 $image_minus = '<span class="glyphicon glyphicon-minus hide"></span>';
491
492 $tpl->setVariable('ADDITIONAL_ATTRS', "style='float:left';");
493 $tpl->setCurrentBlock('multi_icons');
494 $tpl->setVariable('IMAGE_PLUS', $image_plus);
495 $tpl->setVariable('IMAGE_MINUS', $image_minus);
496 $tpl->parseCurrentBlock();
497 $output .= $tpl->get();
498 }
499 }
500 if ($this->getMulti()) {
501 $output = "<div id='{$this->getFieldId()}' class='multi_line_input'>{$output}</div>";
502
503 global $tpl;
504 $options = json_encode($this->input_options);
505 $tpl->addOnLoadCode("$('#{$this->getFieldId()}').multi_line_input({$this->getFieldId()}, '{$options}')");
506 }
507
508 $a_tpl->setCurrentBlock("prop_generic");
509 $a_tpl->setVariable("PROP_GENERIC", $output);
510 $a_tpl->parseCurrentBlock();
511 }
512
513
517 public function getTableFilterHTML()
518 {
519 $html = $this->render();
520
521 return $html;
522 }
523
524
528 public function getToolbarHTML()
529 {
530 $html = $this->render("toolbar");
531
532 return $html;
533 }
534
535
539 public function isPositionMovable()
540 {
542 }
543
544
549 {
550 $this->position_movable = $position_movable;
551 }
552
553
557 public function isShowLabelOnce()
558 {
560 }
561
562
567 {
568 $this->setShowLabel(false);
569 $this->show_label_once = $show_label_once;
570 }
571
572
576 public function isShowInfo()
577 {
578 return $this->show_info;
579 }
580
581
585 public function setShowInfo($show_info)
586 {
587 $this->show_info = $show_info;
588 }
589
590
594 public function isRenderOneForEmptyValue()
595 {
597 }
598
599
604 {
605 $this->render_one_for_empty_value = $render_one_for_empty_value;
606 }
607}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
This class represents a date/time property in a property form.
Class for single dates.
This class represents a property in a property form.
setType($a_type)
Set Type.
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
static get($a_glyph, $a_text="")
Get glyph html.
This class represents a hidden form property in a property form.
addCustomAttribute($key, $value, $override=false)
addInput(\ilFormPropertyGUI $input, $options=array())
setRenderOneForEmptyValue($render_one_for_empty_value)
render($iterator_id=0, $clean_render=false)
Render item.
__construct($a_title="", $a_postvar="")
Constructor.
setMulti($a_multi, $a_sortable=false, $a_addremove=true)
createInputPostVar($iterator_id, \ilFormPropertyGUI $input)
special template class to simplify handling of ITX/PEAR
This class represents a text area property in a property form.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$valid
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$data
Definition: storeScorm.php:23