1a25f0a04SGreg Roach<?php 2*3976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees; 20a25f0a04SGreg Roach 21a25f0a04SGreg Roach/** 2276692c8bSGreg Roach * System for generating menus. 23a25f0a04SGreg Roach */ 24c1010edaSGreg Roachclass Menu 25c1010edaSGreg Roach{ 26a25f0a04SGreg Roach /** @var string The text to be displayed in the mneu */ 27a25f0a04SGreg Roach private $label; 28a25f0a04SGreg Roach 29a25f0a04SGreg Roach /** @var string The target URL or href */ 30a25f0a04SGreg Roach private $link; 31a25f0a04SGreg Roach 323d0a6fa1SGreg Roach /** @var string The CSS class used to style this menu item */ 333d0a6fa1SGreg Roach private $class; 34a25f0a04SGreg Roach 353cf92ae2SGreg Roach /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */ 363cf92ae2SGreg Roach private $attrs; 37a25f0a04SGreg Roach 3876692c8bSGreg Roach /** @var Menu[] An optional list of sub-menus. */ 39a25f0a04SGreg Roach private $submenus; 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** 42a25f0a04SGreg Roach * Constructor for the menu class 43a25f0a04SGreg Roach * 44a25f0a04SGreg Roach * @param string $label The label for the menu item 45a25f0a04SGreg Roach * @param string $link The target URL 463cf92ae2SGreg Roach * @param string $class A CSS class 473cf92ae2SGreg Roach * @param string[] $attrs Optional attributes, such as onclick or data-xxx 48a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 49a25f0a04SGreg Roach */ 50c1010edaSGreg Roach public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = []) 51c1010edaSGreg Roach { 52a25f0a04SGreg Roach $this 53a25f0a04SGreg Roach ->setLabel($label) 54a25f0a04SGreg Roach ->setLink($link) 553d0a6fa1SGreg Roach ->setClass($class) 563cf92ae2SGreg Roach ->setAttrs($attrs) 57a25f0a04SGreg Roach ->setSubmenus($submenus); 58a25f0a04SGreg Roach } 59a25f0a04SGreg Roach 60a25f0a04SGreg Roach /** 6115d603e7SGreg Roach * Render this menu using Bootstrap4 markup 62a25f0a04SGreg Roach * 63a25f0a04SGreg Roach * @return string 64a25f0a04SGreg Roach */ 65e364afe4SGreg Roach public function bootstrap4(): string 66c1010edaSGreg Roach { 6726eae716SGreg Roach if (!empty($this->submenus)) { 68a25f0a04SGreg Roach $submenus = ''; 69a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 7015d603e7SGreg Roach $attrs = ''; 7115d603e7SGreg Roach foreach ($submenu->attrs as $key => $value) { 72d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 73a25f0a04SGreg Roach } 74a25f0a04SGreg Roach 7515d603e7SGreg Roach $class = trim('dropdown-item ' . $submenu->class); 7625b2dde3SGreg Roach $submenus .= '<a class="' . $class . '" href="' . e($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>'; 90b2ce94c6SRico Sonntag } 91b2ce94c6SRico Sonntag 923cf92ae2SGreg Roach $attrs = ''; 933cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 94d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 95a25f0a04SGreg Roach } 96a25f0a04SGreg Roach 9715d603e7SGreg Roach $class = trim('nav-item ' . $this->class); 9815d603e7SGreg Roach 9925b2dde3SGreg Roach return '<li class="' . $class . '"><a class="nav-link" href="' . e($this->link) . '"' . $attrs . '>' . $this->label . '</a></li>'; 100a25f0a04SGreg Roach } 101a25f0a04SGreg Roach 102a25f0a04SGreg Roach /** 1033cf92ae2SGreg Roach * Get the optional attributes. 1043cf92ae2SGreg Roach * 1053cf92ae2SGreg Roach * @return string[] 1063cf92ae2SGreg Roach */ 1078f53f488SRico Sonntag public function getAttrs(): array 108c1010edaSGreg Roach { 1093cf92ae2SGreg Roach return $this->attrs; 1103cf92ae2SGreg Roach } 1113cf92ae2SGreg Roach 1123cf92ae2SGreg Roach /** 1133cf92ae2SGreg Roach * Set the optional attributes. 1143cf92ae2SGreg Roach * 1153cf92ae2SGreg Roach * @param string[] $attrs 1163cf92ae2SGreg Roach * 1173cf92ae2SGreg Roach * @return $this 1183cf92ae2SGreg Roach */ 1198f53f488SRico Sonntag public function setAttrs(array $attrs): self 120c1010edaSGreg Roach { 1213cf92ae2SGreg Roach $this->attrs = $attrs; 1223cf92ae2SGreg Roach 1233cf92ae2SGreg Roach return $this; 1243cf92ae2SGreg Roach } 1253cf92ae2SGreg Roach 1263cf92ae2SGreg Roach /** 12776692c8bSGreg Roach * Get the class. 12876692c8bSGreg Roach * 129a25f0a04SGreg Roach * @return string 130a25f0a04SGreg Roach */ 1318f53f488SRico Sonntag public function getClass(): string 132c1010edaSGreg Roach { 1333d0a6fa1SGreg Roach return $this->class; 134a25f0a04SGreg Roach } 135a25f0a04SGreg Roach 136a25f0a04SGreg Roach /** 13776692c8bSGreg Roach * Set the class. 13876692c8bSGreg Roach * 1393d0a6fa1SGreg Roach * @param string $class 140a25f0a04SGreg Roach * 141a25f0a04SGreg Roach * @return $this 142a25f0a04SGreg Roach */ 1438f53f488SRico Sonntag public function setClass($class): self 144c1010edaSGreg Roach { 1453d0a6fa1SGreg Roach $this->class = $class; 146a25f0a04SGreg Roach 147a25f0a04SGreg Roach return $this; 148a25f0a04SGreg Roach } 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach /** 15176692c8bSGreg Roach * Get the label. 15276692c8bSGreg Roach * 153a25f0a04SGreg Roach * @return string 154a25f0a04SGreg Roach */ 1558f53f488SRico Sonntag public function getLabel(): string 156c1010edaSGreg Roach { 157a25f0a04SGreg Roach return $this->label; 158a25f0a04SGreg Roach } 159a25f0a04SGreg Roach 160a25f0a04SGreg Roach /** 16176692c8bSGreg Roach * Set the label. 16276692c8bSGreg Roach * 163a25f0a04SGreg Roach * @param string $label 164a25f0a04SGreg Roach * 165a25f0a04SGreg Roach * @return $this 166a25f0a04SGreg Roach */ 1678f53f488SRico Sonntag public function setLabel($label): self 168c1010edaSGreg Roach { 169a25f0a04SGreg Roach $this->label = $label; 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach return $this; 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach 174a25f0a04SGreg Roach /** 17576692c8bSGreg Roach * Get the link. 1763cf92ae2SGreg Roach * 177a25f0a04SGreg Roach * @return string 178a25f0a04SGreg Roach */ 1798f53f488SRico Sonntag public function getLink(): string 180c1010edaSGreg Roach { 181a25f0a04SGreg Roach return $this->link; 182a25f0a04SGreg Roach } 183a25f0a04SGreg Roach 184a25f0a04SGreg Roach /** 18576692c8bSGreg Roach * Set the link. 18676692c8bSGreg Roach * 187a25f0a04SGreg Roach * @param string $link 188a25f0a04SGreg Roach * 189a25f0a04SGreg Roach * @return $this 190a25f0a04SGreg Roach */ 1918f53f488SRico Sonntag public function setLink($link): self 192c1010edaSGreg Roach { 193a25f0a04SGreg Roach $this->link = $link; 194a25f0a04SGreg Roach 195a25f0a04SGreg Roach return $this; 196a25f0a04SGreg Roach } 197a25f0a04SGreg Roach 198a25f0a04SGreg Roach /** 199a25f0a04SGreg Roach * Add a submenu to this menu 200a25f0a04SGreg Roach * 201ecb5b5e9SGreg Roach * @param Menu $menu 202ecb5b5e9SGreg Roach * 203ecb5b5e9SGreg Roach * @return $this 204a25f0a04SGreg Roach */ 2058f53f488SRico Sonntag public function addSubmenu($menu): self 206c1010edaSGreg Roach { 207a25f0a04SGreg Roach $this->submenus[] = $menu; 208ecb5b5e9SGreg Roach 209ecb5b5e9SGreg Roach return $this; 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach 212a25f0a04SGreg Roach /** 21376692c8bSGreg Roach * Get the sub-menus. 21476692c8bSGreg Roach * 215a25f0a04SGreg Roach * @return Menu[] 216a25f0a04SGreg Roach */ 2178f53f488SRico Sonntag public function getSubmenus(): array 218c1010edaSGreg Roach { 219a25f0a04SGreg Roach return $this->submenus; 220a25f0a04SGreg Roach } 221a25f0a04SGreg Roach 222a25f0a04SGreg Roach /** 22376692c8bSGreg Roach * Set the sub-menus. 22476692c8bSGreg Roach * 225a25f0a04SGreg Roach * @param Menu[] $submenus 226a25f0a04SGreg Roach * 227a25f0a04SGreg Roach * @return $this 228a25f0a04SGreg Roach */ 2298f53f488SRico Sonntag public function setSubmenus(array $submenus): self 230c1010edaSGreg Roach { 231a25f0a04SGreg Roach $this->submenus = $submenus; 232a25f0a04SGreg Roach 233a25f0a04SGreg Roach return $this; 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach} 236