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