ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
39  global $lng;
40 
41  $this->lng = $lng;
42 
43  parent::__construct($a_type);
44  }
45 
49  public static function getInstance()
50  {
51  return new self(self::TYPE_SPLIT);
52  }
53 
57  public function addMenuItem(ilSplitButtonMenuItem $menu_item)
58  {
59  $this->menu_items[] = $menu_item;
60  }
61 
65  public function removeMenuItem(ilSplitButtonMenuItem $menu_item)
66  {
67  $key = array_search($menu_item, $this->menu_items);
68  if($key !== false)
69  {
70  unset($this->menu_items[$key]);
71  }
72  }
73 
77  public function hasMenuItems()
78  {
79  return count($this->menu_items) > 0;
80  }
81 
85  public function getMenuItems()
86  {
87  return $this->menu_items;
88  }
89 
94  public function setMenuItems($menu_items)
95  {
96  array_walk($menu_items, function(&$item, $idx) {
97  if(!($item instanceof ilSplitButtonMenuItem))
98  {
99  throw new ilSplitButtonException(sprintf(
100  "Cannot set menu items, element at index '%s' is not of type 'ilSplitButtonItem'", $idx
101  ));
102  }
103  });
104 
105  $this->menu_items = $menu_items;
106  }
107 
111  public function getDefaultButton()
112  {
113  return $this->default_button;
114  }
115 
119  public function hasDefaultButton()
120  {
121  return ($this->default_button instanceof ilButtonBase);
122  }
123 
128  {
129  $this->default_button = $default_button;
130  }
131 
136  public function render()
137  {
138  $tpl = new ilTemplate('tpl.split_button.html', true, true, 'Services/UIComponent/SplitButton');
139 
140  if(!$this->hasDefaultButton())
141  {
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  {
150  $btn_classes = $this->getDefaultButton()->getCSSClasses();
151  if($this->getDefaultButton()->isPrimary())
152  {
153  $btn_classes[] = 'btn-primary';
154  }
155  $tpl->setVariable('BTN_CSS_CLASS', implode(' ', $btn_classes));
156 
157  foreach($this->getMenuItems() as $item)
158  {
159  if($item instanceof ilSplitButtonSeparatorMenuItem)
160  {
161  $tpl->setCurrentBlock('separator');
162  $tpl->touchBlock('separator');
163  $tpl->parseCurrentBlock();
164  }
165  else
166  {
167  $tpl->setCurrentBlock('item');
168  $tpl->setVariable('CONTENT', $item->getContent());
169  $tpl->parseCurrentBlock();
170  }
171 
172  $tpl->setCurrentBlock('items');
173  $tpl->parseCurrentBlock();
174  }
175 
176  $tpl->setVariable('TXT_TOGGLE_DROPDOWN', $this->lng->txt('toggle_dropdown'));
177  }
178 
179  return $tpl->get();
180  }
181 }
addMenuItem(ilSplitButtonMenuItem $menu_item)
Interface ilSplitButtonSeparatorMenuItem.
isPrimary()
Get primary status.
global $tpl
Definition: ilias.php:8
Class ilSplitButton.
$a_type
Definition: workflow.php:93
removeMenuItem(ilSplitButtonMenuItem $menu_item)
special template class to simplify handling of ITX/PEAR
Interface ilSplitButtonMenuItem.
Create styles array
The data for the language used.
setDefaultButton(ilButtonBase $default_button)
__construct($a_type)
Constructor.
Class ilSplittButtonException.