ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilToolbarGUI.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/UIComponent/Button/classes/class.ilSubmitButton.php');
3 require_once('./Services/UIComponent/Button/classes/class.ilLinkButton.php');
4 
5 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
6 
18 {
19 
23  protected static $instances = 0;
24 
28  protected $id = '';
29 
33  protected $form_action = '';
34 
38  protected $hidden;
39 
43  public $items = array();
44 
48  protected $lead_img = array(
49  'img' => '',
50  'alt' => '',
51  );
52 
56  protected $open_form_tag = true;
57 
61  protected $close_form_tag = true;
62 
66  protected $form_target = "";
67 
71  protected $form_name = "";
72 
76  protected $prevent_double_submission = false;
77 
81  protected $sticky_items = array();
82 
86  protected $has_separator = false;
87 
88  public function __construct()
89  {
90  self::$instances++;
91  }
92 
100  public function setFormAction($a_val, $a_multipart = false, $a_target = "")
101  {
102  $this->form_action = $a_val;
103  $this->multipart = $a_multipart;
104  $this->form_target = $a_target;
105  }
106 
112  public function getFormAction()
113  {
114  return $this->form_action;
115  }
116 
117 
124  public function setLeadingImage($a_img, $a_alt)
125  {
126  $this->lead_img = array("img" => $a_img, "alt" => $a_alt);
127  }
128 
134  public function setHidden($a_val)
135  {
136  $this->hidden = $a_val;
137  }
138 
144  public function getHidden()
145  {
146  return $this->hidden;
147  }
148 
154  public function setId($a_val)
155  {
156  $this->id = $a_val;
157  }
158 
164  public function getId()
165  {
166  return $this->id ? $this->id : self::$instances;
167  }
168 
174  public function setPreventDoubleSubmission($a_val)
175  {
176  $this->prevent_double_submission = $a_val;
177  }
178 
184  public function getPreventDoubleSubmission()
185  {
187  }
188 
199  public function addButton($a_txt, $a_cmd, $a_target = "", $a_acc_key = "", $a_additional_attrs = '',
200  $a_id = "", $a_class = 'submit')
201  {
202  $this->items[] = array("type" => "button", "txt" => $a_txt, "cmd" => $a_cmd,
203  "target" => $a_target, "acc_key" => $a_acc_key, 'add_attrs' => $a_additional_attrs,
204  "id" => $a_id, "class" => $a_class);
205  }
206 
218  function addFormButton($a_txt, $a_cmd, $a_acc_key = "", $a_primary = false, $a_class = false)
219  {
220  if ($a_primary) {
221  $button = ilSubmitButton::getInstance();
222  $button->setPrimary(true);
223  $button->setCaption($a_txt, false);
224  $button->setCommand($a_cmd);
225  $button->setAccessKey($a_acc_key);
226  $this->addStickyItem($button);
227  } else {
228  $this->items[] = array("type" => "fbutton", "txt" => $a_txt, "cmd" => $a_cmd,
229  "acc_key" => $a_acc_key, "primary" => $a_primary, "class" => $a_class);
230  }
231  }
232 
233 
240  public function addInputItem(ilToolbarItem $a_item, $a_output_label = false)
241  {
242  $this->items[] = array("type" => "input", "input" => $a_item, "label" => $a_output_label);
243  }
244 
245 
253  public function addStickyItem(ilToolbarItem $a_item, $a_output_label = false)
254  {
255  $this->sticky_items[] = array("item"=>$a_item, "label"=>$a_output_label);
256  }
257 
258 
264  public function addButtonInstance(ilButtonBase $a_button)
265  {
266  if ($a_button->isPrimary()) {
267  $this->addStickyItem($a_button);
268  } else {
269  $this->items[] = array("type" => "button_obj", "instance" => $a_button);
270  }
271  }
272 
273  // bs-patch start
277  public function addDropDown($a_txt, $a_dd_html)
278  {
279  $this->items[] = array("type" => "dropdown", "txt" => $a_txt, "dd_html" => $a_dd_html);
280  }
281  // bs-patch end
282 
286  public function addSeparator()
287  {
288  $this->items[] = array("type" => "separator");
289  $this->has_separator = true;
290  }
291 
295  public function addText($a_text)
296  {
297  $this->items[] = array("type" => "text", "text" => $a_text);
298  }
299 
303  public function addSpacer($a_width = null)
304  {
305  $this->items[] = array("type" => "spacer", "width" => $a_width);
306  }
307 
308 
316  public function addLink($a_caption, $a_url, $a_disabled = false)
317  {
318  $this->items[] = array("type" => "link", "txt" => $a_caption, "cmd" => $a_url, "disabled" => $a_disabled);
319  }
320 
326  public function setOpenFormTag($a_val)
327  {
328  $this->open_form_tag = $a_val;
329  }
330 
336  public function getOpenFormTag()
337  {
338  return $this->open_form_tag;
339  }
340 
346  function setCloseFormTag($a_val)
347  {
348  $this->close_form_tag = $a_val;
349  }
350 
356  public function getCloseFormTag()
357  {
358  return $this->close_form_tag;
359  }
360 
366  public function setFormName($a_val)
367  {
368  $this->form_name = $a_val;
369  }
370 
376  public function getFormName()
377  {
378  return $this->form_name;
379  }
380 
381 
387  public function getGroupedItems()
388  {
389  $groups = array();
390  $group = array();
391  foreach ($this->items as $item) {
392  if ($item['type'] == 'separator') {
393  $groups[] = $group;
394  $group = array();
395  } else {
396  $group[] = $item;
397  }
398  }
399  if (count($group)) {
400  $groups[] = $group;
401  }
402 
403  return $groups;
404  }
405 
409  public function getHTML()
410  {
411  global $lng;
412 
414 
415  if (count($this->items) || count($this->sticky_items))
416  {
417  $tpl = new ilTemplate("tpl.toolbar.html", true, true, "Services/UIComponent/Toolbar");
418  $tpl->setVariable('TOOLBAR_ID', $this->getId());
419  if (count($this->sticky_items)) {
420  $tpl_sticky = new ilTemplate("tpl.toolbar_sticky_items.html", true, true, "Services/UIComponent/Toolbar");
422  foreach ($this->sticky_items as $sticky_item)
423  {
424  if($sticky_item['label'])
425  {
426  $tpl_sticky->setCurrentBlock('input_label');
427  $tpl_sticky->setVariable('TXT_INPUT', $sticky_item['item']->getTitle());
428  $tpl_sticky->parseCurrentBlock();
429  }
430  $tpl_sticky->setCurrentBlock('sticky_item');
431  $tpl_sticky->setVariable('STICKY_ITEM_HTML', $sticky_item['item']->getToolbarHTML());
432  $tpl_sticky->parseCurrentBlock();
433  }
434  $tpl->setCurrentBlock('sticky_items');
435  $tpl->setVariable('STICKY_ITEMS', $tpl_sticky->get());
436  $tpl->parseCurrentBlock();
437  }
438 
439  // Hide toggle button if only sticky items are in the toolbar
440  if (count($this->items) == 0) {
441  $tpl->setVariable('HIDE_TOGGLE_CLASS', ' hidden');
442  }
443 
444  $markup_items = '';
445  foreach($this->getGroupedItems() as $i => $group)
446  {
447  $tpl_items = new ilTemplate("tpl.toolbar_items.html", true, true, "Services/UIComponent/Toolbar");
448  if ($i > 0) {
449  static $tpl_separator;
450  if ($tpl_separator === null) {
451  $tpl_separator = new ilTemplate('tpl.toolbar_separator.html', true, true, 'Services/UIComponent/Toolbar');
452  }
453  $tpl_separator->touchBlock('separator');
454  $markup_items .= $tpl_separator->get();
455  }
456  foreach ($group as $item) {
457  switch ($item["type"])
458  {
459  case "button":
460  $tpl_items->setCurrentBlock("button");
461  $tpl_items->setVariable("BTN_TXT", $item["txt"]);
462  $tpl_items->setVariable("BTN_LINK", $item["cmd"]);
463  if ($item["target"] != "")
464  {
465  $tpl_items->setVariable("BTN_TARGET", 'target="'.$item["target"].'"');
466  }
467  if ($item["id"] != "")
468  {
469  $tpl_items->setVariable("BID", 'id="'.$item["id"].'"');
470  }
471  if ($item["acc_key"] != "")
472  {
473  include_once("./Services/Accessibility/classes/class.ilAccessKeyGUI.php");
474  $tpl_items->setVariable("BTN_ACC_KEY",
475  ilAccessKeyGUI::getAttribute($item["acc_key"]));
476  }
477  if(($item['add_attrs']))
478  {
479  $tpl_items->setVariable('BTN_ADD_ARG',$item['add_attrs']);
480  }
481  $tpl_items->setVariable('BTN_CLASS',$item['class']);
482  $tpl_items->parseCurrentBlock();
483  $tpl_items->touchBlock("item");
484  break;
485 
486  case "fbutton":
487  $tpl_items->setCurrentBlock("form_button");
488  $tpl_items->setVariable("SUB_TXT", $item["txt"]);
489  $tpl_items->setVariable("SUB_CMD", $item["cmd"]);
490  if($item["primary"])
491  {
492  $tpl_items->setVariable("SUB_CLASS", " emphsubmit");
493  }
494  else if($item["class"])
495  {
496  $tpl_items->setVariable("SUB_CLASS", " ".$item["class"]);
497  }
498  $tpl_items->parseCurrentBlock();
499  $tpl_items->touchBlock("item");
500  break;
501 
502  case "button_obj":
503  $tpl_items->setCurrentBlock("button_instance");
504  $tpl_items->setVariable("BUTTON_OBJ", $item["instance"]->render());
505  $tpl_items->parseCurrentBlock();
506  $tpl_items->touchBlock("item");
507  break;
508 
509  case "input":
510  if ($item["label"])
511  {
512  $tpl_items->setCurrentBlock("input_label");
513  $tpl_items->setVariable("TXT_INPUT", $item["input"]->getTitle());
514  $tpl_items->parseCurrentBlock();
515  }
516  $tpl_items->setCurrentBlock("input");
517  $tpl_items->setVariable("INPUT_HTML", $item["input"]->getToolbarHTML());
518  $tpl_items->parseCurrentBlock();
519  $tpl_items->touchBlock("item");
520  break;
521 
522  // bs-patch start
523  case "dropdown":
524  $tpl_items->setCurrentBlock("dropdown");
525  $tpl_items->setVariable("TXT_DROPDOWN", $item["txt"]);
526  $tpl_items->setVariable("DROP_DOWN", $item["dd_html"]);
527  $tpl_items->parseCurrentBlock();
528  $tpl_items->touchBlock("item");
529  break;
530  // bs-patch end
531  case "text":
532  $tpl_items->setCurrentBlock("text");
533  $tpl_items->setVariable("VAL_TEXT", $item["text"]);
534  $tpl_items->touchBlock("item");
535  break;
536 
537  case "spacer":
538  $tpl_items->touchBlock("spacer");
539  if(!$item["width"])
540  {
541  $item["width"] = 2;
542  }
543  $tpl_items->setVariable("SPACER_WIDTH", $item["width"]);
544  $tpl_items->touchBlock("item");
545  break;
546 
547  case "link":
548  if ($item["disabled"] == false) {
549  $tpl_items->setCurrentBlock("link");
550  $tpl_items->setVariable("LINK_TXT", $item["txt"]);
551  $tpl_items->setVariable("LINK_URL", $item["cmd"]);
552  $tpl_items->parseCurrentBlock();
553  $tpl_items->touchBlock("item");
554  break;
555  }
556  else {
557  $tpl_items->setCurrentBlock("link_disabled");
558  $tpl_items->setVariable("LINK_DISABLED_TXT", $item["txt"]);
559  //$tpl_items->setVariable("LINK_URL", $item["cmd"]);
560  $tpl_items->parseCurrentBlock();
561  $tpl_items->touchBlock("item");
562  break;
563  }
564  }
565  }
566  $li = (count($group) > 1) ? "<li class='ilToolbarGroup'>" : "<li>";
567  $markup_items .= $li . $tpl_items->get() . '</li>';
568  }
569 
570  $tpl->setVariable('ITEMS', $markup_items);
571  $tpl->setVariable("TXT_FUNCTIONS", $lng->txt("functions"));
572  if ($this->lead_img["img"] != "")
573  {
574  $tpl->setCurrentBlock("lead_image");
575  $tpl->setVariable("IMG_SRC", $this->lead_img["img"]);
576  $tpl->setVariable("IMG_ALT", $this->lead_img["alt"]);
577  $tpl->parseCurrentBlock();
578  }
579 
580  // form?
581  if ($this->getFormAction() != "")
582  {
583  // #18947
584  $GLOBALS["tpl"]->addJavaScript("Services/Form/js/Form.js");
585 
586  if ($this->getOpenFormTag())
587  {
588  $tpl->setCurrentBlock("form_open");
589  $tpl->setVariable("FORMACTION", $this->getFormAction());
590  if($this->getPreventDoubleSubmission())
591  {
592  $tpl->setVariable("FORM_CLASS", "preventDoubleSubmission");
593  }
594  if ($this->multipart)
595  {
596  $tpl->setVariable("ENC_TYPE", 'enctype="multipart/form-data"');
597  }
598  if ($this->form_target != "")
599  {
600  $tpl->setVariable("TARGET", ' target="'.$this->form_target.'" ');
601  }
602  if ($this->form_name != "")
603  {
604  $tpl->setVariable("FORMNAME", 'name="'.$this->getFormName().'"');
605  }
606 
607  $tpl->parseCurrentBlock();
608  }
609  if ($this->getCloseFormTag())
610  {
611  $tpl->touchBlock("form_close");
612  }
613  }
614 
615  // id
616  if ($this->getId() != "")
617  {
618  $tpl->setVariable("ID", ' id="'.$this->getId().'" ');
619  }
620 
621  // hidden style
622  if ($this->getHidden())
623  {
624  $tpl->setVariable("HIDDEN_CLASS", 'ilNoDisplay');
625  }
626 
627  return $tpl->get();
628  }
629  return "";
630  }
631 
632 
636  public function getItems()
637  {
638  return $this->items;
639  }
640 
641 
645  public function setItems($items)
646  {
647  $this->items = $items;
648  }
649 
650 
656  protected function applyAutoStickyToSingleElement()
657  {
658  if (count($this->items) == 1 && count($this->sticky_items) == 0) {
659  $supported_types = array('button', 'fbutton', 'button_obj');
660  $item = $this->items[0];
661  if (!in_array($item['type'], $supported_types)) {
662  return;
663  }
664  $button = null;
665  switch ($item['type']) {
666  case 'button_obj':
667  $button = $item['instance'];
668  break;
669  case 'fbutton':
670  $button = ilSubmitButton::getInstance();
671  $button->setPrimary($item['primary']);
672  $button->setCaption($item['txt'], false);
673  $button->setCommand($item['cmd']);
674  $button->setAccessKey($item['acc_key']);
675  break;
676  case 'button':
677  $button = ilLinkButton::getInstance();
678  $button->setCaption($item['txt'], false);
679  $button->setUrl($item['cmd']);
680  $button->setTarget($item['target']);
681  $button->setId($item['id']);
682  $button->setAccessKey($item['acc_key']);
683  break;
684  }
685  $this->addStickyItem($button);
686  $this->items = array();
687  }
688  }
689 }
applyAutoStickyToSingleElement()
If the toolbar consists of only one button, make it sticky Note: Atm this is only possible for button...
setId($a_val)
Set id.
setFormName($a_val)
Set form name.
setOpenFormTag($a_val)
Set open form tag.
addButton($a_txt, $a_cmd, $a_target="", $a_acc_key="", $a_additional_attrs='', $a_id="", $a_class='submit')
Add button to toolbar.
getFormName()
Get form name.
addSpacer($a_width=null)
Add spacer.
addText($a_text)
Add text.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
Interface for property form input GUI classes that can be used in ilToolbarGUI.
setPreventDoubleSubmission($a_val)
Set prevent double submission.
addButtonInstance(ilButtonBase $a_button)
Add button instance.
addLink($a_caption, $a_url, $a_disabled=false)
Add link.
isPrimary()
Get primary status.
global $tpl
Definition: ilias.php:8
setCloseFormTag($a_val)
Set close form tag.
addInputItem(ilToolbarItem $a_item, $a_output_label=false)
Add input item.
setFormAction($a_val, $a_multipart=false, $a_target="")
Set form action (if form action is set, toolbar is wrapped into form tags)
$li
Definition: langwiz.php:233
addFormButton($a_txt, $a_cmd, $a_acc_key="", $a_primary=false, $a_class=false)
Add form button to toolbar.
special template class to simplify handling of ITX/PEAR
addDropDown($a_txt, $a_dd_html)
Add input item.
addSeparator()
Add separator.
getGroupedItems()
Get all groups (items separated by a separator)
Create styles array
The data for the language used.
getOpenFormTag()
Get open form tag.
setHidden($a_val)
Set hidden.
addStickyItem(ilToolbarItem $a_item, $a_output_label=false)
Add a sticky item.
global $lng
Definition: privfeed.php:17
getPreventDoubleSubmission()
Get prevent double submission.
getFormAction()
Get form action.
getCloseFormTag()
Get close form tag.
static getAttribute($a_func_id)
Get accesskey HTML attribute.
getHidden()
Get hidden.
setLeadingImage($a_img, $a_alt)
Set leading image.