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 19a25f0a04SGreg Roach/** 20a25f0a04SGreg Roach * Class Menu - System for generating menus. 21a25f0a04SGreg Roach */ 22a25f0a04SGreg Roachclass Menu { 23a25f0a04SGreg Roach /** @var string The text to be displayed in the mneu */ 24a25f0a04SGreg Roach private $label; 25a25f0a04SGreg Roach 26a25f0a04SGreg Roach /** @var string The target URL or href*/ 27a25f0a04SGreg Roach private $link; 28a25f0a04SGreg Roach 29a25f0a04SGreg Roach /** @var string The CSS ID to be used for this menu item */ 30a25f0a04SGreg Roach private $id; 31a25f0a04SGreg Roach 32a25f0a04SGreg Roach /** @var string An onclick action, typically used with a link of "#" */ 33a25f0a04SGreg Roach private $onclick; 34a25f0a04SGreg Roach 35a25f0a04SGreg Roach /** @var Menu[] */ 36a25f0a04SGreg Roach private $submenus; 37a25f0a04SGreg Roach 38a25f0a04SGreg Roach /** @var string Used internally to create javascript menus */ 39a25f0a04SGreg Roach private $parentmenu; 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 42a25f0a04SGreg Roach private $submenuclass; 43a25f0a04SGreg Roach 44a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 45a25f0a04SGreg Roach private $iconclass; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var string Used to format javascript menus */ 48a25f0a04SGreg Roach private $class; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** 51a25f0a04SGreg Roach * Constructor for the menu class 52a25f0a04SGreg Roach * 53a25f0a04SGreg Roach * @param string $label The label for the menu item 54a25f0a04SGreg Roach * @param string $link The target URL 55a25f0a04SGreg Roach * @param string $id An CSS identifier 56a25f0a04SGreg Roach * @param string $onclick A javascript onclick handler 57a25f0a04SGreg Roach * @param Menu[] $submenus Any submenus 58a25f0a04SGreg Roach */ 59a25f0a04SGreg Roach public function __construct($label, $link = '#', $id = '', $onclick = '', $submenus = array()) { 60a25f0a04SGreg Roach $this 61a25f0a04SGreg Roach ->setLabel($label) 62a25f0a04SGreg Roach ->setLink($link) 63a25f0a04SGreg Roach ->setId($id) 64a25f0a04SGreg Roach ->setOnclick($onclick) 65a25f0a04SGreg Roach ->setSubmenus($submenus); 66a25f0a04SGreg Roach } 67a25f0a04SGreg Roach 68a25f0a04SGreg Roach /** 69a25f0a04SGreg Roach * Convert this menu to an HTML list, for easy rendering of 70a25f0a04SGreg Roach * lists of menus/nulls. 71a25f0a04SGreg Roach * 72a25f0a04SGreg Roach * @return string 73a25f0a04SGreg Roach */ 74a25f0a04SGreg Roach public function __toString() { 75a25f0a04SGreg Roach return $this->getMenuAsList(); 76a25f0a04SGreg Roach } 77a25f0a04SGreg Roach 78a25f0a04SGreg Roach /** 79a25f0a04SGreg Roach * Render this menu using Bootstrap markup 80a25f0a04SGreg Roach * 81a25f0a04SGreg Roach * @return string 82a25f0a04SGreg Roach */ 83a25f0a04SGreg Roach public function bootstrap() { 84a25f0a04SGreg Roach if ($this->iconclass) { 85a25f0a04SGreg Roach $class = ' class="' . $this->iconclass . '"'; 86a25f0a04SGreg Roach } else { 87a25f0a04SGreg Roach $class = ''; 88a25f0a04SGreg Roach } 89a25f0a04SGreg Roach if ($this->id) { 90a25f0a04SGreg Roach $id = ' id="' . $this->id . '"'; 91a25f0a04SGreg Roach } else { 92a25f0a04SGreg Roach $id = ''; 93a25f0a04SGreg Roach } 94a25f0a04SGreg Roach 95a25f0a04SGreg Roach if ($this->submenus) { 96a25f0a04SGreg Roach $submenus = ''; 97a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 98a25f0a04SGreg Roach $submenus .= $submenu->bootstrap(); 99a25f0a04SGreg Roach } 100a25f0a04SGreg Roach 101a25f0a04SGreg Roach return 102a25f0a04SGreg Roach '<li' . $id . ' class="dropdown">' . 103a25f0a04SGreg Roach '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . 104a25f0a04SGreg Roach $this->label . 105a25f0a04SGreg Roach ' <span class="caret"></span></a>' . 106a25f0a04SGreg Roach '<ul class="dropdown-menu" role="menu">' . 107a25f0a04SGreg Roach $submenus . 108a25f0a04SGreg Roach '</ul>' . 109a25f0a04SGreg Roach '</li>'; 110a25f0a04SGreg Roach } else { 111a25f0a04SGreg Roach if ($this->onclick) { 112a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 113a25f0a04SGreg Roach } else { 114a25f0a04SGreg Roach $onclick = ''; 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach return '<li' . $id . $class . '><a href="' . $this->link . '"' . $onclick . '>' . $this->label . '</a></li>'; 118a25f0a04SGreg Roach } 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach 121a25f0a04SGreg Roach /** 122a25f0a04SGreg Roach * Set the CSS classes for this menu 123a25f0a04SGreg Roach * 124a25f0a04SGreg Roach * @param string $class 125a25f0a04SGreg Roach * @param string $submenuclass 126a25f0a04SGreg Roach * @param string $iconclass 127a25f0a04SGreg Roach */ 128a25f0a04SGreg Roach public function addClass($class, $submenuclass = '', $iconclass = '') { 129a25f0a04SGreg Roach $this->class = $class; 130a25f0a04SGreg Roach $this->submenuclass = $submenuclass; 131a25f0a04SGreg Roach $this->iconclass = $iconclass; 132a25f0a04SGreg Roach } 133a25f0a04SGreg Roach 134a25f0a04SGreg Roach /** 135a25f0a04SGreg Roach * @return string 136a25f0a04SGreg Roach */ 137a25f0a04SGreg Roach public function getId() { 138a25f0a04SGreg Roach return $this->id; 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach 141a25f0a04SGreg Roach /** 142a25f0a04SGreg Roach * @param string $id 143a25f0a04SGreg Roach * 144a25f0a04SGreg Roach * @return $this 145a25f0a04SGreg Roach */ 146a25f0a04SGreg Roach public function setId($id) { 147a25f0a04SGreg Roach $this->id = $id; 148a25f0a04SGreg Roach 149a25f0a04SGreg Roach return $this; 150a25f0a04SGreg Roach } 151a25f0a04SGreg Roach 152a25f0a04SGreg Roach /** 153a25f0a04SGreg Roach * @return string 154a25f0a04SGreg Roach */ 155a25f0a04SGreg Roach public function getLabel() { 156a25f0a04SGreg Roach return $this->label; 157a25f0a04SGreg Roach } 158a25f0a04SGreg Roach 159a25f0a04SGreg Roach /** 160a25f0a04SGreg Roach * @param string $label 161a25f0a04SGreg Roach * 162a25f0a04SGreg Roach * @return $this 163a25f0a04SGreg Roach */ 164a25f0a04SGreg Roach public function setLabel($label) { 165a25f0a04SGreg Roach $this->label = $label; 166a25f0a04SGreg Roach 167a25f0a04SGreg Roach return $this; 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach 170a25f0a04SGreg Roach /** 171a25f0a04SGreg Roach * @return string 172a25f0a04SGreg Roach */ 173a25f0a04SGreg Roach public function getLink() { 174a25f0a04SGreg Roach return $this->link; 175a25f0a04SGreg Roach } 176a25f0a04SGreg Roach 177a25f0a04SGreg Roach /** 178a25f0a04SGreg Roach * @param string $link 179a25f0a04SGreg Roach * 180a25f0a04SGreg Roach * @return $this 181a25f0a04SGreg Roach */ 182a25f0a04SGreg Roach public function setLink($link) { 183a25f0a04SGreg Roach $this->link = $link; 184a25f0a04SGreg Roach 185a25f0a04SGreg Roach return $this; 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach 188a25f0a04SGreg Roach /** 189a25f0a04SGreg Roach * @return string 190a25f0a04SGreg Roach */ 191a25f0a04SGreg Roach public function getOnclick() { 192a25f0a04SGreg Roach return $this->onclick; 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach 195a25f0a04SGreg Roach /** 196a25f0a04SGreg Roach * @param string $onclick 197a25f0a04SGreg Roach * 198a25f0a04SGreg Roach * @return $this 199a25f0a04SGreg Roach */ 200a25f0a04SGreg Roach public function setOnclick($onclick) { 201a25f0a04SGreg Roach $this->onclick = $onclick; 202a25f0a04SGreg Roach 203a25f0a04SGreg Roach return $this; 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach 206a25f0a04SGreg Roach /** 207a25f0a04SGreg Roach * Add a submenu to this menu 208a25f0a04SGreg Roach * 209*ecb5b5e9SGreg Roach * @param Menu $menu 210*ecb5b5e9SGreg Roach * 211*ecb5b5e9SGreg Roach * @return $this 212a25f0a04SGreg Roach */ 213a25f0a04SGreg Roach public function addSubmenu($menu) { 214a25f0a04SGreg Roach $this->submenus[] = $menu; 215*ecb5b5e9SGreg Roach 216*ecb5b5e9SGreg Roach return $this; 217a25f0a04SGreg Roach } 218a25f0a04SGreg Roach 219a25f0a04SGreg Roach /** 220a25f0a04SGreg Roach * Render this menu using javascript popups.. 221a25f0a04SGreg Roach * 222a25f0a04SGreg Roach * @return string 223a25f0a04SGreg Roach */ 224a25f0a04SGreg Roach public function getMenu() { 2253cfaf201SGreg Roach global $menucount; 226a25f0a04SGreg Roach 227a25f0a04SGreg Roach if (!isset($menucount)) { 228a25f0a04SGreg Roach $menucount = 0; 229a25f0a04SGreg Roach } else { 230a25f0a04SGreg Roach $menucount++; 231a25f0a04SGreg Roach } 232a25f0a04SGreg Roach $id = $menucount . rand(); 233a25f0a04SGreg Roach $c = count($this->submenus); 234a25f0a04SGreg Roach $output = "<div id=\"menu{$id}\" class=\"{$this->class}\">"; 235a25f0a04SGreg Roach $link = "<a href=\"{$this->link}\" onmouseover=\""; 236a25f0a04SGreg Roach if ($c >= 0) { 237a25f0a04SGreg Roach $link .= "show_submenu('menu{$id}_subs', 'menu{$id}');"; 238a25f0a04SGreg Roach } 239a25f0a04SGreg Roach $link .= '" onmouseout="'; 240a25f0a04SGreg Roach if ($c >= 0) { 241a25f0a04SGreg Roach $link .= "timeout_submenu('menu{$id}_subs');"; 242a25f0a04SGreg Roach } 243a25f0a04SGreg Roach if ($this->onclick) { 244a25f0a04SGreg Roach $link .= "\" onclick=\"{$this->onclick}"; 245a25f0a04SGreg Roach } 246a25f0a04SGreg Roach $link .= "\">"; 247a25f0a04SGreg Roach $output .= $link; 248a25f0a04SGreg Roach $output .= $this->label; 249a25f0a04SGreg Roach $output .= "</a>"; 250a25f0a04SGreg Roach 251a25f0a04SGreg Roach if ($c > 0) { 252a25f0a04SGreg Roach $submenuid = "menu{$id}_subs"; 253c0af647eSGreg Roach if (I18N::direction() === 'ltr') { 254a25f0a04SGreg Roach $output .= '<div style="text-align: left;">'; 255a25f0a04SGreg Roach } else { 256a25f0a04SGreg Roach $output .= '<div style="text-align: right;">'; 257a25f0a04SGreg Roach } 258a25f0a04SGreg Roach $output .= "<div id=\"menu{$id}_subs\" class=\"{$this->submenuclass}\" style=\"position: absolute; visibility: hidden; z-index: 100;"; 259a25f0a04SGreg Roach $output .= "\" onmouseover=\"show_submenu('{$this->parentmenu}'); show_submenu('{$submenuid}');\" onmouseout=\"timeout_submenu('menu{$id}_subs');\">"; 260a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 261a25f0a04SGreg Roach $submenu->parentmenu = $submenuid; 262a25f0a04SGreg Roach $output .= $submenu->getMenu(); 263a25f0a04SGreg Roach } 264a25f0a04SGreg Roach $output .= "</div></div>"; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach $output .= "</div>"; 267a25f0a04SGreg Roach 268a25f0a04SGreg Roach return $output; 269a25f0a04SGreg Roach } 270a25f0a04SGreg Roach 271a25f0a04SGreg Roach /** 272a25f0a04SGreg Roach * Render this menu as an HTML list 273a25f0a04SGreg Roach * 274a25f0a04SGreg Roach * @return string 275a25f0a04SGreg Roach */ 276a25f0a04SGreg Roach public function getMenuAsList() { 277a25f0a04SGreg Roach if ($this->iconclass) { 278a25f0a04SGreg Roach $class = ' class="' . $this->iconclass . '"'; 279a25f0a04SGreg Roach } else { 280a25f0a04SGreg Roach $class = ''; 281a25f0a04SGreg Roach } 282a25f0a04SGreg Roach if ($this->onclick) { 283a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 284a25f0a04SGreg Roach } else { 285a25f0a04SGreg Roach $onclick = ''; 286a25f0a04SGreg Roach } 287a25f0a04SGreg Roach if ($this->link) { 288a25f0a04SGreg Roach $link = ' href="' . $this->link . '"'; 289a25f0a04SGreg Roach } else { 290a25f0a04SGreg Roach $link = ''; 291a25f0a04SGreg Roach } 292a25f0a04SGreg Roach if ($this->id) { 293a25f0a04SGreg Roach $id = ' id="' . $this->id . '"'; 294a25f0a04SGreg Roach } else { 295a25f0a04SGreg Roach $id = ''; 296a25f0a04SGreg Roach } 297a25f0a04SGreg Roach $html = '<a' . $link . $class . $onclick . '>' . $this->label . '</a>'; 298a25f0a04SGreg Roach if ($this->submenus) { 299a25f0a04SGreg Roach $html .= '<ul>'; 300a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 301a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 302a25f0a04SGreg Roach } 303a25f0a04SGreg Roach $html .= '</ul>'; 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach 306a25f0a04SGreg Roach return '<li' . $id . '>' . $html . '</li>'; 307a25f0a04SGreg Roach } 308a25f0a04SGreg Roach 309a25f0a04SGreg Roach /** 310a25f0a04SGreg Roach * @return Menu[] 311a25f0a04SGreg Roach */ 312a25f0a04SGreg Roach public function getSubmenus() { 313a25f0a04SGreg Roach return $this->submenus; 314a25f0a04SGreg Roach } 315a25f0a04SGreg Roach 316a25f0a04SGreg Roach /** 317a25f0a04SGreg Roach * @param Menu[] $submenus 318a25f0a04SGreg Roach * 319a25f0a04SGreg Roach * @return $this 320a25f0a04SGreg Roach */ 321a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 322a25f0a04SGreg Roach $this->submenus = $submenus; 323a25f0a04SGreg Roach 324a25f0a04SGreg Roach return $this; 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach} 327