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