xref: /webtrees/app/Menu.php (revision 3d0a6fa104c785a847746348b8ca40fdd4474e89)
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
29*3d0a6fa1SGreg Roach	/** @var string The CSS class used to style this menu item */
30*3d0a6fa1SGreg Roach	private $class;
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 */
48*3d0a6fa1SGreg Roach	private $menuclass;
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
55*3d0a6fa1SGreg Roach	 * @param string    $class    An CSS class
56a25f0a04SGreg Roach	 * @param string    $onclick  A javascript onclick handler
57a25f0a04SGreg Roach	 * @param Menu[] $submenus Any submenus
58a25f0a04SGreg Roach	 */
59*3d0a6fa1SGreg Roach	public function __construct($label, $link = '#', $class = '', $onclick = '', $submenus = array()) {
60a25f0a04SGreg Roach		$this
61a25f0a04SGreg Roach			->setLabel($label)
62a25f0a04SGreg Roach			->setLink($link)
63*3d0a6fa1SGreg Roach			->setClass($class)
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->submenus) {
85a25f0a04SGreg Roach			$submenus = '';
86a25f0a04SGreg Roach			foreach ($this->submenus as $submenu) {
87a25f0a04SGreg Roach				$submenus .= $submenu->bootstrap();
88a25f0a04SGreg Roach			}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach			return
91*3d0a6fa1SGreg Roach				'<li class="' . $this->class . ' dropdown">' .
92a25f0a04SGreg Roach				'<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' .
93a25f0a04SGreg Roach				$this->label .
94a25f0a04SGreg Roach				' <span class="caret"></span></a>' .
95a25f0a04SGreg Roach				'<ul class="dropdown-menu" role="menu">' .
96a25f0a04SGreg Roach				$submenus .
97a25f0a04SGreg Roach				'</ul>' .
98a25f0a04SGreg Roach				'</li>';
99a25f0a04SGreg Roach		} else {
100a25f0a04SGreg Roach			if ($this->onclick) {
101a25f0a04SGreg Roach				$onclick = ' onclick="' . $this->onclick . '"';
102a25f0a04SGreg Roach			} else {
103a25f0a04SGreg Roach				$onclick = '';
104a25f0a04SGreg Roach			}
105a25f0a04SGreg Roach
106*3d0a6fa1SGreg Roach			return '<li class="' . $this->class . '"><a href="' . $this->link . '"' . $onclick . '>' . $this->label . '</a></li>';
107a25f0a04SGreg Roach		}
108a25f0a04SGreg Roach	}
109a25f0a04SGreg Roach
110a25f0a04SGreg Roach	/**
111*3d0a6fa1SGreg Roach	 * Set the CSS classes for the (legacy) javascript menus
112a25f0a04SGreg Roach	 *
113a25f0a04SGreg Roach	 * @param string $class
114a25f0a04SGreg Roach	 * @param string $submenuclass
115a25f0a04SGreg Roach	 * @param string $iconclass
116a25f0a04SGreg Roach	 */
117*3d0a6fa1SGreg Roach	public function addClass($menuclass, $submenuclass = '', $iconclass = '') {
118*3d0a6fa1SGreg Roach		$this->menuclass = $menuclass;
119a25f0a04SGreg Roach		$this->submenuclass = $submenuclass;
120a25f0a04SGreg Roach		$this->iconclass = $iconclass;
121a25f0a04SGreg Roach	}
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach	/**
124a25f0a04SGreg Roach	 * @return string
125a25f0a04SGreg Roach	 */
126*3d0a6fa1SGreg Roach	public function getClass() {
127*3d0a6fa1SGreg Roach		return $this->class;
128a25f0a04SGreg Roach	}
129a25f0a04SGreg Roach
130a25f0a04SGreg Roach	/**
131*3d0a6fa1SGreg Roach	 * @param string $class
132a25f0a04SGreg Roach	 *
133a25f0a04SGreg Roach	 * @return $this
134a25f0a04SGreg Roach	 */
135*3d0a6fa1SGreg Roach	public function setClass($class) {
136*3d0a6fa1SGreg Roach		$this->class = $class;
137a25f0a04SGreg Roach
138a25f0a04SGreg Roach		return $this;
139a25f0a04SGreg Roach	}
140a25f0a04SGreg Roach
141a25f0a04SGreg Roach	/**
142a25f0a04SGreg Roach	 * @return string
143a25f0a04SGreg Roach	 */
144a25f0a04SGreg Roach	public function getLabel() {
145a25f0a04SGreg Roach		return $this->label;
146a25f0a04SGreg Roach	}
147a25f0a04SGreg Roach
148a25f0a04SGreg Roach	/**
149a25f0a04SGreg Roach	 * @param string $label
150a25f0a04SGreg Roach	 *
151a25f0a04SGreg Roach	 * @return $this
152a25f0a04SGreg Roach	 */
153a25f0a04SGreg Roach	public function setLabel($label) {
154a25f0a04SGreg Roach		$this->label = $label;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach		return $this;
157a25f0a04SGreg Roach	}
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach	/**
160a25f0a04SGreg Roach	 * @return string
161a25f0a04SGreg Roach	 */
162a25f0a04SGreg Roach	public function getLink() {
163a25f0a04SGreg Roach		return $this->link;
164a25f0a04SGreg Roach	}
165a25f0a04SGreg Roach
166a25f0a04SGreg Roach	/**
167a25f0a04SGreg Roach	 * @param string $link
168a25f0a04SGreg Roach	 *
169a25f0a04SGreg Roach	 * @return $this
170a25f0a04SGreg Roach	 */
171a25f0a04SGreg Roach	public function setLink($link) {
172a25f0a04SGreg Roach		$this->link = $link;
173a25f0a04SGreg Roach
174a25f0a04SGreg Roach		return $this;
175a25f0a04SGreg Roach	}
176a25f0a04SGreg Roach
177a25f0a04SGreg Roach	/**
178a25f0a04SGreg Roach	 * @return string
179a25f0a04SGreg Roach	 */
180a25f0a04SGreg Roach	public function getOnclick() {
181a25f0a04SGreg Roach		return $this->onclick;
182a25f0a04SGreg Roach	}
183a25f0a04SGreg Roach
184a25f0a04SGreg Roach	/**
185a25f0a04SGreg Roach	 * @param string $onclick
186a25f0a04SGreg Roach	 *
187a25f0a04SGreg Roach	 * @return $this
188a25f0a04SGreg Roach	 */
189a25f0a04SGreg Roach	public function setOnclick($onclick) {
190a25f0a04SGreg Roach		$this->onclick = $onclick;
191a25f0a04SGreg Roach
192a25f0a04SGreg Roach		return $this;
193a25f0a04SGreg Roach	}
194a25f0a04SGreg Roach
195a25f0a04SGreg Roach	/**
196a25f0a04SGreg Roach	 * Add a submenu to this menu
197a25f0a04SGreg Roach	 *
198ecb5b5e9SGreg Roach	 * @param Menu $menu
199ecb5b5e9SGreg Roach	 *
200ecb5b5e9SGreg Roach	 * @return $this
201a25f0a04SGreg Roach	 */
202a25f0a04SGreg Roach	public function addSubmenu($menu) {
203a25f0a04SGreg Roach		$this->submenus[] = $menu;
204ecb5b5e9SGreg Roach
205ecb5b5e9SGreg Roach		return $this;
206a25f0a04SGreg Roach	}
207a25f0a04SGreg Roach
208a25f0a04SGreg Roach	/**
209a25f0a04SGreg Roach	 * Render this menu using javascript popups..
210a25f0a04SGreg Roach	 *
211a25f0a04SGreg Roach	 * @return string
212a25f0a04SGreg Roach	 */
213a25f0a04SGreg Roach	public function getMenu() {
2143cfaf201SGreg Roach		global $menucount;
215a25f0a04SGreg Roach
216a25f0a04SGreg Roach		if (!isset($menucount)) {
217a25f0a04SGreg Roach			$menucount = 0;
218a25f0a04SGreg Roach		} else {
219a25f0a04SGreg Roach			$menucount++;
220a25f0a04SGreg Roach		}
221a25f0a04SGreg Roach		$id = $menucount . rand();
222a25f0a04SGreg Roach		$c = count($this->submenus);
223*3d0a6fa1SGreg Roach		$output = "<div id=\"menu{$id}\" class=\"{$this->menuclass}\">";
224a25f0a04SGreg Roach		$link = "<a href=\"{$this->link}\" onmouseover=\"";
225a25f0a04SGreg Roach		if ($c >= 0) {
226a25f0a04SGreg Roach			$link .= "show_submenu('menu{$id}_subs', 'menu{$id}');";
227a25f0a04SGreg Roach		}
228a25f0a04SGreg Roach		$link .= '" onmouseout="';
229a25f0a04SGreg Roach		if ($c >= 0) {
230a25f0a04SGreg Roach			$link .= "timeout_submenu('menu{$id}_subs');";
231a25f0a04SGreg Roach		}
232a25f0a04SGreg Roach		if ($this->onclick) {
233a25f0a04SGreg Roach			$link .= "\" onclick=\"{$this->onclick}";
234a25f0a04SGreg Roach		}
235a25f0a04SGreg Roach		$link .= "\">";
236a25f0a04SGreg Roach		$output .= $link;
237a25f0a04SGreg Roach		$output .= $this->label;
238a25f0a04SGreg Roach		$output .= "</a>";
239a25f0a04SGreg Roach
240a25f0a04SGreg Roach		if ($c > 0) {
241a25f0a04SGreg Roach			$submenuid = "menu{$id}_subs";
242c0af647eSGreg Roach			if (I18N::direction() === 'ltr') {
243a25f0a04SGreg Roach				$output .= '<div style="text-align: left;">';
244a25f0a04SGreg Roach			} else {
245a25f0a04SGreg Roach				$output .= '<div style="text-align: right;">';
246a25f0a04SGreg Roach			}
247a25f0a04SGreg Roach			$output .= "<div id=\"menu{$id}_subs\" class=\"{$this->submenuclass}\" style=\"position: absolute; visibility: hidden; z-index: 100;";
248a25f0a04SGreg Roach			$output .= "\" onmouseover=\"show_submenu('{$this->parentmenu}'); show_submenu('{$submenuid}');\" onmouseout=\"timeout_submenu('menu{$id}_subs');\">";
249a25f0a04SGreg Roach			foreach ($this->submenus as $submenu) {
250a25f0a04SGreg Roach				$submenu->parentmenu = $submenuid;
251a25f0a04SGreg Roach				$output .= $submenu->getMenu();
252a25f0a04SGreg Roach			}
253a25f0a04SGreg Roach			$output .= "</div></div>";
254a25f0a04SGreg Roach		}
255a25f0a04SGreg Roach		$output .= "</div>";
256a25f0a04SGreg Roach
257a25f0a04SGreg Roach		return $output;
258a25f0a04SGreg Roach	}
259a25f0a04SGreg Roach
260a25f0a04SGreg Roach	/**
261a25f0a04SGreg Roach	 * Render this menu as an HTML list
262a25f0a04SGreg Roach	 *
263a25f0a04SGreg Roach	 * @return string
264a25f0a04SGreg Roach	 */
265a25f0a04SGreg Roach	public function getMenuAsList() {
266a25f0a04SGreg Roach		if ($this->onclick) {
267a25f0a04SGreg Roach			$onclick = ' onclick="' . $this->onclick . '"';
268a25f0a04SGreg Roach		} else {
269a25f0a04SGreg Roach			$onclick = '';
270a25f0a04SGreg Roach		}
271a25f0a04SGreg Roach		if ($this->link) {
272a25f0a04SGreg Roach			$link = ' href="' . $this->link . '"';
273a25f0a04SGreg Roach		} else {
274a25f0a04SGreg Roach			$link = '';
275a25f0a04SGreg Roach		}
276*3d0a6fa1SGreg Roach		$html = '<a' . $link . $onclick . '>' . $this->label . '</a>';
277a25f0a04SGreg Roach		if ($this->submenus) {
278a25f0a04SGreg Roach			$html .= '<ul>';
279a25f0a04SGreg Roach			foreach ($this->submenus as $submenu) {
280a25f0a04SGreg Roach				$html .= $submenu->getMenuAsList();
281a25f0a04SGreg Roach			}
282a25f0a04SGreg Roach			$html .= '</ul>';
283a25f0a04SGreg Roach		}
284a25f0a04SGreg Roach
285*3d0a6fa1SGreg Roach		return '<li class="' . $this->class . '">' . $html . '</li>';
286a25f0a04SGreg Roach	}
287a25f0a04SGreg Roach
288a25f0a04SGreg Roach	/**
289a25f0a04SGreg Roach	 * @return Menu[]
290a25f0a04SGreg Roach	 */
291a25f0a04SGreg Roach	public function getSubmenus() {
292a25f0a04SGreg Roach		return $this->submenus;
293a25f0a04SGreg Roach	}
294a25f0a04SGreg Roach
295a25f0a04SGreg Roach	/**
296a25f0a04SGreg Roach	 * @param Menu[] $submenus
297a25f0a04SGreg Roach	 *
298a25f0a04SGreg Roach	 * @return $this
299a25f0a04SGreg Roach	 */
300a25f0a04SGreg Roach	public function setSubmenus(array $submenus) {
301a25f0a04SGreg Roach		$this->submenus = $submenus;
302a25f0a04SGreg Roach
303a25f0a04SGreg Roach		return $this;
304a25f0a04SGreg Roach	}
305a25f0a04SGreg Roach}
306