ILIAS  release_8 Revision v8.24
class.ilSplitButtonGUI.php
Go to the documentation of this file.
1<?php
2
24{
29 protected array $menu_items = [];
30
31 protected function __construct(int $a_type)
32 {
33 parent::__construct($a_type);
34 }
35
36 public static function getInstance(): self
37 {
38 return new self(self::TYPE_SPLIT);
39 }
40
41 public function addMenuItem(ilSplitButtonMenuItem $menu_item): void
42 {
43 $this->menu_items[] = $menu_item;
44 }
45
46 public function removeMenuItem(ilSplitButtonMenuItem $menu_item): void
47 {
48 $key = array_search($menu_item, $this->menu_items);
49 if ($key !== false) {
50 unset($this->menu_items[$key]);
51 }
52 }
53
54 public function hasMenuItems(): bool
55 {
56 return count($this->menu_items) > 0;
57 }
58
62 public function getMenuItems(): array
63 {
64 return $this->menu_items;
65 }
66
71 public function setMenuItems(array $menu_items): void
72 {
73 array_walk($menu_items, static function ($item, $idx): void {
74 if (!($item instanceof ilSplitButtonMenuItem)) {
75 throw new ilSplitButtonException(sprintf(
76 "Cannot set menu items, element at index '%s' is not of type 'ilSplitButtonItem'",
77 $idx
78 ));
79 }
80 });
81
82 $this->menu_items = $menu_items;
83 }
84
85 public function getDefaultButton(): ilButtonBase
86 {
88 }
89
90 public function hasDefaultButton(): bool
91 {
92 return ($this->default_button instanceof ilButtonBase);
93 }
94
96 {
97 $this->default_button = $default_button;
98 }
99
103 public function render(): string
104 {
105 $tpl = new ilTemplate('tpl.split_button.html', true, true, 'Services/UIComponent/SplitButton');
106
107 if (!$this->hasDefaultButton()) {
108 throw new ilSplitButtonException(
109 "Cannot render a split button without a default button"
110 );
111 }
112
113 $tpl->setVariable('DEFAULT_ITEM_CONTENT', $this->getDefaultButton()->render());
114 if ($this->hasMenuItems()) {
115 $btn_classes = $this->getDefaultButton()->getCSSClasses();
116 if ($this->getDefaultButton()->isPrimary()) {
117 $btn_classes[] = 'btn-primary';
118 }
119 $tpl->setVariable('BTN_CSS_CLASS', implode(' ', $btn_classes));
120
121 foreach ($this->getMenuItems() as $item) {
122 if ($item instanceof ilSplitButtonSeparatorMenuItem) {
123 $tpl->setCurrentBlock('separator');
124 $tpl->touchBlock('separator');
125 } else {
126 $tpl->setCurrentBlock('item');
127 $tpl->setVariable('CONTENT', $item->getContent());
128 }
129 $tpl->parseCurrentBlock();
130
131 $tpl->setCurrentBlock('items');
132 $tpl->parseCurrentBlock();
133 }
134
135 $tpl->setVariable('TXT_TOGGLE_DROPDOWN', $this->lng->txt('toggle_dropdown'));
136 }
137
138 return $tpl->get();
139 }
140}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
removeMenuItem(ilSplitButtonMenuItem $menu_item)
setMenuItems(array $menu_items)
setDefaultButton(ilButtonBase $default_button)
addMenuItem(ilSplitButtonMenuItem $menu_item)
special template class to simplify handling of ITX/PEAR
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193