1a25f0a04SGreg Roach<?php 23976b470SGreg 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 */ 17*fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 22a25f0a04SGreg Roach/** 2376692c8bSGreg Roach * System for generating menus. 24a25f0a04SGreg Roach */ 25c1010edaSGreg Roachclass Menu 26c1010edaSGreg Roach{ 27a25f0a04SGreg Roach /** @var string The text to be displayed in the mneu */ 28a25f0a04SGreg Roach private $label; 29a25f0a04SGreg Roach 30a25f0a04SGreg Roach /** @var string The target URL or href */ 31a25f0a04SGreg Roach private $link; 32a25f0a04SGreg Roach 333d0a6fa1SGreg Roach /** @var string The CSS class used to style this menu item */ 343d0a6fa1SGreg Roach private $class; 35a25f0a04SGreg Roach 363cf92ae2SGreg Roach /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */ 373cf92ae2SGreg Roach private $attrs; 38a25f0a04SGreg Roach 3976692c8bSGreg Roach /** @var Menu[] An optional list of sub-menus. */ 40a25f0a04SGreg Roach private $submenus; 41a25f0a04SGreg Roach 42a25f0a04SGreg Roach /** 43a25f0a04SGreg Roach * Constructor for the menu class 44a25f0a04SGreg Roach * 45a25f0a04SGreg Roach * @param string $label The label for the menu item 46a25f0a04SGreg Roach * @param string $link The target URL 473cf92ae2SGreg Roach * @param string $class A CSS class 483cf92ae2SGreg Roach * @param string[] $attrs Optional attributes, such as onclick or data-xxx 49a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 50a25f0a04SGreg Roach */ 51c1010edaSGreg Roach public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = []) 52c1010edaSGreg Roach { 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 */ 66e364afe4SGreg Roach public function bootstrap4(): string 67c1010edaSGreg Roach { 6826eae716SGreg Roach if (!empty($this->submenus)) { 69a25f0a04SGreg Roach $submenus = ''; 70a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 7115d603e7SGreg Roach $attrs = ''; 7215d603e7SGreg Roach foreach ($submenu->attrs as $key => $value) { 73d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 74a25f0a04SGreg Roach } 75a25f0a04SGreg Roach 7615d603e7SGreg Roach $class = trim('dropdown-item ' . $submenu->class); 7725b2dde3SGreg Roach $submenus .= '<a class="' . $class . '" href="' . e($submenu->link) . '"' . $attrs . '>' . $submenu->label . '</a>'; 7815d603e7SGreg Roach } 7915d603e7SGreg Roach 8015d603e7SGreg Roach $class = trim('nav-item dropdown ' . $this->class); 8115d603e7SGreg Roach 82a25f0a04SGreg Roach return 8315d603e7SGreg Roach '<li class="' . $class . '">' . 8415d603e7SGreg Roach '<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 85a25f0a04SGreg Roach $this->label . 86a25f0a04SGreg Roach '<span class="caret"></span></a>' . 8715d603e7SGreg Roach '<div class="dropdown-menu" role="menu">' . 88a25f0a04SGreg Roach $submenus . 8915d603e7SGreg Roach '</div>' . 90a25f0a04SGreg Roach '</li>'; 91b2ce94c6SRico Sonntag } 92b2ce94c6SRico Sonntag 933cf92ae2SGreg Roach $attrs = ''; 943cf92ae2SGreg Roach foreach ($this->attrs as $key => $value) { 95d53324c9SGreg Roach $attrs .= ' ' . $key . '="' . e($value) . '"'; 96a25f0a04SGreg Roach } 97a25f0a04SGreg Roach 9815d603e7SGreg Roach $class = trim('nav-item ' . $this->class); 9915d603e7SGreg Roach 10025b2dde3SGreg Roach return '<li class="' . $class . '"><a class="nav-link" href="' . e($this->link) . '"' . $attrs . '>' . $this->label . '</a></li>'; 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach 103a25f0a04SGreg Roach /** 1043cf92ae2SGreg Roach * Get the optional attributes. 1053cf92ae2SGreg Roach * 1063cf92ae2SGreg Roach * @return string[] 1073cf92ae2SGreg Roach */ 1088f53f488SRico Sonntag public function getAttrs(): array 109c1010edaSGreg Roach { 1103cf92ae2SGreg Roach return $this->attrs; 1113cf92ae2SGreg Roach } 1123cf92ae2SGreg Roach 1133cf92ae2SGreg Roach /** 1143cf92ae2SGreg Roach * Set the optional attributes. 1153cf92ae2SGreg Roach * 1163cf92ae2SGreg Roach * @param string[] $attrs 1173cf92ae2SGreg Roach * 1183cf92ae2SGreg Roach * @return $this 1193cf92ae2SGreg Roach */ 1208f53f488SRico Sonntag public function setAttrs(array $attrs): self 121c1010edaSGreg Roach { 1223cf92ae2SGreg Roach $this->attrs = $attrs; 1233cf92ae2SGreg Roach 1243cf92ae2SGreg Roach return $this; 1253cf92ae2SGreg Roach } 1263cf92ae2SGreg Roach 1273cf92ae2SGreg Roach /** 12876692c8bSGreg Roach * Get the class. 12976692c8bSGreg Roach * 130a25f0a04SGreg Roach * @return string 131a25f0a04SGreg Roach */ 1328f53f488SRico Sonntag public function getClass(): string 133c1010edaSGreg Roach { 1343d0a6fa1SGreg Roach return $this->class; 135a25f0a04SGreg Roach } 136a25f0a04SGreg Roach 137a25f0a04SGreg Roach /** 13876692c8bSGreg Roach * Set the class. 13976692c8bSGreg Roach * 1403d0a6fa1SGreg Roach * @param string $class 141a25f0a04SGreg Roach * 142a25f0a04SGreg Roach * @return $this 143a25f0a04SGreg Roach */ 1448f53f488SRico Sonntag public function setClass($class): self 145c1010edaSGreg Roach { 1463d0a6fa1SGreg Roach $this->class = $class; 147a25f0a04SGreg Roach 148a25f0a04SGreg Roach return $this; 149a25f0a04SGreg Roach } 150a25f0a04SGreg Roach 151a25f0a04SGreg Roach /** 15276692c8bSGreg Roach * Get the label. 15376692c8bSGreg Roach * 154a25f0a04SGreg Roach * @return string 155a25f0a04SGreg Roach */ 1568f53f488SRico Sonntag public function getLabel(): string 157c1010edaSGreg Roach { 158a25f0a04SGreg Roach return $this->label; 159a25f0a04SGreg Roach } 160a25f0a04SGreg Roach 161a25f0a04SGreg Roach /** 16276692c8bSGreg Roach * Set the label. 16376692c8bSGreg Roach * 164a25f0a04SGreg Roach * @param string $label 165a25f0a04SGreg Roach * 166a25f0a04SGreg Roach * @return $this 167a25f0a04SGreg Roach */ 1688f53f488SRico Sonntag public function setLabel($label): self 169c1010edaSGreg Roach { 170a25f0a04SGreg Roach $this->label = $label; 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach return $this; 173a25f0a04SGreg Roach } 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach /** 17676692c8bSGreg Roach * Get the link. 1773cf92ae2SGreg Roach * 178a25f0a04SGreg Roach * @return string 179a25f0a04SGreg Roach */ 1808f53f488SRico Sonntag public function getLink(): string 181c1010edaSGreg Roach { 182a25f0a04SGreg Roach return $this->link; 183a25f0a04SGreg Roach } 184a25f0a04SGreg Roach 185a25f0a04SGreg Roach /** 18676692c8bSGreg Roach * Set the link. 18776692c8bSGreg Roach * 188a25f0a04SGreg Roach * @param string $link 189a25f0a04SGreg Roach * 190a25f0a04SGreg Roach * @return $this 191a25f0a04SGreg Roach */ 1928f53f488SRico Sonntag public function setLink($link): self 193c1010edaSGreg Roach { 194a25f0a04SGreg Roach $this->link = $link; 195a25f0a04SGreg Roach 196a25f0a04SGreg Roach return $this; 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach 199a25f0a04SGreg Roach /** 200a25f0a04SGreg Roach * Add a submenu to this menu 201a25f0a04SGreg Roach * 202ecb5b5e9SGreg Roach * @param Menu $menu 203ecb5b5e9SGreg Roach * 204ecb5b5e9SGreg Roach * @return $this 205a25f0a04SGreg Roach */ 2068f53f488SRico Sonntag public function addSubmenu($menu): self 207c1010edaSGreg Roach { 208a25f0a04SGreg Roach $this->submenus[] = $menu; 209ecb5b5e9SGreg Roach 210ecb5b5e9SGreg Roach return $this; 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach 213a25f0a04SGreg Roach /** 21476692c8bSGreg Roach * Get the sub-menus. 21576692c8bSGreg Roach * 216a25f0a04SGreg Roach * @return Menu[] 217a25f0a04SGreg Roach */ 2188f53f488SRico Sonntag public function getSubmenus(): array 219c1010edaSGreg Roach { 220a25f0a04SGreg Roach return $this->submenus; 221a25f0a04SGreg Roach } 222a25f0a04SGreg Roach 223a25f0a04SGreg Roach /** 22476692c8bSGreg Roach * Set the sub-menus. 22576692c8bSGreg Roach * 226a25f0a04SGreg Roach * @param Menu[] $submenus 227a25f0a04SGreg Roach * 228a25f0a04SGreg Roach * @return $this 229a25f0a04SGreg Roach */ 2308f53f488SRico Sonntag public function setSubmenus(array $submenus): self 231c1010edaSGreg Roach { 232a25f0a04SGreg Roach $this->submenus = $submenus; 233a25f0a04SGreg Roach 234a25f0a04SGreg Roach return $this; 235a25f0a04SGreg Roach } 236a25f0a04SGreg Roach} 237