ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
4require_once 'Services/UIComponent/SplitButton/classes/class.ilButtonToSplitButtonMenuItemAdapter.php';
5require_once 'Services/UIComponent/SplitButton/classes/class.ilSplitButtonItemDivider.php';
6require_once 'Services/UIComponent/SplitButton/exceptions/class.ilSplitButtonException.php';
7require_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
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 {
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}
An exception for terminatinating execution or to throw for unit testing.
isPrimary()
Get primary status.
__construct($a_type)
Constructor.
Class ilSplittButtonException.
Class ilSplitButton.
removeMenuItem(ilSplitButtonMenuItem $menu_item)
setDefaultButton(ilButtonBase $default_button)
addMenuItem(ilSplitButtonMenuItem $menu_item)
special template class to simplify handling of ITX/PEAR
Interface ilSplitButtonMenuItem.
Interface ilSplitButtonSeparatorMenuItem.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46