ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
22  protected $lng;
23 
24 
28  protected static $instances = 0;
29 
33  protected $id = '';
34 
38  protected $form_action = '';
39 
43  protected $hidden;
44 
48  public $items = array();
49 
53  protected $lead_img = array(
54  'img' => '',
55  'alt' => '',
56  );
57 
61  protected $open_form_tag = true;
62 
66  protected $close_form_tag = true;
67 
71  protected $form_target = "";
72 
76  protected $form_name = "";
77 
81  protected $prevent_double_submission = false;
82 
86  protected $sticky_items = array();
87 
91  protected $has_separator = false;
92 
93  public function __construct()
94  {
95  global $DIC;
96  $this->lng = $DIC->language();
97 
98  $this->ui = $DIC->ui();
99 
100  self::$instances++;
101  }
102 
110  public function setFormAction($a_val, $a_multipart = false, $a_target = "")
111  {
112  $this->form_action = $a_val;
113  $this->multipart = $a_multipart;
114  $this->form_target = $a_target;
115  }
116 
122  public function getFormAction()
123  {
124  return $this->form_action;
125  }
126 
127 
134  public function setLeadingImage($a_img, $a_alt)
135  {
136  $this->lead_img = array("img" => $a_img, "alt" => $a_alt);
137  }
138 
144  public function setHidden($a_val)
145  {
146  $this->hidden = $a_val;
147  }
148 
154  public function getHidden()
155  {
156  return $this->hidden;
157  }
158 
164  public function setId($a_val)
165  {
166  $this->id = $a_val;
167  }
168 
174  public function getId()
175  {
176  return $this->id ? $this->id : self::$instances;
177  }
178 
184  public function setPreventDoubleSubmission($a_val)
185  {
186  $this->prevent_double_submission = $a_val;
187  }
188 
194  public function getPreventDoubleSubmission()
195  {
197  }
198 
209  public function addButton(
210  $a_txt,
211  $a_cmd,
212  $a_target = "",
213  $a_acc_key = "",
214  $a_additional_attrs = '',
215  $a_id = "",
216  $a_class = 'submit'
217  ) {
218  $this->items[] = array("type" => "button", "txt" => $a_txt, "cmd" => $a_cmd,
219  "target" => $a_target, "acc_key" => $a_acc_key, 'add_attrs' => $a_additional_attrs,
220  "id" => $a_id, "class" => $a_class);
221  }
222 
234  public function addFormButton($a_txt, $a_cmd, $a_acc_key = "", $a_primary = false, $a_class = false)
235  {
236  if ($a_primary) {
237  $button = ilSubmitButton::getInstance();
238  $button->setPrimary(true);
239  $button->setCaption($a_txt, false);
240  $button->setCommand($a_cmd);
241  $button->setAccessKey($a_acc_key);
242  $this->addStickyItem($button);
243  } else {
244  $this->items[] = array("type" => "fbutton", "txt" => $a_txt, "cmd" => $a_cmd,
245  "acc_key" => $a_acc_key, "primary" => $a_primary, "class" => $a_class);
246  }
247  }
248 
249 
256  public function addInputItem(ilToolbarItem $a_item, $a_output_label = false)
257  {
258  $this->items[] = array("type" => "input", "input" => $a_item, "label" => $a_output_label);
259  }
260 
261 
269  public function addStickyItem($a_item, $a_output_label = false)
270  {
271  $this->sticky_items[] = array("item" => $a_item, "label" => $a_output_label);
272  }
273 
274 
280  public function addButtonInstance(ilButtonBase $a_button)
281  {
282  if ($a_button->isPrimary()) {
283  $this->addStickyItem($a_button);
284  } else {
285  $this->items[] = array("type" => "button_obj", "instance" => $a_button);
286  }
287  }
288 
289  // bs-patch start
293  public function addDropDown($a_txt, $a_dd_html)
294  {
295  $this->items[] = array("type" => "dropdown", "txt" => $a_txt, "dd_html" => $a_dd_html);
296  }
297  // bs-patch end
298 
302  public function addSeparator()
303  {
304  $this->items[] = array("type" => "separator");
305  $this->has_separator = true;
306  }
307 
311  public function addText($a_text)
312  {
313  $this->items[] = array("type" => "text", "text" => $a_text);
314  }
315 
319  public function addSpacer($a_width = null)
320  {
321  $this->items[] = array("type" => "spacer", "width" => $a_width);
322  }
323 
327  public function addComponent(\ILIAS\UI\Component\Component $a_comp)
328  {
329  $this->items[] = array("type" => "component", "component" => $a_comp);
330  }
331 
339  public function addLink($a_caption, $a_url, $a_disabled = false)
340  {
341  $this->items[] = array("type" => "link", "txt" => $a_caption, "cmd" => $a_url, "disabled" => $a_disabled);
342  }
343 
349  public function setOpenFormTag($a_val)
350  {
351  $this->open_form_tag = $a_val;
352  }
353 
359  public function getOpenFormTag()
360  {
361  return $this->open_form_tag;
362  }
363 
369  public function setCloseFormTag($a_val)
370  {
371  $this->close_form_tag = $a_val;
372  }
373 
379  public function getCloseFormTag()
380  {
381  return $this->close_form_tag;
382  }
383 
389  public function setFormName($a_val)
390  {
391  $this->form_name = $a_val;
392  }
393 
399  public function getFormName()
400  {
401  return $this->form_name;
402  }
403 
404 
410  public function getGroupedItems()
411  {
412  $groups = array();
413  $group = array();
414  foreach ($this->items as $item) {
415  if ($item['type'] == 'separator') {
416  $groups[] = $group;
417  $group = array();
418  } else {
419  $group[] = $item;
420  }
421  }
422  if (count($group)) {
423  $groups[] = $group;
424  }
425 
426  return $groups;
427  }
428 
432  public function getHTML()
433  {
434  $lng = $this->lng;
435 
437 
438  if (count($this->items) || count($this->sticky_items)) {
439  $tpl = new ilTemplate("tpl.toolbar.html", true, true, "Services/UIComponent/Toolbar");
440  $tpl->setVariable('TOOLBAR_ID', $this->getId());
441  if (count($this->sticky_items)) {
442  $tpl_sticky = new ilTemplate("tpl.toolbar_sticky_items.html", true, true, "Services/UIComponent/Toolbar");
444  foreach ($this->sticky_items as $sticky_item) {
445  if ($sticky_item['label']) {
446  $tpl_sticky->setCurrentBlock('input_label');
447  $tpl_sticky->setVariable('TXT_INPUT', $sticky_item['item']->getTitle());
448  $tpl_sticky->parseCurrentBlock();
449  }
450 
451  if ($sticky_item['item'] instanceof ilToolbarItem) {
452  $tpl_sticky->setCurrentBlock('sticky_item');
453  $tpl_sticky->setVariable('STICKY_ITEM_HTML', $sticky_item['item']->getToolbarHTML());
454  $tpl_sticky->parseCurrentBlock();
455  } elseif ($sticky_item['item'] instanceof \ILIAS\UI\Component\Component) {
456  $tpl_sticky->setCurrentBlock("sticky_item");
457  $tpl_sticky->setVariable("STICKY_ITEM_HTML", $this->ui->renderer()->render($sticky_item['item']));
458  $tpl_sticky->parseCurrentBlock();
459  }
460  }
461  $tpl->setCurrentBlock('sticky_items');
462  $tpl->setVariable('STICKY_ITEMS', $tpl_sticky->get());
463  $tpl->parseCurrentBlock();
464  }
465 
466  // Hide toggle button if only sticky items are in the toolbar
467  if (count($this->items) == 0) {
468  $tpl->setVariable('HIDE_TOGGLE_CLASS', ' hidden');
469  }
470 
471  $markup_items = '';
472  foreach ($this->getGroupedItems() as $i => $group) {
473  $tpl_items = new ilTemplate("tpl.toolbar_items.html", true, true, "Services/UIComponent/Toolbar");
474  if ($i > 0) {
475  static $tpl_separator;
476  if ($tpl_separator === null) {
477  $tpl_separator = new ilTemplate('tpl.toolbar_separator.html', true, true, 'Services/UIComponent/Toolbar');
478  }
479  $tpl_separator->touchBlock('separator');
480  $markup_items .= $tpl_separator->get();
481  }
482  foreach ($group as $item) {
483  switch ($item["type"]) {
484  case "button":
485  $tpl_items->setCurrentBlock("button");
486  $tpl_items->setVariable("BTN_TXT", $item["txt"]);
487  $tpl_items->setVariable("BTN_LINK", $item["cmd"]);
488  if ($item["target"] != "") {
489  $tpl_items->setVariable("BTN_TARGET", 'target="' . $item["target"] . '"');
490  }
491  if ($item["id"] != "") {
492  $tpl_items->setVariable("BID", 'id="' . $item["id"] . '"');
493  }
494  if ($item["acc_key"] != "") {
495  include_once("./Services/Accessibility/classes/class.ilAccessKeyGUI.php");
496  $tpl_items->setVariable(
497  "BTN_ACC_KEY",
498  ilAccessKeyGUI::getAttribute($item["acc_key"])
499  );
500  }
501  if (($item['add_attrs'])) {
502  $tpl_items->setVariable('BTN_ADD_ARG', $item['add_attrs']);
503  }
504  $tpl_items->setVariable('BTN_CLASS', $item['class']);
505  $tpl_items->parseCurrentBlock();
506  $tpl_items->touchBlock("item");
507  break;
508 
509  case "fbutton":
510  $tpl_items->setCurrentBlock("form_button");
511  $tpl_items->setVariable("SUB_TXT", $item["txt"]);
512  $tpl_items->setVariable("SUB_CMD", $item["cmd"]);
513  if ($item["primary"]) {
514  $tpl_items->setVariable("SUB_CLASS", " emphsubmit");
515  } elseif ($item["class"]) {
516  $tpl_items->setVariable("SUB_CLASS", " " . $item["class"]);
517  }
518  $tpl_items->parseCurrentBlock();
519  $tpl_items->touchBlock("item");
520  break;
521 
522  case "button_obj":
523  $tpl_items->setCurrentBlock("button_instance");
524  $tpl_items->setVariable("BUTTON_OBJ", $item["instance"]->render());
525  $tpl_items->parseCurrentBlock();
526  $tpl_items->touchBlock("item");
527  break;
528 
529  case "input":
530  if ($item["label"]) {
531  $tpl_items->setCurrentBlock("input_label");
532  $tpl_items->setVariable("TXT_INPUT", $item["input"]->getTitle());
533  $tpl_items->parseCurrentBlock();
534  }
535  $tpl_items->setCurrentBlock("input");
536  $tpl_items->setVariable("INPUT_HTML", $item["input"]->getToolbarHTML());
537  $tpl_items->parseCurrentBlock();
538  $tpl_items->touchBlock("item");
539  break;
540 
541  // bs-patch start
542  case "dropdown":
543  $tpl_items->setCurrentBlock("dropdown");
544  $tpl_items->setVariable("TXT_DROPDOWN", $item["txt"]);
545  $tpl_items->setVariable("DROP_DOWN", $item["dd_html"]);
546  $tpl_items->parseCurrentBlock();
547  $tpl_items->touchBlock("item");
548  break;
549  // bs-patch end
550  case "text":
551  $tpl_items->setCurrentBlock("text");
552  $tpl_items->setVariable("VAL_TEXT", $item["text"]);
553  $tpl_items->touchBlock("item");
554  break;
555 
556  case "component":
557  $tpl_items->setCurrentBlock("component");
558  $tpl_items->setVariable("COMPONENT", $this->ui->renderer()->render($item["component"]));
559  $tpl_items->touchBlock("item");
560  break;
561 
562  case "spacer":
563  $tpl_items->touchBlock("spacer");
564  if (!$item["width"]) {
565  $item["width"] = 2;
566  }
567  $tpl_items->setVariable("SPACER_WIDTH", $item["width"]);
568  $tpl_items->touchBlock("item");
569  break;
570 
571  case "link":
572  if ($item["disabled"] == false) {
573  $tpl_items->setCurrentBlock("link");
574  $tpl_items->setVariable("LINK_TXT", $item["txt"]);
575  $tpl_items->setVariable("LINK_URL", $item["cmd"]);
576  $tpl_items->parseCurrentBlock();
577  $tpl_items->touchBlock("item");
578  break;
579  } else {
580  $tpl_items->setCurrentBlock("link_disabled");
581  $tpl_items->setVariable("LINK_DISABLED_TXT", $item["txt"]);
582  //$tpl_items->setVariable("LINK_URL", $item["cmd"]);
583  $tpl_items->parseCurrentBlock();
584  $tpl_items->touchBlock("item");
585  break;
586  }
587  }
588  }
589  $li = (count($group) > 1) ? "<li class='ilToolbarGroup'>" : "<li>";
590  $markup_items .= $li . $tpl_items->get() . '</li>';
591  }
592 
593  $tpl->setVariable('ITEMS', $markup_items);
594  $tpl->setVariable("TXT_FUNCTIONS", $lng->txt("functions"));
595  if ($this->lead_img["img"] != "") {
596  $tpl->setCurrentBlock("lead_image");
597  $tpl->setVariable("IMG_SRC", $this->lead_img["img"]);
598  $tpl->setVariable("IMG_ALT", $this->lead_img["alt"]);
599  $tpl->parseCurrentBlock();
600  }
601 
602  // form?
603  if ($this->getFormAction() != "") {
604  // #18947
605  $GLOBALS["tpl"]->addJavaScript("Services/Form/js/Form.js");
606 
607  if ($this->getOpenFormTag()) {
608  $tpl->setCurrentBlock("form_open");
609  $tpl->setVariable("FORMACTION", $this->getFormAction());
610  if ($this->getPreventDoubleSubmission()) {
611  $tpl->setVariable("FORM_CLASS", "preventDoubleSubmission");
612  }
613  if ($this->multipart) {
614  $tpl->setVariable("ENC_TYPE", 'enctype="multipart/form-data"');
615  }
616  if ($this->form_target != "") {
617  $tpl->setVariable("TARGET", ' target="' . $this->form_target . '" ');
618  }
619  if ($this->form_name != "") {
620  $tpl->setVariable("FORMNAME", 'name="' . $this->getFormName() . '"');
621  }
622 
623  $tpl->parseCurrentBlock();
624  }
625  if ($this->getCloseFormTag()) {
626  $tpl->touchBlock("form_close");
627  }
628  }
629 
630  // id
631  if ($this->getId() != "") {
632  $tpl->setVariable("ID", ' id="' . $this->getId() . '" ');
633  }
634 
635  // hidden style
636  if ($this->getHidden()) {
637  $tpl->setVariable("HIDDEN_CLASS", 'ilNoDisplay');
638  }
639 
640  return $tpl->get();
641  }
642  return "";
643  }
644 
645 
649  public function getItems()
650  {
651  return $this->items;
652  }
653 
654 
658  public function setItems($items)
659  {
660  $this->items = $items;
661  }
662 
663 
669  protected function applyAutoStickyToSingleElement()
670  {
671  if (count($this->items) == 1 && count($this->sticky_items) == 0) {
672  $supported_types = array('button', 'fbutton', 'button_obj');
673  $item = $this->items[0];
674  if (!in_array($item['type'], $supported_types)) {
675  return;
676  }
677  $button = null;
678  switch ($item['type']) {
679  case 'button_obj':
680  $button = $item['instance'];
681  break;
682  case 'fbutton':
683  $button = ilSubmitButton::getInstance();
684  $button->setPrimary($item['primary']);
685  $button->setCaption($item['txt'], false);
686  $button->setCommand($item['cmd']);
687  $button->setAccessKey($item['acc_key']);
688  break;
689  case 'button':
690  $button = ilLinkButton::getInstance();
691  $button->setCaption($item['txt'], false);
692  $button->setUrl($item['cmd']);
693  $button->setTarget($item['target']);
694  $button->setId($item['id']);
695  $button->setAccessKey($item['acc_key']);
696  break;
697  }
698  $this->addStickyItem($button);
699  $this->items = array();
700  }
701  }
702 }
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.
Class Factory.
global $DIC
Definition: saml.php:7
addStickyItem($a_item, $a_output_label=false)
Add a sticky item.
setOpenFormTag($a_val)
Set open form tag.
$tpl
Definition: ilias.php:10
Class BaseForm.
getFormName()
Get form name.
addComponent(\ILIAS\UI\Component\Component $a_comp)
Add component.
addSpacer($a_width=null)
Add spacer.
addText($a_text)
Add text.
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.
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)
getOpenFormTag()
Get open form tag.
setHidden($a_val)
Set hidden.
addButton( $a_txt, $a_cmd, $a_target="", $a_acc_key="", $a_additional_attrs='', $a_id="", $a_class='submit')
Add button to toolbar.
getPreventDoubleSubmission()
Get prevent double submission.
getFormAction()
Get form action.
getCloseFormTag()
Get close form tag.
$i
Definition: disco.tpl.php:19
static getAttribute($a_func_id)
Get accesskey HTML attribute.
getHidden()
Get hidden.
setLeadingImage($a_img, $a_alt)
Set leading image.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.