ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSplitButtonGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/UIComponent/SplitButton/classes/class.ilButtonToSplitButtonMenuItemAdapter.php';
5 require_once 'Services/UIComponent/SplitButton/classes/class.ilSplitButtonItemDivider.php';
6 require_once 'Services/UIComponent/SplitButton/exceptions/class.ilSplitButtonException.php';
7 require_once "Services/UIComponent/Button/classes/class.ilButtonBase.php";
8 
15 {
19  protected $lng;
20 
24  protected $default_button;
25 
29  protected $menu_items = array();
30 
34  public function __construct($a_type)
35  {
36  global $DIC;
37 
41  $lng = $DIC->language();
42 
43  $this->lng = $lng;
44 
45  parent::__construct($a_type);
46  }
47 
51  public static function getInstance()
52  {
53  return new self(self::TYPE_SPLIT);
54  }
55 
59  public function addMenuItem(ilSplitButtonMenuItem $menu_item)
60  {
61  $this->menu_items[] = $menu_item;
62  }
63 
67  public function removeMenuItem(ilSplitButtonMenuItem $menu_item)
68  {
69  $key = array_search($menu_item, $this->menu_items);
70  if ($key !== false) {
71  unset($this->menu_items[$key]);
72  }
73  }
74 
78  public function hasMenuItems()
79  {
80  return count($this->menu_items) > 0;
81  }
82 
86  public function getMenuItems()
87  {
88  return $this->menu_items;
89  }
90 
95  public function setMenuItems($menu_items)
96  {
97  array_walk($menu_items, function (&$item, $idx) {
98  if (!($item instanceof ilSplitButtonMenuItem)) {
99  throw new ilSplitButtonException(sprintf(
100  "Cannot set menu items, element at index '%s' is not of type 'ilSplitButtonItem'",
101  $idx
102  ));
103  }
104  });
105 
106  $this->menu_items = $menu_items;
107  }
108 
112  public function getDefaultButton()
113  {
114  return $this->default_button;
115  }
116 
120  public function hasDefaultButton()
121  {
122  return ($this->default_button instanceof ilButtonBase);
123  }
124 
129  {
130  $this->default_button = $default_button;
131  }
132 
137  public function render()
138  {
139  $tpl = new ilTemplate('tpl.split_button.html', true, true, 'Services/UIComponent/SplitButton');
140 
141  if (!$this->hasDefaultButton()) {
142  throw new ilSplitButtonException(
143  "Cannot render a split button without a default button"
144  );
145  }
146 
147  $tpl->setVariable('DEFAULT_ITEM_CONTENT', $this->getDefaultButton()->render());
148  if ($this->hasMenuItems()) {
149  $btn_classes = $this->getDefaultButton()->getCSSClasses();
150  if ($this->getDefaultButton()->isPrimary()) {
151  $btn_classes[] = 'btn-primary';
152  }
153  $tpl->setVariable('BTN_CSS_CLASS', implode(' ', $btn_classes));
154 
155  foreach ($this->getMenuItems() as $item) {
156  if ($item instanceof ilSplitButtonSeparatorMenuItem) {
157  $tpl->setCurrentBlock('separator');
158  $tpl->touchBlock('separator');
159  $tpl->parseCurrentBlock();
160  } else {
161  $tpl->setCurrentBlock('item');
162  $tpl->setVariable('CONTENT', $item->getContent());
163  $tpl->parseCurrentBlock();
164  }
165 
166  $tpl->setCurrentBlock('items');
167  $tpl->parseCurrentBlock();
168  }
169 
170  $tpl->setVariable('TXT_TOGGLE_DROPDOWN', $this->lng->txt('toggle_dropdown'));
171  }
172 
173  return $tpl->get();
174  }
175 }
addMenuItem(ilSplitButtonMenuItem $menu_item)
global $DIC
Definition: saml.php:7
Interface ilSplitButtonSeparatorMenuItem.
$tpl
Definition: ilias.php:10
isPrimary()
Get primary status.
Class ilSplitButton.
$a_type
Definition: workflow.php:92
removeMenuItem(ilSplitButtonMenuItem $menu_item)
special template class to simplify handling of ITX/PEAR
Interface ilSplitButtonMenuItem.
setDefaultButton(ilButtonBase $default_button)
__construct($a_type)
Constructor.
$key
Definition: croninfo.php:18
Class ilSplittButtonException.