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 * 209a25f0a04SGreg Roach * @param Menu[] 210a25f0a04SGreg Roach */ 211a25f0a04SGreg Roach public function addSubmenu($menu) { 212a25f0a04SGreg Roach $this->submenus[] = $menu; 213a25f0a04SGreg Roach } 214a25f0a04SGreg Roach 215a25f0a04SGreg Roach /** 216a25f0a04SGreg Roach * Render this menu using javascript popups.. 217a25f0a04SGreg Roach * 218a25f0a04SGreg Roach * @return string 219a25f0a04SGreg Roach */ 220a25f0a04SGreg Roach public function getMenu() { 221*3cfaf201SGreg Roach global $menucount; 222a25f0a04SGreg Roach 223a25f0a04SGreg Roach if (!isset($menucount)) { 224a25f0a04SGreg Roach $menucount = 0; 225a25f0a04SGreg Roach } else { 226a25f0a04SGreg Roach $menucount++; 227a25f0a04SGreg Roach } 228a25f0a04SGreg Roach $id = $menucount . rand(); 229a25f0a04SGreg Roach $c = count($this->submenus); 230a25f0a04SGreg Roach $output = "<div id=\"menu{$id}\" class=\"{$this->class}\">"; 231a25f0a04SGreg Roach $link = "<a href=\"{$this->link}\" onmouseover=\""; 232a25f0a04SGreg Roach if ($c >= 0) { 233a25f0a04SGreg Roach $link .= "show_submenu('menu{$id}_subs', 'menu{$id}');"; 234a25f0a04SGreg Roach } 235a25f0a04SGreg Roach $link .= '" onmouseout="'; 236a25f0a04SGreg Roach if ($c >= 0) { 237a25f0a04SGreg Roach $link .= "timeout_submenu('menu{$id}_subs');"; 238a25f0a04SGreg Roach } 239a25f0a04SGreg Roach if ($this->onclick) { 240a25f0a04SGreg Roach $link .= "\" onclick=\"{$this->onclick}"; 241a25f0a04SGreg Roach } 242a25f0a04SGreg Roach $link .= "\">"; 243a25f0a04SGreg Roach $output .= $link; 244a25f0a04SGreg Roach $output .= $this->label; 245a25f0a04SGreg Roach $output .= "</a>"; 246a25f0a04SGreg Roach 247a25f0a04SGreg Roach if ($c > 0) { 248a25f0a04SGreg Roach $submenuid = "menu{$id}_subs"; 249*3cfaf201SGreg Roach if (I18N::textDirection() === 'ltr') { 250a25f0a04SGreg Roach $output .= '<div style="text-align: left;">'; 251a25f0a04SGreg Roach } else { 252a25f0a04SGreg Roach $output .= '<div style="text-align: right;">'; 253a25f0a04SGreg Roach } 254a25f0a04SGreg Roach $output .= "<div id=\"menu{$id}_subs\" class=\"{$this->submenuclass}\" style=\"position: absolute; visibility: hidden; z-index: 100;"; 255a25f0a04SGreg Roach $output .= "\" onmouseover=\"show_submenu('{$this->parentmenu}'); show_submenu('{$submenuid}');\" onmouseout=\"timeout_submenu('menu{$id}_subs');\">"; 256a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 257a25f0a04SGreg Roach $submenu->parentmenu = $submenuid; 258a25f0a04SGreg Roach $output .= $submenu->getMenu(); 259a25f0a04SGreg Roach } 260a25f0a04SGreg Roach $output .= "</div></div>"; 261a25f0a04SGreg Roach } 262a25f0a04SGreg Roach $output .= "</div>"; 263a25f0a04SGreg Roach 264a25f0a04SGreg Roach return $output; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach /** 268a25f0a04SGreg Roach * Render this menu as an HTML list 269a25f0a04SGreg Roach * 270a25f0a04SGreg Roach * @return string 271a25f0a04SGreg Roach */ 272a25f0a04SGreg Roach public function getMenuAsList() { 273a25f0a04SGreg Roach if ($this->iconclass) { 274a25f0a04SGreg Roach $class = ' class="' . $this->iconclass . '"'; 275a25f0a04SGreg Roach } else { 276a25f0a04SGreg Roach $class = ''; 277a25f0a04SGreg Roach } 278a25f0a04SGreg Roach if ($this->onclick) { 279a25f0a04SGreg Roach $onclick = ' onclick="' . $this->onclick . '"'; 280a25f0a04SGreg Roach } else { 281a25f0a04SGreg Roach $onclick = ''; 282a25f0a04SGreg Roach } 283a25f0a04SGreg Roach if ($this->link) { 284a25f0a04SGreg Roach $link = ' href="' . $this->link . '"'; 285a25f0a04SGreg Roach } else { 286a25f0a04SGreg Roach $link = ''; 287a25f0a04SGreg Roach } 288a25f0a04SGreg Roach if ($this->id) { 289a25f0a04SGreg Roach $id = ' id="' . $this->id . '"'; 290a25f0a04SGreg Roach } else { 291a25f0a04SGreg Roach $id = ''; 292a25f0a04SGreg Roach } 293a25f0a04SGreg Roach $html = '<a' . $link . $class . $onclick . '>' . $this->label . '</a>'; 294a25f0a04SGreg Roach if ($this->submenus) { 295a25f0a04SGreg Roach $html .= '<ul>'; 296a25f0a04SGreg Roach foreach ($this->submenus as $submenu) { 297a25f0a04SGreg Roach $html .= $submenu->getMenuAsList(); 298a25f0a04SGreg Roach } 299a25f0a04SGreg Roach $html .= '</ul>'; 300a25f0a04SGreg Roach } 301a25f0a04SGreg Roach 302a25f0a04SGreg Roach return '<li' . $id . '>' . $html . '</li>'; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach 305a25f0a04SGreg Roach /** 306a25f0a04SGreg Roach * @return Menu[] 307a25f0a04SGreg Roach */ 308a25f0a04SGreg Roach public function getSubmenus() { 309a25f0a04SGreg Roach return $this->submenus; 310a25f0a04SGreg Roach } 311a25f0a04SGreg Roach 312a25f0a04SGreg Roach /** 313a25f0a04SGreg Roach * @param Menu[] $submenus 314a25f0a04SGreg Roach * 315a25f0a04SGreg Roach * @return $this 316a25f0a04SGreg Roach */ 317a25f0a04SGreg Roach public function setSubmenus(array $submenus) { 318a25f0a04SGreg Roach $this->submenus = $submenus; 319a25f0a04SGreg Roach 320a25f0a04SGreg Roach return $this; 321a25f0a04SGreg Roach } 322a25f0a04SGreg Roach} 323