ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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()
35  {
39  global $lng;
40 
41  $this->lng = $lng;
42  }
43 
47  public static function getInstance()
48  {
49  return new self(self::TYPE_SPLIT);
50  }
51 
55  public function addMenuItem(ilSplitButtonMenuItem $menu_item)
56  {
57  $this->menu_items[] = $menu_item;
58  }
59 
63  public function removeMenuItem(ilSplitButtonMenuItem $menu_item)
64  {
65  $key = array_search($menu_item, $this->menu_items);
66  if($key !== false)
67  {
68  unset($this->menu_items[$key]);
69  }
70  }
71 
75  public function hasMenuItems()
76  {
77  return count($this->menu_items) > 0;
78  }
79 
83  public function getMenuItems()
84  {
85  return $this->menu_items;
86  }
87 
92  public function setMenuItems($menu_items)
93  {
94  array_walk($menu_items, function(&$item, $idx) {
95  if(!($item instanceof ilSplitButtonMenuItem))
96  {
97  throw new ilSplitButtonException(sprintf(
98  "Cannot set menu items, element at index '%s' is not of type 'ilSplitButtonItem'", $idx
99  ));
100  }
101  });
102 
103  $this->menu_items = $menu_items;
104  }
105 
109  public function getDefaultButton()
110  {
111  return $this->default_button;
112  }
113 
117  public function hasDefaultButton()
118  {
119  return ($this->default_button instanceof ilButtonBase);
120  }
121 
126  {
127  $this->default_button = $default_button;
128  }
129 
134  public function render()
135  {
136  $tpl = new ilTemplate('tpl.split_button.html', true, true, 'Services/UIComponent/SplitButton');
137 
138  if(!$this->hasDefaultButton())
139  {
140  throw new ilSplitButtonException(
141  "Cannot render a split button without a default button"
142  );
143  }
144 
145  $tpl->setVariable('DEFAULT_ITEM_CONTENT', $this->getDefaultButton()->render());
146  if($this->hasMenuItems())
147  {
148  $btn_classes = $this->getDefaultButton()->getCSSClasses();
149  if($this->getDefaultButton()->isPrimary())
150  {
151  $btn_classes[] = 'btn-primary';
152  }
153  $tpl->setVariable('BTN_CSS_CLASS', implode(' ', $btn_classes));
154 
155  foreach($this->getMenuItems() as $item)
156  {
157  if($item instanceof ilSplitButtonSeparatorMenuItem)
158  {
159  $tpl->setCurrentBlock('separator');
160  $tpl->touchBlock('separator');
161  $tpl->parseCurrentBlock();
162  }
163  else
164  {
165  $tpl->setCurrentBlock('item');
166  $tpl->setVariable('CONTENT', $item->getContent());
167  $tpl->parseCurrentBlock();
168  }
169 
170  $tpl->setCurrentBlock('items');
171  $tpl->parseCurrentBlock();
172  }
173 
174  $tpl->setVariable('TXT_TOGGLE_DROPDOWN', $this->lng->txt('toggle_dropdown'));
175  }
176 
177  return $tpl->get();
178  }
179 }
addMenuItem(ilSplitButtonMenuItem $menu_item)
Interface ilSplitButtonSeparatorMenuItem.
isPrimary()
Get primary status.
global $tpl
Definition: ilias.php:8
Class ilSplitButton.
removeMenuItem(ilSplitButtonMenuItem $menu_item)
special template class to simplify handling of ITX/PEAR
Interface ilSplitButtonMenuItem.
setDefaultButton(ilButtonBase $default_button)
__construct($a_type)
Constructor.
Class ilSplittButtonException.