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 19fee045eeSGreg Roachuse Rhumsaa\Uuid\Uuid; 20fee045eeSGreg 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 */ 473d0a6fa1SGreg Roach private $menuclass; 48a25f0a04SGreg Roach 49a25f0a04SGreg Roach /** 50a25f0a04SGreg Roach * Constructor for the menu class 51a25f0a04SGreg Roach * 52a25f0a04SGreg Roach * @param string $label The label for the menu item 53a25f0a04SGreg Roach * @param string $link The target URL 543d0a6fa1SGreg Roach * @param string $class An CSS class 55a25f0a04SGreg Roach * @param string $onclick A javascript onclick handler 56a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 57a25f0a04SGreg Roach */ 583d0a6fa1SGreg Roach public function __construct($label, $link = '#', $class = '', $onclick = '', $submenus = array()) { 59a25f0a04SGreg Roach $this 60a25f0a04SGreg Roach ->setLabel($label) 61a25f0a04SGreg Roach ->setLink($link) 623d0a6fa1SGreg Roach ->setClass($class) 63a25f0a04SGreg Roach ->setOnclick($onclick) 64a25f0a04SGreg Roach ->setSubmenus($submenus); 65a25f0a04SGreg Roach } 66a25f0a04SGreg Roach 67a25f0a04SGreg Roach /** 68a25f0a04SGreg Roach * Convert this menu to an HTML list, for easy rendering of 69a25f0a04SGreg Roach * lists of menus/nulls. 70a25f0a04SGreg Roach * 71a25f0a04SGreg Roach * @return string 72a25f0a04SGreg Roach */ 73a25f0a04SGreg Roach public function __toString() { 74a25f0a04SGreg Roach return $this->getMenuAsList(); 75a25f0a04SGreg Roach } 76a25f0a04SGreg Roach 77a25f0a04SGreg Roach /** 78a25f0a04SGreg Roach * Render this menu using Bootstrap markup 79a25f0a04SGreg Roach * 80a25f0a04SGreg Roach * @return string 81a25f0a04SGreg Roach */ 82a25f0a04SGreg Roach public function bootstrap() { 83a25f0a04SGreg Roach if ($this->submenus) { 84a25f0a04SGreg Roach $submenus = ''; 85a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 86a25f0a04SGreg Roach $submenus .= $submenu->bootstrap(); 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 89a25f0a04SGreg Roach return 903d0a6fa1SGreg Roach '<li class="' . $this->class . ' dropdown">' . 91a25f0a04SGreg Roach '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 92a25f0a04SGreg Roach $this->label . 93a25f0a04SGreg Roach ' <span class="caret"></span></a>' . 94a25f0a04SGreg Roach '<ul class="dropdown-menu" role="menu">' . 95a25f0a04SGreg Roach $submenus . 96a25f0a04SGreg Roach '</ul>' . 97a25f0a04SGreg Roach '</li>'; 98a25f0a04SGreg Roach } else { 99a25f0a04SGreg Roach if ($this->onclick) { 100a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 101a25f0a04SGreg Roach } else { 102a25f0a04SGreg Roach $onclick = ''; 103a25f0a04SGreg Roach } 104a25f0a04SGreg Roach 1053d0a6fa1SGreg Roach return '<li class="' . $this->class . '"><a href="' . $this->link . '"' . $onclick . '>' . $this->label . '</a></li>'; 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach } 108a25f0a04SGreg Roach 109a25f0a04SGreg Roach /** 1103d0a6fa1SGreg Roach * Set the CSS classes for the (legacy) javascript menus 111a25f0a04SGreg Roach * 112794c6b5bSGreg Roach * @param string $menuclass 113a25f0a04SGreg Roach * @param string $submenuclass 114a25f0a04SGreg Roach */ 115*cfd4d92fSGreg Roach public function addClass($menuclass, $submenuclass = '') { 1163d0a6fa1SGreg Roach $this->menuclass = $menuclass; 117a25f0a04SGreg Roach $this->submenuclass = $submenuclass; 118a25f0a04SGreg Roach } 119a25f0a04SGreg Roach 120a25f0a04SGreg Roach /** 121a25f0a04SGreg Roach * @return string 122a25f0a04SGreg Roach */ 1233d0a6fa1SGreg Roach public function getClass() { 1243d0a6fa1SGreg Roach return $this->class; 125a25f0a04SGreg Roach } 126a25f0a04SGreg Roach 127a25f0a04SGreg Roach /** 1283d0a6fa1SGreg Roach * @param string $class 129a25f0a04SGreg Roach * 130a25f0a04SGreg Roach * @return $this 131a25f0a04SGreg Roach */ 1323d0a6fa1SGreg Roach public function setClass($class) { 1333d0a6fa1SGreg Roach $this->class = $class; 134a25f0a04SGreg Roach 135a25f0a04SGreg Roach return $this; 136a25f0a04SGreg Roach } 137a25f0a04SGreg Roach 138a25f0a04SGreg Roach /** 139a25f0a04SGreg Roach * @return string 140a25f0a04SGreg Roach */ 141a25f0a04SGreg Roach public function getLabel() { 142a25f0a04SGreg Roach return $this->label; 143a25f0a04SGreg Roach } 144a25f0a04SGreg Roach 145a25f0a04SGreg Roach /** 146a25f0a04SGreg Roach * @param string $label 147a25f0a04SGreg Roach * 148a25f0a04SGreg Roach * @return $this 149a25f0a04SGreg Roach */ 150a25f0a04SGreg Roach public function setLabel($label) { 151a25f0a04SGreg Roach $this->label = $label; 152a25f0a04SGreg Roach 153a25f0a04SGreg Roach return $this; 154a25f0a04SGreg Roach } 155a25f0a04SGreg Roach 156a25f0a04SGreg Roach /** 157a25f0a04SGreg Roach * @return string 158a25f0a04SGreg Roach */ 159a25f0a04SGreg Roach public function getLink() { 160a25f0a04SGreg Roach return $this->link; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach 163a25f0a04SGreg Roach /** 164a25f0a04SGreg Roach * @param string $link 165a25f0a04SGreg Roach * 166a25f0a04SGreg Roach * @return $this 167a25f0a04SGreg Roach */ 168a25f0a04SGreg Roach public function setLink($link) { 169a25f0a04SGreg Roach $this->link = $link; 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach return $this; 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach 174a25f0a04SGreg Roach /** 175a25f0a04SGreg Roach * @return string 176a25f0a04SGreg Roach */ 177a25f0a04SGreg Roach public function getOnclick() { 178a25f0a04SGreg Roach return $this->onclick; 179a25f0a04SGreg Roach } 180a25f0a04SGreg Roach 181a25f0a04SGreg Roach /** 182a25f0a04SGreg Roach * @param string $onclick 183a25f0a04SGreg Roach * 184a25f0a04SGreg Roach * @return $this 185a25f0a04SGreg Roach */ 186a25f0a04SGreg Roach public function setOnclick($onclick) { 187a25f0a04SGreg Roach $this->onclick = $onclick; 188a25f0a04SGreg Roach 189a25f0a04SGreg Roach return $this; 190a25f0a04SGreg Roach } 191a25f0a04SGreg Roach 192a25f0a04SGreg Roach /** 193a25f0a04SGreg Roach * Add a submenu to this menu 194a25f0a04SGreg Roach * 195ecb5b5e9SGreg Roach * @param Menu $menu 196ecb5b5e9SGreg Roach * 197ecb5b5e9SGreg Roach * @return $this 198a25f0a04SGreg Roach */ 199a25f0a04SGreg Roach public function addSubmenu($menu) { 200a25f0a04SGreg Roach $this->submenus[] = $menu; 201ecb5b5e9SGreg Roach 202ecb5b5e9SGreg Roach return $this; 203a25f0a04SGreg Roach } 204a25f0a04SGreg Roach 205a25f0a04SGreg Roach /** 206a25f0a04SGreg Roach * Render this menu using javascript popups.. 207a25f0a04SGreg Roach * 208a25f0a04SGreg Roach * @return string 209a25f0a04SGreg Roach */ 210a25f0a04SGreg Roach public function getMenu() { 211fee045eeSGreg Roach $menu_id = 'menu-' . Uuid::uuid4(); 212fee045eeSGreg Roach $sub_menu_id = 'sub-' . $menu_id; 213a25f0a04SGreg Roach 214fee045eeSGreg Roach $html = '<a href="' . $this->link . '"'; 215a25f0a04SGreg Roach if ($this->onclick) { 216fee045eeSGreg Roach $html .= ' onclick="' . $this->onclick . '"'; 217a25f0a04SGreg Roach } 218fee045eeSGreg Roach if (!empty($this->submenus)) { 219fee045eeSGreg Roach $html .= ' onmouseover="show_submenu(\'' . $sub_menu_id . '\', \'' . $menu_id . '\');"'; 220fee045eeSGreg Roach $html .= ' onmouseout="timeout_submenu(\'' . $sub_menu_id . '\');"'; 221fee045eeSGreg Roach } 222fee045eeSGreg Roach $html .= '>' . $this->label . '</a>'; 223a25f0a04SGreg Roach 224fee045eeSGreg Roach if (!empty($this->submenus)) { 225fee045eeSGreg Roach $html .= '<div id="' . $sub_menu_id . '" class="' . $this->submenuclass . '"'; 226fee045eeSGreg Roach $html .= ' style="position: absolute; visibility: hidden; z-index: 100; text-align: ' . (I18N::direction() === 'ltr' ? 'left' : 'right') . '"'; 227fee045eeSGreg Roach $html .= ' onmouseover="show_submenu(\'' . $this->parentmenu . '\'); show_submenu(\'' . $sub_menu_id . '\');"'; 228fee045eeSGreg Roach $html .= ' onmouseout="timeout_submenu(\'' . $sub_menu_id . '\');">'; 229a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 230fee045eeSGreg Roach $submenu->parentmenu = $sub_menu_id; 231fee045eeSGreg Roach $html .= $submenu->getMenu(); 232a25f0a04SGreg Roach } 233fee045eeSGreg Roach $html .= '</div></div>'; 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach 236fee045eeSGreg Roach return '<div id="' . $menu_id . '" class="' . $this->menuclass . '">' . $html . '</div>'; 237a25f0a04SGreg Roach } 238a25f0a04SGreg Roach 239a25f0a04SGreg Roach /** 240a25f0a04SGreg Roach * Render this menu as an HTML list 241a25f0a04SGreg Roach * 242a25f0a04SGreg Roach * @return string 243a25f0a04SGreg Roach */ 244a25f0a04SGreg Roach public function getMenuAsList() { 245a25f0a04SGreg Roach if ($this->onclick) { 246a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 247a25f0a04SGreg Roach } else { 248a25f0a04SGreg Roach $onclick = ''; 249a25f0a04SGreg Roach } 250a25f0a04SGreg Roach if ($this->link) { 251a25f0a04SGreg Roach $link = ' href="' . $this->link . '"'; 252a25f0a04SGreg Roach } else { 253a25f0a04SGreg Roach $link = ''; 254a25f0a04SGreg Roach } 2553d0a6fa1SGreg Roach $html = '<a' . $link . $onclick . '>' . $this->label . '</a>'; 256a25f0a04SGreg Roach if ($this->submenus) { 257a25f0a04SGreg Roach $html .= '<ul>'; 258a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 259a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 260a25f0a04SGreg Roach } 261a25f0a04SGreg Roach $html .= '</ul>'; 262a25f0a04SGreg Roach } 263a25f0a04SGreg Roach 2643d0a6fa1SGreg Roach return '<li class="' . $this->class . '">' . $html . '</li>'; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach /** 268a25f0a04SGreg Roach * @return Menu[] 269a25f0a04SGreg Roach */ 270a25f0a04SGreg Roach public function getSubmenus() { 271a25f0a04SGreg Roach return $this->submenus; 272a25f0a04SGreg Roach } 273a25f0a04SGreg Roach 274a25f0a04SGreg Roach /** 275a25f0a04SGreg Roach * @param Menu[] $submenus 276a25f0a04SGreg Roach * 277a25f0a04SGreg Roach * @return $this 278a25f0a04SGreg Roach */ 279a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 280a25f0a04SGreg Roach $this->submenus = $submenus; 281a25f0a04SGreg Roach 282a25f0a04SGreg Roach return $this; 283a25f0a04SGreg Roach } 284a25f0a04SGreg Roach} 285