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