1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 18a25f0a04SGreg Roach/** 1976692c8bSGreg Roach * System for generating menus. 20a25f0a04SGreg Roach */ 21a25f0a04SGreg Roachclass Menu { 22a25f0a04SGreg Roach /** @var string The text to be displayed in the mneu */ 23a25f0a04SGreg Roach private $label; 24a25f0a04SGreg Roach 25a25f0a04SGreg Roach /** @var string The target URL or href*/ 26a25f0a04SGreg Roach private $link; 27a25f0a04SGreg Roach 283d0a6fa1SGreg Roach /** @var string The CSS class used to style this menu item */ 293d0a6fa1SGreg Roach private $class; 30a25f0a04SGreg Roach 313cf92ae2SGreg Roach /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */ 323cf92ae2SGreg Roach private $attrs; 33a25f0a04SGreg Roach 3476692c8bSGreg Roach /** @var Menu[] An optional list of sub-menus. */ 35a25f0a04SGreg Roach private $submenus; 36a25f0a04SGreg Roach 37a25f0a04SGreg Roach /** 38a25f0a04SGreg Roach * Constructor for the menu class 39a25f0a04SGreg Roach * 40a25f0a04SGreg Roach * @param string $label The label for the menu item 41a25f0a04SGreg Roach * @param string $link The target URL 423cf92ae2SGreg Roach * @param string $class A CSS class 433cf92ae2SGreg Roach * @param string[] $attrs Optional attributes, such as onclick or data-xxx 44a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 45a25f0a04SGreg Roach */ 4613abd6f3SGreg Roach public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = []) { 47a25f0a04SGreg Roach $this 48a25f0a04SGreg Roach ->setLabel($label) 49a25f0a04SGreg Roach ->setLink($link) 503d0a6fa1SGreg Roach ->setClass($class) 513cf92ae2SGreg Roach ->setAttrs($attrs) 52a25f0a04SGreg Roach ->setSubmenus($submenus); 53a25f0a04SGreg Roach } 54a25f0a04SGreg Roach 55a25f0a04SGreg Roach /** 5615d603e7SGreg Roach * Render this menu using Bootstrap4 markup 57a25f0a04SGreg Roach * 58a25f0a04SGreg Roach * @return string 59a25f0a04SGreg Roach */ 6015d603e7SGreg Roach public function bootstrap4() { 61a25f0a04SGreg Roach if ($this->submenus) { 62a25f0a04SGreg Roach $submenus = ''; 63a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 6415d603e7SGreg Roach $attrs = ''; 6515d603e7SGreg Roach foreach ($submenu->attrs as $key => $value) { 66d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 67a25f0a04SGreg Roach } 68a25f0a04SGreg Roach 6915d603e7SGreg Roach $class = trim('dropdown-item ' . $submenu->class); 70*25b2dde3SGreg Roach $submenus .= '<a class="' . $class . '" href="' . e($submenu->link) . '"' . $attrs . '>' . $submenu->label . '</a>'; 7115d603e7SGreg Roach } 7215d603e7SGreg Roach 7315d603e7SGreg Roach $class = trim('nav-item dropdown ' . $this->class); 7415d603e7SGreg Roach 75a25f0a04SGreg Roach return 7615d603e7SGreg Roach '<li class="' . $class . '">' . 7715d603e7SGreg Roach '<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 78a25f0a04SGreg Roach $this->label . 79a25f0a04SGreg Roach '<span class="caret"></span></a>' . 8015d603e7SGreg Roach '<div class="dropdown-menu" role="menu">' . 81a25f0a04SGreg Roach $submenus . 8215d603e7SGreg Roach '</div>' . 83a25f0a04SGreg Roach '</li>'; 84a25f0a04SGreg Roach } else { 853cf92ae2SGreg Roach $attrs = ''; 863cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 87d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 88a25f0a04SGreg Roach } 89a25f0a04SGreg Roach 9015d603e7SGreg Roach $class = trim('nav-item ' . $this->class); 9115d603e7SGreg Roach 92*25b2dde3SGreg Roach return '<li class="' . $class . '"><a class="nav-link" href="' . e($this->link) . '"' . $attrs . '>' . $this->label . '</a></li>'; 93a25f0a04SGreg Roach } 94a25f0a04SGreg Roach } 95a25f0a04SGreg Roach 96a25f0a04SGreg Roach /** 973cf92ae2SGreg Roach * Get the optional attributes. 983cf92ae2SGreg Roach * 993cf92ae2SGreg Roach * @return string[] 1003cf92ae2SGreg Roach */ 1013cf92ae2SGreg Roach public function getAttrs() { 1023cf92ae2SGreg Roach return $this->attrs; 1033cf92ae2SGreg Roach } 1043cf92ae2SGreg Roach 1053cf92ae2SGreg Roach /** 1063cf92ae2SGreg Roach * Set the optional attributes. 1073cf92ae2SGreg Roach * 1083cf92ae2SGreg Roach * @param string[] $attrs 1093cf92ae2SGreg Roach * 1103cf92ae2SGreg Roach * @return $this 1113cf92ae2SGreg Roach */ 1123cf92ae2SGreg Roach public function setAttrs(array $attrs) { 1133cf92ae2SGreg Roach $this->attrs = $attrs; 1143cf92ae2SGreg Roach 1153cf92ae2SGreg Roach return $this; 1163cf92ae2SGreg Roach } 1173cf92ae2SGreg Roach 1183cf92ae2SGreg Roach /** 11976692c8bSGreg Roach * Get the class. 12076692c8bSGreg Roach * 121a25f0a04SGreg Roach * @return string 122a25f0a04SGreg Roach */ 1233d0a6fa1SGreg Roach public function getClass() { 1243d0a6fa1SGreg Roach return $this->class; 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach 127a25f0a04SGreg Roach /** 12876692c8bSGreg Roach * Set the class. 12976692c8bSGreg Roach * 1303d0a6fa1SGreg Roach * @param string $class 131a25f0a04SGreg Roach * 132a25f0a04SGreg Roach * @return $this 133a25f0a04SGreg Roach */ 1343d0a6fa1SGreg Roach public function setClass($class) { 1353d0a6fa1SGreg Roach $this->class = $class; 136a25f0a04SGreg Roach 137a25f0a04SGreg Roach return $this; 138a25f0a04SGreg Roach } 139a25f0a04SGreg Roach 140a25f0a04SGreg Roach /** 14176692c8bSGreg Roach * Get the label. 14276692c8bSGreg Roach * 143a25f0a04SGreg Roach * @return string 144a25f0a04SGreg Roach */ 145a25f0a04SGreg Roach public function getLabel() { 146a25f0a04SGreg Roach return $this->label; 147a25f0a04SGreg Roach } 148a25f0a04SGreg Roach 149a25f0a04SGreg Roach /** 15076692c8bSGreg Roach * Set the label. 15176692c8bSGreg Roach * 152a25f0a04SGreg Roach * @param string $label 153a25f0a04SGreg Roach * 154a25f0a04SGreg Roach * @return $this 155a25f0a04SGreg Roach */ 156a25f0a04SGreg Roach public function setLabel($label) { 157a25f0a04SGreg Roach $this->label = $label; 158a25f0a04SGreg Roach 159a25f0a04SGreg Roach return $this; 160a25f0a04SGreg Roach } 161a25f0a04SGreg Roach 162a25f0a04SGreg Roach /** 16376692c8bSGreg Roach * Get the link. 1643cf92ae2SGreg Roach * 165a25f0a04SGreg Roach * @return string 166a25f0a04SGreg Roach */ 167a25f0a04SGreg Roach public function getLink() { 168a25f0a04SGreg Roach return $this->link; 169a25f0a04SGreg Roach } 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach /** 17276692c8bSGreg Roach * Set the link. 17376692c8bSGreg Roach * 174a25f0a04SGreg Roach * @param string $link 175a25f0a04SGreg Roach * 176a25f0a04SGreg Roach * @return $this 177a25f0a04SGreg Roach */ 178a25f0a04SGreg Roach public function setLink($link) { 179a25f0a04SGreg Roach $this->link = $link; 180a25f0a04SGreg Roach 181a25f0a04SGreg Roach return $this; 182a25f0a04SGreg Roach } 183a25f0a04SGreg Roach 184a25f0a04SGreg Roach /** 185a25f0a04SGreg Roach * Add a submenu to this menu 186a25f0a04SGreg Roach * 187ecb5b5e9SGreg Roach * @param Menu $menu 188ecb5b5e9SGreg Roach * 189ecb5b5e9SGreg Roach * @return $this 190a25f0a04SGreg Roach */ 191a25f0a04SGreg Roach public function addSubmenu($menu) { 192a25f0a04SGreg Roach $this->submenus[] = $menu; 193ecb5b5e9SGreg Roach 194ecb5b5e9SGreg Roach return $this; 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach 197a25f0a04SGreg Roach /** 198a25f0a04SGreg Roach * Render this menu as an HTML list 199a25f0a04SGreg Roach * 200a25f0a04SGreg Roach * @return string 201a25f0a04SGreg Roach */ 202a25f0a04SGreg Roach public function getMenuAsList() { 2033cf92ae2SGreg Roach $attrs = ''; 2043cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 205d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 206a25f0a04SGreg Roach } 207a25f0a04SGreg Roach if ($this->link) { 208*25b2dde3SGreg Roach $link = ' href="' . e($this->link) . '"'; 209a25f0a04SGreg Roach } else { 210a25f0a04SGreg Roach $link = ''; 211a25f0a04SGreg Roach } 2123cf92ae2SGreg Roach $html = '<a' . $link . $attrs . '>' . $this->label . '</a>'; 213a25f0a04SGreg Roach if ($this->submenus) { 214a25f0a04SGreg Roach $html .= '<ul>'; 215a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 216a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 217a25f0a04SGreg Roach } 218a25f0a04SGreg Roach $html .= '</ul>'; 219a25f0a04SGreg Roach } 220a25f0a04SGreg Roach 2213d0a6fa1SGreg Roach return '<li class="' . $this->class . '">' . $html . '</li>'; 222a25f0a04SGreg Roach } 223a25f0a04SGreg Roach 224a25f0a04SGreg Roach /** 22576692c8bSGreg Roach * Get the sub-menus. 22676692c8bSGreg Roach * 227a25f0a04SGreg Roach * @return Menu[] 228a25f0a04SGreg Roach */ 229a25f0a04SGreg Roach public function getSubmenus() { 230a25f0a04SGreg Roach return $this->submenus; 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach 233a25f0a04SGreg Roach /** 23476692c8bSGreg Roach * Set the sub-menus. 23576692c8bSGreg Roach * 236a25f0a04SGreg Roach * @param Menu[] $submenus 237a25f0a04SGreg Roach * 238a25f0a04SGreg Roach * @return $this 239a25f0a04SGreg Roach */ 240a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 241a25f0a04SGreg Roach $this->submenus = $submenus; 242a25f0a04SGreg Roach 243a25f0a04SGreg Roach return $this; 244a25f0a04SGreg Roach } 245a25f0a04SGreg Roach} 246