1a25f0a04SGreg Roach<?php 2dd04c183SGreg Roachnamespace Fisharebest\Webtrees; 3a25f0a04SGreg Roach 4a25f0a04SGreg Roach/** 5a25f0a04SGreg Roach * webtrees: online genealogy 6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team 7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 10a25f0a04SGreg Roach * (at your option) any later version. 11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14a25f0a04SGreg Roach * GNU General Public License for more details. 15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 17a25f0a04SGreg Roach */ 18a25f0a04SGreg Roach 19*fee045eeSGreg Roachuse Rhumsaa\Uuid\Uuid; 20*fee045eeSGreg Roach 21a25f0a04SGreg Roach/** 22a25f0a04SGreg Roach * Class Menu - System for generating menus. 23a25f0a04SGreg Roach */ 24a25f0a04SGreg Roachclass Menu { 25a25f0a04SGreg Roach /** @var string The text to be displayed in the mneu */ 26a25f0a04SGreg Roach private $label; 27a25f0a04SGreg Roach 28a25f0a04SGreg Roach /** @var string The target URL or href*/ 29a25f0a04SGreg Roach private $link; 30a25f0a04SGreg Roach 313d0a6fa1SGreg Roach /** @var string The CSS class used to style this menu item */ 323d0a6fa1SGreg Roach private $class; 33a25f0a04SGreg Roach 34a25f0a04SGreg Roach /** @var string An onclick action, typically used with a link of "#" */ 35a25f0a04SGreg Roach private $onclick; 36a25f0a04SGreg Roach 37a25f0a04SGreg Roach /** @var Menu[] */ 38a25f0a04SGreg Roach private $submenus; 39a25f0a04SGreg Roach 40a25f0a04SGreg Roach /** @var string Used internally to create javascript menus */ 41a25f0a04SGreg Roach private $parentmenu; 42a25f0a04SGreg Roach 43a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 44a25f0a04SGreg Roach private $submenuclass; 45a25f0a04SGreg Roach 46a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 47a25f0a04SGreg Roach private $iconclass; 48a25f0a04SGreg Roach 49a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 503d0a6fa1SGreg Roach private $menuclass; 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach /** 53a25f0a04SGreg Roach * Constructor for the menu class 54a25f0a04SGreg Roach * 55a25f0a04SGreg Roach * @param string $label The label for the menu item 56a25f0a04SGreg Roach * @param string $link The target URL 573d0a6fa1SGreg Roach * @param string $class An CSS class 58a25f0a04SGreg Roach * @param string $onclick A javascript onclick handler 59a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 60a25f0a04SGreg Roach */ 613d0a6fa1SGreg Roach public function __construct($label, $link = '#', $class = '', $onclick = '', $submenus = array()) { 62a25f0a04SGreg Roach $this 63a25f0a04SGreg Roach ->setLabel($label) 64a25f0a04SGreg Roach ->setLink($link) 653d0a6fa1SGreg Roach ->setClass($class) 66a25f0a04SGreg Roach ->setOnclick($onclick) 67a25f0a04SGreg Roach ->setSubmenus($submenus); 68a25f0a04SGreg Roach } 69a25f0a04SGreg Roach 70a25f0a04SGreg Roach /** 71a25f0a04SGreg Roach * Convert this menu to an HTML list, for easy rendering of 72a25f0a04SGreg Roach * lists of menus/nulls. 73a25f0a04SGreg Roach * 74a25f0a04SGreg Roach * @return string 75a25f0a04SGreg Roach */ 76a25f0a04SGreg Roach public function __toString() { 77a25f0a04SGreg Roach return $this->getMenuAsList(); 78a25f0a04SGreg Roach } 79a25f0a04SGreg Roach 80a25f0a04SGreg Roach /** 81a25f0a04SGreg Roach * Render this menu using Bootstrap markup 82a25f0a04SGreg Roach * 83a25f0a04SGreg Roach * @return string 84a25f0a04SGreg Roach */ 85a25f0a04SGreg Roach public function bootstrap() { 86a25f0a04SGreg Roach if ($this->submenus) { 87a25f0a04SGreg Roach $submenus = ''; 88a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 89a25f0a04SGreg Roach $submenus .= $submenu->bootstrap(); 90a25f0a04SGreg Roach } 91a25f0a04SGreg Roach 92a25f0a04SGreg Roach return 933d0a6fa1SGreg Roach '<li class="' . $this->class . ' dropdown">' . 94a25f0a04SGreg Roach '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 95a25f0a04SGreg Roach $this->label . 96a25f0a04SGreg Roach ' <span class="caret"></span></a>' . 97a25f0a04SGreg Roach '<ul class="dropdown-menu" role="menu">' . 98a25f0a04SGreg Roach $submenus . 99a25f0a04SGreg Roach '</ul>' . 100a25f0a04SGreg Roach '</li>'; 101a25f0a04SGreg Roach } else { 102a25f0a04SGreg Roach if ($this->onclick) { 103a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 104a25f0a04SGreg Roach } else { 105a25f0a04SGreg Roach $onclick = ''; 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach 1083d0a6fa1SGreg Roach return '<li class="' . $this->class . '"><a href="' . $this->link . '"' . $onclick . '>' . $this->label . '</a></li>'; 109a25f0a04SGreg Roach } 110a25f0a04SGreg Roach } 111a25f0a04SGreg Roach 112a25f0a04SGreg Roach /** 1133d0a6fa1SGreg Roach * Set the CSS classes for the (legacy) javascript menus 114a25f0a04SGreg Roach * 115794c6b5bSGreg Roach * @param string $menuclass 116a25f0a04SGreg Roach * @param string $submenuclass 117a25f0a04SGreg Roach * @param string $iconclass 118a25f0a04SGreg Roach */ 1193d0a6fa1SGreg Roach public function addClass($menuclass, $submenuclass = '', $iconclass = '') { 1203d0a6fa1SGreg Roach $this->menuclass = $menuclass; 121a25f0a04SGreg Roach $this->submenuclass = $submenuclass; 122a25f0a04SGreg Roach $this->iconclass = $iconclass; 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach 125a25f0a04SGreg Roach /** 126a25f0a04SGreg Roach * @return string 127a25f0a04SGreg Roach */ 1283d0a6fa1SGreg Roach public function getClass() { 1293d0a6fa1SGreg Roach return $this->class; 130a25f0a04SGreg Roach } 131a25f0a04SGreg Roach 132a25f0a04SGreg Roach /** 1333d0a6fa1SGreg Roach * @param string $class 134a25f0a04SGreg Roach * 135a25f0a04SGreg Roach * @return $this 136a25f0a04SGreg Roach */ 1373d0a6fa1SGreg Roach public function setClass($class) { 1383d0a6fa1SGreg Roach $this->class = $class; 139a25f0a04SGreg Roach 140a25f0a04SGreg Roach return $this; 141a25f0a04SGreg Roach } 142a25f0a04SGreg Roach 143a25f0a04SGreg Roach /** 144a25f0a04SGreg Roach * @return string 145a25f0a04SGreg Roach */ 146a25f0a04SGreg Roach public function getLabel() { 147a25f0a04SGreg Roach return $this->label; 148a25f0a04SGreg Roach } 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach /** 151a25f0a04SGreg Roach * @param string $label 152a25f0a04SGreg Roach * 153a25f0a04SGreg Roach * @return $this 154a25f0a04SGreg Roach */ 155a25f0a04SGreg Roach public function setLabel($label) { 156a25f0a04SGreg Roach $this->label = $label; 157a25f0a04SGreg Roach 158a25f0a04SGreg Roach return $this; 159a25f0a04SGreg Roach } 160a25f0a04SGreg Roach 161a25f0a04SGreg Roach /** 162a25f0a04SGreg Roach * @return string 163a25f0a04SGreg Roach */ 164a25f0a04SGreg Roach public function getLink() { 165a25f0a04SGreg Roach return $this->link; 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach 168a25f0a04SGreg Roach /** 169a25f0a04SGreg Roach * @param string $link 170a25f0a04SGreg Roach * 171a25f0a04SGreg Roach * @return $this 172a25f0a04SGreg Roach */ 173a25f0a04SGreg Roach public function setLink($link) { 174a25f0a04SGreg Roach $this->link = $link; 175a25f0a04SGreg Roach 176a25f0a04SGreg Roach return $this; 177a25f0a04SGreg Roach } 178a25f0a04SGreg Roach 179a25f0a04SGreg Roach /** 180a25f0a04SGreg Roach * @return string 181a25f0a04SGreg Roach */ 182a25f0a04SGreg Roach public function getOnclick() { 183a25f0a04SGreg Roach return $this->onclick; 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach 186a25f0a04SGreg Roach /** 187a25f0a04SGreg Roach * @param string $onclick 188a25f0a04SGreg Roach * 189a25f0a04SGreg Roach * @return $this 190a25f0a04SGreg Roach */ 191a25f0a04SGreg Roach public function setOnclick($onclick) { 192a25f0a04SGreg Roach $this->onclick = $onclick; 193a25f0a04SGreg Roach 194a25f0a04SGreg Roach return $this; 195a25f0a04SGreg Roach } 196a25f0a04SGreg Roach 197a25f0a04SGreg Roach /** 198a25f0a04SGreg Roach * Add a submenu to this menu 199a25f0a04SGreg Roach * 200ecb5b5e9SGreg Roach * @param Menu $menu 201ecb5b5e9SGreg Roach * 202ecb5b5e9SGreg Roach * @return $this 203a25f0a04SGreg Roach */ 204a25f0a04SGreg Roach public function addSubmenu($menu) { 205a25f0a04SGreg Roach $this->submenus[] = $menu; 206ecb5b5e9SGreg Roach 207ecb5b5e9SGreg Roach return $this; 208a25f0a04SGreg Roach } 209a25f0a04SGreg Roach 210a25f0a04SGreg Roach /** 211a25f0a04SGreg Roach * Render this menu using javascript popups.. 212a25f0a04SGreg Roach * 213a25f0a04SGreg Roach * @return string 214a25f0a04SGreg Roach */ 215a25f0a04SGreg Roach public function getMenu() { 216*fee045eeSGreg Roach $menu_id = 'menu-' . Uuid::uuid4(); 217*fee045eeSGreg Roach $sub_menu_id = 'sub-' . $menu_id; 218a25f0a04SGreg Roach 219*fee045eeSGreg Roach $html = '<a href="' . $this->link . '"'; 220a25f0a04SGreg Roach if ($this->onclick) { 221*fee045eeSGreg Roach $html .= ' onclick="' . $this->onclick . '"'; 222a25f0a04SGreg Roach } 223*fee045eeSGreg Roach if (!empty($this->submenus)) { 224*fee045eeSGreg Roach $html .= ' onmouseover="show_submenu(\'' . $sub_menu_id . '\', \'' . $menu_id . '\');"'; 225*fee045eeSGreg Roach $html .= ' onmouseout="timeout_submenu(\'' . $sub_menu_id . '\');"'; 226*fee045eeSGreg Roach } 227*fee045eeSGreg Roach $html .= '>' . $this->label . '</a>'; 228a25f0a04SGreg Roach 229*fee045eeSGreg Roach if (!empty($this->submenus)) { 230*fee045eeSGreg Roach $html .= '<div id="' . $sub_menu_id . '" class="' . $this->submenuclass . '"'; 231*fee045eeSGreg Roach $html .= ' style="position: absolute; visibility: hidden; z-index: 100; text-align: ' . (I18N::direction() === 'ltr' ? 'left' : 'right') . '"'; 232*fee045eeSGreg Roach $html .= ' onmouseover="show_submenu(\'' . $this->parentmenu . '\'); show_submenu(\'' . $sub_menu_id . '\');"'; 233*fee045eeSGreg Roach $html .= ' onmouseout="timeout_submenu(\'' . $sub_menu_id . '\');">'; 234a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 235*fee045eeSGreg Roach $submenu->parentmenu = $sub_menu_id; 236*fee045eeSGreg Roach $html .= $submenu->getMenu(); 237a25f0a04SGreg Roach } 238*fee045eeSGreg Roach $html .= '</div></div>'; 239a25f0a04SGreg Roach } 240a25f0a04SGreg Roach 241*fee045eeSGreg Roach return '<div id="' . $menu_id . '" class="' . $this->menuclass . '">' . $html . '</div>'; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach 244a25f0a04SGreg Roach /** 245a25f0a04SGreg Roach * Render this menu as an HTML list 246a25f0a04SGreg Roach * 247a25f0a04SGreg Roach * @return string 248a25f0a04SGreg Roach */ 249a25f0a04SGreg Roach public function getMenuAsList() { 250a25f0a04SGreg Roach if ($this->onclick) { 251a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 252a25f0a04SGreg Roach } else { 253a25f0a04SGreg Roach $onclick = ''; 254a25f0a04SGreg Roach } 255a25f0a04SGreg Roach if ($this->link) { 256a25f0a04SGreg Roach $link = ' href="' . $this->link . '"'; 257a25f0a04SGreg Roach } else { 258a25f0a04SGreg Roach $link = ''; 259a25f0a04SGreg Roach } 2603d0a6fa1SGreg Roach $html = '<a' . $link . $onclick . '>' . $this->label . '</a>'; 261a25f0a04SGreg Roach if ($this->submenus) { 262a25f0a04SGreg Roach $html .= '<ul>'; 263a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 264a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach $html .= '</ul>'; 267a25f0a04SGreg Roach } 268a25f0a04SGreg Roach 2693d0a6fa1SGreg Roach return '<li class="' . $this->class . '">' . $html . '</li>'; 270a25f0a04SGreg Roach } 271a25f0a04SGreg Roach 272a25f0a04SGreg Roach /** 273a25f0a04SGreg Roach * @return Menu[] 274a25f0a04SGreg Roach */ 275a25f0a04SGreg Roach public function getSubmenus() { 276a25f0a04SGreg Roach return $this->submenus; 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach 279a25f0a04SGreg Roach /** 280a25f0a04SGreg Roach * @param Menu[] $submenus 281a25f0a04SGreg Roach * 282a25f0a04SGreg Roach * @return $this 283a25f0a04SGreg Roach */ 284a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 285a25f0a04SGreg Roach $this->submenus = $submenus; 286a25f0a04SGreg Roach 287a25f0a04SGreg Roach return $this; 288a25f0a04SGreg Roach } 289a25f0a04SGreg Roach} 290