ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilOrgUnitMultiLineInputGUI.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 $show_label_once = false;
57  protected $hidden_inputs = array();
61  protected $position_movable = false;
65  protected $counter = 0;
69  protected $show_info = false;
70 
71 
78  public function __construct($a_title = "", $a_postvar = "")
79  {
80  parent::__construct($a_title, $a_postvar);
81  $this->setType("line_select");
82  $this->setMulti(true);
83  $this->initCSSandJS();
84  }
85 
86 
90  public function getHook($key)
91  {
92  if (isset($this->hooks[$key])) {
93  return $this->hooks[$key];
94  }
95 
96  return false;
97  }
98 
99 
103  public function addHook($key, $options)
104  {
105  $this->hooks[$key] = $options;
106  }
107 
108 
114  public function removeHook($key)
115  {
116  if (isset($this->hooks[$key])) {
117  unset($this->hooks[$key]);
118 
119  return true;
120  }
121 
122  return false;
123  }
124 
125 
130  public function addInput(\ilFormPropertyGUI $input, $options = array())
131  {
132  $this->inputs[$input->getPostVar()] = $input;
133  $this->input_options[$input->getPostVar()] = $options;
134  $this->counter++;
135  }
136 
137 
141  public function getTemplateDir()
142  {
143  return $this->template_dir;
144  }
145 
146 
150  public function setTemplateDir($template_dir)
151  {
152  $this->template_dir = $template_dir;
153  }
154 
155 
159  public function isShowLabel()
160  {
161  return $this->show_label;
162  }
163 
164 
168  public function setShowLabel($show_label)
169  {
170  $this->show_label = $show_label;
171  }
172 
173 
179  public function getInputs()
180  {
181  return $this->inputs;
182  }
183 
184 
188  public function setMulti($a_multi, $a_sortable = false, $a_addremove = true)
189  {
190  $this->multi = $a_multi;
191  }
192 
193 
199  public function setValue($a_value)
200  {
201  foreach ($this->inputs as $key => $item) {
202  if (method_exists($item, 'setValue')) {
203  $item->setValue($a_value[$key]);
204  } elseif ($item instanceof \ilDateTimeInputGUI) {
205  $item->setDate(new \ilDate($a_value[$key]['date'], IL_CAL_DATE));
206  }
207  }
208  $this->value = $a_value;
209  }
210 
211 
217  public function getValue()
218  {
219  $out = array();
220  foreach ($this->inputs as $key => $item) {
221  $out[$key] = $item->getValue();
222  }
223 
224  return $out;
225  }
226 
227 
233  public function setValueByArray($a_values)
234  {
235  $data = $a_values[$this->getPostVar()];
236  if ($this->getMulti()) {
237  $this->line_values = $data;
238  } else {
239  $this->setValue($data);
240  }
241  }
242 
243 
249  public function checkInput()
250  {
251  global $lng;
252  $valid = true;
253  // escape data
254  $out_array = array();
255  foreach ($_POST[$this->getPostVar()] as $item_num => $item) {
256  foreach ($this->inputs as $input_key => $input) {
257  if (isset($item[$input_key])) {
258  $out_array[$item_num][$input_key] = (is_string($item[$input_key])) ? \ilUtil::stripSlashes($item[$input_key]) : $item[$input_key];
259  }
260  }
261  }
262  $_POST[$this->getPostVar()] = $out_array;
263  if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
264  $valid = false;
265  }
266  // validate
267  foreach ($this->inputs as $input_key => $inputs) {
268  if (!$inputs->checkInput()) {
269  $valid = false;
270  }
271  }
272  if (!$valid) {
273  $this->setAlert($lng->txt("msg_input_is_required"));
274 
275  return false;
276  }
277 
278  return $valid;
279  }
280 
281 
287  public function addCustomAttribute($key, $value, $override = false)
288  {
289  if (isset($this->cust_attr[$key]) && !$override) {
290  $this->cust_attr[$key] .= ' ' . $value;
291  } else {
292  $this->cust_attr[$key] = $value;
293  }
294  }
295 
296 
300  public function getCustomAttributes()
301  {
302  return (array) $this->cust_attr;
303  }
304 
305 
312  protected function createInputPostVar($iterator_id, \ilFormPropertyGUI $input)
313  {
314  if ($this->getMulti()) {
315  return $this->getPostVar() . '[' . $iterator_id . '][' . $input->getPostVar() . ']';
316  } else {
317  return $this->getPostVar() . '[' . $input->getPostVar() . ']';
318  }
319  }
320 
321 
330  public function render($iterator_id = 0, $clean_render = false)
331  {
332  $first_label = true;
333  $tpl = new \ilTemplate("tpl.multi_line_input.html", true, true, 'Customizing/global/plugins/Services/Repository/RepositoryObject/LiveVoting');
334  $class = 'multi_input_line';
335  $this->addCustomAttribute('class', $class, true);
336  foreach ($this->getCustomAttributes() as $key => $value) {
337  $tpl->setCurrentBlock('cust_attr');
338  $tpl->setVariable('CUSTOM_ATTR_KEY', $key);
339  $tpl->setVariable('CUSTOM_ATTR_VALUE', $value);
340  $tpl->parseCurrentBlock();
341  }
343  foreach ($inputs as $key => $input) {
344  $input = clone $input;
345  $is_hidden = false;
346  $is_ta = false;
347  if (!method_exists($input, 'render')) {
348  switch (true) {
349  case ($input instanceof \ilHiddenInputGUI):
350  $is_hidden = true;
351  break;
352  case ($input instanceof \ilTextAreaInputGUI):
353  $is_ta = true;
354  break;
355  default:
356  throw new \ilException("Method " . get_class($input)
357  . "::render() does not exists! You cannot use this input-type in ilMultiLineInputGUI");
358  }
359  }
360 
361  $is_disabled_hook = $this->getHook(self::HOOK_IS_INPUT_DISABLED);
362  if ($is_disabled_hook !== false && !$clean_render) {
363  $input->setDisabled($is_disabled_hook($this->getValue()));
364  }
365  if ($this->getDisabled()) {
366  $input->setDisabled(true);
367  }
368  if ($iterator_id == 0 && !isset($this->post_var_cache[$key])) {
369  $this->post_var_cache[$key] = $input->getPostVar();
370  } else {
371  // Reset post var
372  $input->setPostVar($this->post_var_cache[$key]);
373  }
374  $post_var = $this->createInputPostVar($iterator_id, $input);
375  $input->setPostVar($post_var);
376  $before_render_hook = $this->getHook(self::HOOK_BEFORE_INPUT_RENDER);
377  if ($before_render_hook !== false && !$clean_render) {
378  $input = $before_render_hook($this->getValue(), $key, $input);
379  }
380  switch (true) {
381  case $is_hidden:
382  $tpl->setCurrentBlock('hidden');
383  $tpl->setVariable('NAME', $post_var);
384  $tpl->setVariable('VALUE', \ilUtil::prepareFormOutput($input->getValue()));
385  break;
386  case $is_ta:
387  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
388  $tpl->setCurrentBlock('input_label');
389  $tpl->setVariable('LABEL', $input->getTitle());
390  $tpl->setVariable('CONTENT', $input->getHTML());
391  $tpl->parseCurrentBlock();
392  $first_label = false;
393  } else {
394  $tpl->setCurrentBlock('input');
395  $tpl->setVariable('CONTENT', $input->getHTML());
396  }
397  break;
398  default:
399  if ($this->isShowLabel() || ($this->isShowLabelOnce() && $first_label)) {
400  $tpl->setCurrentBlock('input_label');
401  $tpl->setVariable('LABEL', $input->getTitle());
402  $tpl->setVariable('CONTENT', $input->render());
403  $first_label = false;
404  } else {
405  $tpl->setCurrentBlock('input');
406  $tpl->setVariable('CONTENT', $input->render());
407  }
408  break;
409  }
410  if ($this->isShowInfo()) {
411  if ($this->isShowLabel()) {
412  $tpl->setCurrentBlock('input_info_label');
413  $tpl->setVariable('INFO_LABEL', $input->getInfo());
414  $tpl->parseCurrentBlock();
415  } else {
416  $tpl->setCurrentBlock('input_info');
417  $tpl->setVariable('INFO', $input->getInfo());
418  $tpl->parseCurrentBlock();
419  }
420  }
421  $tpl->parseCurrentBlock();
422  }
423  if ($this->getMulti() && !$this->getDisabled()) {
424  $image_plus = xlvoGlyphGUI::get('plus');
425  $show_remove = true;
426  $is_removeable_hook = $this->getHook(self::HOOK_IS_LINE_REMOVABLE);
427  if ($is_removeable_hook !== false && !$clean_render) {
428  $show_remove = $is_removeable_hook($this->getValue());
429  }
430  $show_remove = true;
431  $image_minus = ($show_remove) ? xlvoGlyphGUI::get('minus') : '<span class="glyphicon glyphicon-minus hide"></span>';
432  $tpl->setCurrentBlock('multi_icons');
433  $tpl->setVariable('IMAGE_PLUS', $image_plus);
434  $tpl->setVariable('IMAGE_MINUS', $image_minus);
435  $tpl->parseCurrentBlock();
436  if ($this->isPositionMovable()) {
437  $tpl->setCurrentBlock('multi_icons_move');
438  $tpl->setVariable('IMAGE_UP', xlvoGlyphGUI::get(xlvoGlyphGUI::UP));
439  $tpl->setVariable('IMAGE_DOWN', xlvoGlyphGUI::get(xlvoGlyphGUI::DOWN));
440  $tpl->parseCurrentBlock();
441  }
442  }
443 
444  return $tpl->get();
445  }
446 
447 
448  public function initCSSandJS()
449  {
450  global $tpl;
451  $tpl->addJavascript('./Modules/OrgUnit/templates/default/multi_line_input.js');
452  }
453 
454 
460  public function insert(&$a_tpl)
461  {
462  $output = "";
463 
464  $output .= $this->render(0, true);
465  if ($this->getMulti() && is_array($this->line_values) && count($this->line_values) > 0) {
466  foreach ($this->line_values as $run => $data) {
467  $object = $this;
468  $object->setValue($data);
469  $output .= $object->render($run);
470  }
471  } else {
472  $output .= $this->render(0, true);
473  }
474  if ($this->getMulti()) {
475  $output = '<div id="' . $this->getFieldId() . '" class="multi_line_input">' . $output
476  . '</div>';
477  $output .= '<script type="text/javascript">$("#' . $this->getFieldId()
478  . '").multi_line_input(' . json_encode($this->input_options) . ')</script>';
479  }
480  $a_tpl->setCurrentBlock("prop_generic");
481  $a_tpl->setVariable("PROP_GENERIC", $output);
482  $a_tpl->parseCurrentBlock();
483  }
484 
485 
489  public function getTableFilterHTML()
490  {
491  $html = $this->render();
492 
493  return $html;
494  }
495 
496 
500  public function getToolbarHTML()
501  {
502  $html = $this->render("toolbar");
503 
504  return $html;
505  }
506 
507 
511  public function isPositionMovable()
512  {
514  }
515 
516 
521  {
522  $this->position_movable = $position_movable;
523  }
524 
525 
529  public function isShowLabelOnce()
530  {
531  return $this->show_label_once;
532  }
533 
534 
539  {
540  $this->setShowLabel(false);
541  $this->show_label_once = $show_label_once;
542  }
543 
544 
548  public function isShowInfo()
549  {
550  return $this->show_info;
551  }
552 
553 
557  public function setShowInfo($show_info)
558  {
559  $this->show_info = $show_info;
560  }
561 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
createInputPostVar($iterator_id, \ilFormPropertyGUI $input)
getTableFilterHTML()
Get HTML for table filter.
$tpl
Definition: ilias.php:10
getPostVar()
Get Post Variable.
$valid
setAlert($a_alert)
Set Alert Text.
This class represents a date/time property in a property form.
setType($a_type)
Set Type.
checkInput()
Check input, strip slashes etc.
setMulti($a_multi, $a_sortable=false, $a_addremove=true)
This class represents a hidden form property in a property form.
Class for single dates.
getFieldId()
Get Post Variable.
get(string $class_name)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
addCustomAttribute($key, $value, $override=false)
const IL_CAL_DATE
addInput(\ilFormPropertyGUI $input, $options=array())
This class represents a property in a property form.
This class represents a text area property in a property form.
Class ilOrgUnitMultiLineInputGUI.
setValueByArray($a_values)
Set value by array.
insert(&$a_tpl)
Insert property html.
__construct($a_title="", $a_postvar="")
Constructor.
$key
Definition: croninfo.php:18
render($iterator_id=0, $clean_render=false)
Render item.
$_POST["username"]
$html
Definition: example_001.php:87
$data
Definition: bench.php:6