1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 46bdf7674SGreg Roach * Copyright (C) 2017 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 /** @var string Used to format javascript menus */ 38a25f0a04SGreg Roach private $submenuclass; 39a25f0a04SGreg Roach 40a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 413d0a6fa1SGreg Roach private $menuclass; 42a25f0a04SGreg Roach 43a25f0a04SGreg Roach /** 44a25f0a04SGreg Roach * Constructor for the menu class 45a25f0a04SGreg Roach * 46a25f0a04SGreg Roach * @param string $label The label for the menu item 47a25f0a04SGreg Roach * @param string $link The target URL 483cf92ae2SGreg Roach * @param string $class A CSS class 493cf92ae2SGreg Roach * @param string[] $attrs Optional attributes, such as onclick or data-xxx 50a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 51a25f0a04SGreg Roach */ 5213abd6f3SGreg Roach public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = []) { 53a25f0a04SGreg Roach $this 54a25f0a04SGreg Roach ->setLabel($label) 55a25f0a04SGreg Roach ->setLink($link) 563d0a6fa1SGreg Roach ->setClass($class) 573cf92ae2SGreg Roach ->setAttrs($attrs) 58a25f0a04SGreg Roach ->setSubmenus($submenus); 59a25f0a04SGreg Roach } 60a25f0a04SGreg Roach 61a25f0a04SGreg Roach /** 6215d603e7SGreg Roach * Render this menu using Bootstrap4 markup 63a25f0a04SGreg Roach * 64a25f0a04SGreg Roach * @return string 65a25f0a04SGreg Roach */ 6615d603e7SGreg Roach public function bootstrap4() { 67a25f0a04SGreg Roach if ($this->submenus) { 68a25f0a04SGreg Roach $submenus = ''; 69a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 7015d603e7SGreg Roach $attrs = ''; 7115d603e7SGreg Roach foreach ($submenu->attrs as $key => $value) { 72*d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 73a25f0a04SGreg Roach } 74a25f0a04SGreg Roach 7515d603e7SGreg Roach $class = trim('dropdown-item ' . $submenu->class); 7615d603e7SGreg Roach $submenus .= '<a class="' . $class . '" href="' . $submenu->link . '"' . $attrs . '>' . $submenu->label . '</a>'; 7715d603e7SGreg Roach } 7815d603e7SGreg Roach 7915d603e7SGreg Roach $class = trim('nav-item dropdown ' . $this->class); 8015d603e7SGreg Roach 81a25f0a04SGreg Roach return 8215d603e7SGreg Roach '<li class="' . $class . '">' . 8315d603e7SGreg Roach '<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 84a25f0a04SGreg Roach $this->label . 85a25f0a04SGreg Roach '<span class="caret"></span></a>' . 8615d603e7SGreg Roach '<div class="dropdown-menu" role="menu">' . 87a25f0a04SGreg Roach $submenus . 8815d603e7SGreg Roach '</div>' . 89a25f0a04SGreg Roach '</li>'; 90a25f0a04SGreg Roach } else { 913cf92ae2SGreg Roach $attrs = ''; 923cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 93*d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 94a25f0a04SGreg Roach } 95a25f0a04SGreg Roach 9615d603e7SGreg Roach $class = trim('nav-item ' . $this->class); 9715d603e7SGreg Roach 9815d603e7SGreg Roach return '<li class="' . $class . '"><a class="nav-link" href="' . $this->link . '"' . $attrs . '>' . $this->label . '</a></li>'; 99a25f0a04SGreg Roach } 100a25f0a04SGreg Roach } 101a25f0a04SGreg Roach 102a25f0a04SGreg Roach /** 1033cf92ae2SGreg Roach * Get the optional attributes. 1043cf92ae2SGreg Roach * 1053cf92ae2SGreg Roach * @return string[] 1063cf92ae2SGreg Roach */ 1073cf92ae2SGreg Roach public function getAttrs() { 1083cf92ae2SGreg Roach return $this->attrs; 1093cf92ae2SGreg Roach } 1103cf92ae2SGreg Roach 1113cf92ae2SGreg Roach /** 1123cf92ae2SGreg Roach * Set the optional attributes. 1133cf92ae2SGreg Roach * 1143cf92ae2SGreg Roach * @param string[] $attrs 1153cf92ae2SGreg Roach * 1163cf92ae2SGreg Roach * @return $this 1173cf92ae2SGreg Roach */ 1183cf92ae2SGreg Roach public function setAttrs(array $attrs) { 1193cf92ae2SGreg Roach $this->attrs = $attrs; 1203cf92ae2SGreg Roach 1213cf92ae2SGreg Roach return $this; 1223cf92ae2SGreg Roach } 1233cf92ae2SGreg Roach 1243cf92ae2SGreg Roach /** 1253d0a6fa1SGreg Roach * Set the CSS classes for the (legacy) javascript menus 126a25f0a04SGreg Roach * 127794c6b5bSGreg Roach * @param string $menuclass 128a25f0a04SGreg Roach * @param string $submenuclass 129a25f0a04SGreg Roach */ 130cfd4d92fSGreg Roach public function addClass($menuclass, $submenuclass = '') { 1313d0a6fa1SGreg Roach $this->menuclass = $menuclass; 132a25f0a04SGreg Roach $this->submenuclass = $submenuclass; 133a25f0a04SGreg Roach } 134a25f0a04SGreg Roach 135a25f0a04SGreg Roach /** 13676692c8bSGreg Roach * Get the class. 13776692c8bSGreg Roach * 138a25f0a04SGreg Roach * @return string 139a25f0a04SGreg Roach */ 1403d0a6fa1SGreg Roach public function getClass() { 1413d0a6fa1SGreg Roach return $this->class; 142a25f0a04SGreg Roach } 143a25f0a04SGreg Roach 144a25f0a04SGreg Roach /** 14576692c8bSGreg Roach * Set the class. 14676692c8bSGreg Roach * 1473d0a6fa1SGreg Roach * @param string $class 148a25f0a04SGreg Roach * 149a25f0a04SGreg Roach * @return $this 150a25f0a04SGreg Roach */ 1513d0a6fa1SGreg Roach public function setClass($class) { 1523d0a6fa1SGreg Roach $this->class = $class; 153a25f0a04SGreg Roach 154a25f0a04SGreg Roach return $this; 155a25f0a04SGreg Roach } 156a25f0a04SGreg Roach 157a25f0a04SGreg Roach /** 15876692c8bSGreg Roach * Get the label. 15976692c8bSGreg Roach * 160a25f0a04SGreg Roach * @return string 161a25f0a04SGreg Roach */ 162a25f0a04SGreg Roach public function getLabel() { 163a25f0a04SGreg Roach return $this->label; 164a25f0a04SGreg Roach } 165a25f0a04SGreg Roach 166a25f0a04SGreg Roach /** 16776692c8bSGreg Roach * Set the label. 16876692c8bSGreg Roach * 169a25f0a04SGreg Roach * @param string $label 170a25f0a04SGreg Roach * 171a25f0a04SGreg Roach * @return $this 172a25f0a04SGreg Roach */ 173a25f0a04SGreg Roach public function setLabel($label) { 174a25f0a04SGreg Roach $this->label = $label; 175a25f0a04SGreg Roach 176a25f0a04SGreg Roach return $this; 177a25f0a04SGreg Roach } 178a25f0a04SGreg Roach 179a25f0a04SGreg Roach /** 18076692c8bSGreg Roach * Get the link. 1813cf92ae2SGreg Roach * 182a25f0a04SGreg Roach * @return string 183a25f0a04SGreg Roach */ 184a25f0a04SGreg Roach public function getLink() { 185a25f0a04SGreg Roach return $this->link; 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach 188a25f0a04SGreg Roach /** 18976692c8bSGreg Roach * Set the link. 19076692c8bSGreg Roach * 191a25f0a04SGreg Roach * @param string $link 192a25f0a04SGreg Roach * 193a25f0a04SGreg Roach * @return $this 194a25f0a04SGreg Roach */ 195a25f0a04SGreg Roach public function setLink($link) { 196a25f0a04SGreg Roach $this->link = $link; 197a25f0a04SGreg Roach 198a25f0a04SGreg Roach return $this; 199a25f0a04SGreg Roach } 200a25f0a04SGreg Roach 201a25f0a04SGreg Roach /** 202a25f0a04SGreg Roach * Add a submenu to this menu 203a25f0a04SGreg Roach * 204ecb5b5e9SGreg Roach * @param Menu $menu 205ecb5b5e9SGreg Roach * 206ecb5b5e9SGreg Roach * @return $this 207a25f0a04SGreg Roach */ 208a25f0a04SGreg Roach public function addSubmenu($menu) { 209a25f0a04SGreg Roach $this->submenus[] = $menu; 210ecb5b5e9SGreg Roach 211ecb5b5e9SGreg Roach return $this; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214a25f0a04SGreg Roach /** 215a25f0a04SGreg Roach * Render this menu as an HTML list 216a25f0a04SGreg Roach * 217a25f0a04SGreg Roach * @return string 218a25f0a04SGreg Roach */ 219a25f0a04SGreg Roach public function getMenuAsList() { 2203cf92ae2SGreg Roach $attrs = ''; 2213cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 222*d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 223a25f0a04SGreg Roach } 224a25f0a04SGreg Roach if ($this->link) { 225a25f0a04SGreg Roach $link = ' href="' . $this->link . '"'; 226a25f0a04SGreg Roach } else { 227a25f0a04SGreg Roach $link = ''; 228a25f0a04SGreg Roach } 2293cf92ae2SGreg Roach $html = '<a' . $link . $attrs . '>' . $this->label . '</a>'; 230a25f0a04SGreg Roach if ($this->submenus) { 231a25f0a04SGreg Roach $html .= '<ul>'; 232a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 233a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach $html .= '</ul>'; 236a25f0a04SGreg Roach } 237a25f0a04SGreg Roach 2383d0a6fa1SGreg Roach return '<li class="' . $this->class . '">' . $html . '</li>'; 239a25f0a04SGreg Roach } 240a25f0a04SGreg Roach 241a25f0a04SGreg Roach /** 24276692c8bSGreg Roach * Get the sub-menus. 24376692c8bSGreg Roach * 244a25f0a04SGreg Roach * @return Menu[] 245a25f0a04SGreg Roach */ 246a25f0a04SGreg Roach public function getSubmenus() { 247a25f0a04SGreg Roach return $this->submenus; 248a25f0a04SGreg Roach } 249a25f0a04SGreg Roach 250a25f0a04SGreg Roach /** 25176692c8bSGreg Roach * Set the sub-menus. 25276692c8bSGreg Roach * 253a25f0a04SGreg Roach * @param Menu[] $submenus 254a25f0a04SGreg Roach * 255a25f0a04SGreg Roach * @return $this 256a25f0a04SGreg Roach */ 257a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 258a25f0a04SGreg Roach $this->submenus = $submenus; 259a25f0a04SGreg Roach 260a25f0a04SGreg Roach return $this; 261a25f0a04SGreg Roach } 262a25f0a04SGreg Roach} 263