xref: /webtrees/app/Menu.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roach/**
1976692c8bSGreg Roach * System for generating menus.
20a25f0a04SGreg Roach */
21*c1010edaSGreg Roachclass Menu
22*c1010edaSGreg Roach{
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
293d0a6fa1SGreg Roach    /** @var string The CSS class used to style this menu item */
303d0a6fa1SGreg Roach    private $class;
31a25f0a04SGreg Roach
323cf92ae2SGreg Roach    /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */
333cf92ae2SGreg Roach    private $attrs;
34a25f0a04SGreg Roach
3576692c8bSGreg Roach    /** @var Menu[] An optional list of sub-menus. */
36a25f0a04SGreg Roach    private $submenus;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /**
39a25f0a04SGreg Roach     * Constructor for the menu class
40a25f0a04SGreg Roach     *
41a25f0a04SGreg Roach     * @param string   $label    The label for the menu item
42a25f0a04SGreg Roach     * @param string   $link     The target URL
433cf92ae2SGreg Roach     * @param string   $class    A CSS class
443cf92ae2SGreg Roach     * @param string[] $attrs    Optional attributes, such as onclick or data-xxx
45a25f0a04SGreg Roach     * @param Menu[]   $submenus Any submenus
46a25f0a04SGreg Roach     */
47*c1010edaSGreg Roach    public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = [])
48*c1010edaSGreg Roach    {
49a25f0a04SGreg Roach        $this
50a25f0a04SGreg Roach            ->setLabel($label)
51a25f0a04SGreg Roach            ->setLink($link)
523d0a6fa1SGreg Roach            ->setClass($class)
533cf92ae2SGreg Roach            ->setAttrs($attrs)
54a25f0a04SGreg Roach            ->setSubmenus($submenus);
55a25f0a04SGreg Roach    }
56a25f0a04SGreg Roach
57a25f0a04SGreg Roach    /**
5815d603e7SGreg Roach     * Render this menu using Bootstrap4 markup
59a25f0a04SGreg Roach     *
60a25f0a04SGreg Roach     * @return string
61a25f0a04SGreg Roach     */
62*c1010edaSGreg Roach    public function bootstrap4()
63*c1010edaSGreg Roach    {
64a25f0a04SGreg Roach        if ($this->submenus) {
65a25f0a04SGreg Roach            $submenus = '';
66a25f0a04SGreg Roach            foreach ($this->submenus as $submenu) {
6715d603e7SGreg Roach                $attrs = '';
6815d603e7SGreg Roach                foreach ($submenu->attrs as $key => $value) {
69d53324c9SGreg Roach                    $attrs .= ' ' . $key . '="' . e($value) . '"';
70a25f0a04SGreg Roach                }
71a25f0a04SGreg Roach
7215d603e7SGreg Roach                $class    = trim('dropdown-item ' . $submenu->class);
7325b2dde3SGreg Roach                $submenus .= '<a class="' . $class . '" href="' . e($submenu->link) . '"' . $attrs . '>' . $submenu->label . '</a>';
7415d603e7SGreg Roach            }
7515d603e7SGreg Roach
7615d603e7SGreg Roach            $class = trim('nav-item dropdown ' . $this->class);
7715d603e7SGreg Roach
78a25f0a04SGreg Roach            return
7915d603e7SGreg Roach                '<li class="' . $class . '">' .
8015d603e7SGreg Roach                '<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' .
81a25f0a04SGreg Roach                $this->label .
82a25f0a04SGreg Roach                '<span class="caret"></span></a>' .
8315d603e7SGreg Roach                '<div class="dropdown-menu" role="menu">' .
84a25f0a04SGreg Roach                $submenus .
8515d603e7SGreg Roach                '</div>' .
86a25f0a04SGreg Roach                '</li>';
87a25f0a04SGreg Roach        } else {
883cf92ae2SGreg Roach            $attrs = '';
893cf92ae2SGreg Roach            foreach ($this->attrs as $key => $value) {
90d53324c9SGreg Roach                $attrs .= ' ' . $key . '="' . e($value) . '"';
91a25f0a04SGreg Roach            }
92a25f0a04SGreg Roach
9315d603e7SGreg Roach            $class = trim('nav-item ' . $this->class);
9415d603e7SGreg Roach
9525b2dde3SGreg Roach            return '<li class="' . $class . '"><a class="nav-link" href="' . e($this->link) . '"' . $attrs . '>' . $this->label . '</a></li>';
96a25f0a04SGreg Roach        }
97a25f0a04SGreg Roach    }
98a25f0a04SGreg Roach
99a25f0a04SGreg Roach    /**
1003cf92ae2SGreg Roach     * Get the optional attributes.
1013cf92ae2SGreg Roach     *
1023cf92ae2SGreg Roach     * @return string[]
1033cf92ae2SGreg Roach     */
104*c1010edaSGreg Roach    public function getAttrs()
105*c1010edaSGreg Roach    {
1063cf92ae2SGreg Roach        return $this->attrs;
1073cf92ae2SGreg Roach    }
1083cf92ae2SGreg Roach
1093cf92ae2SGreg Roach    /**
1103cf92ae2SGreg Roach     * Set the optional attributes.
1113cf92ae2SGreg Roach     *
1123cf92ae2SGreg Roach     * @param string[] $attrs
1133cf92ae2SGreg Roach     *
1143cf92ae2SGreg Roach     * @return $this
1153cf92ae2SGreg Roach     */
116*c1010edaSGreg Roach    public function setAttrs(array $attrs)
117*c1010edaSGreg Roach    {
1183cf92ae2SGreg Roach        $this->attrs = $attrs;
1193cf92ae2SGreg Roach
1203cf92ae2SGreg Roach        return $this;
1213cf92ae2SGreg Roach    }
1223cf92ae2SGreg Roach
1233cf92ae2SGreg Roach    /**
12476692c8bSGreg Roach     * Get the class.
12576692c8bSGreg Roach     *
126a25f0a04SGreg Roach     * @return string
127a25f0a04SGreg Roach     */
128*c1010edaSGreg Roach    public function getClass()
129*c1010edaSGreg Roach    {
1303d0a6fa1SGreg Roach        return $this->class;
131a25f0a04SGreg Roach    }
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach    /**
13476692c8bSGreg Roach     * Set the class.
13576692c8bSGreg Roach     *
1363d0a6fa1SGreg Roach     * @param string $class
137a25f0a04SGreg Roach     *
138a25f0a04SGreg Roach     * @return $this
139a25f0a04SGreg Roach     */
140*c1010edaSGreg Roach    public function setClass($class)
141*c1010edaSGreg Roach    {
1423d0a6fa1SGreg Roach        $this->class = $class;
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach        return $this;
145a25f0a04SGreg Roach    }
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach    /**
14876692c8bSGreg Roach     * Get the label.
14976692c8bSGreg Roach     *
150a25f0a04SGreg Roach     * @return string
151a25f0a04SGreg Roach     */
152*c1010edaSGreg Roach    public function getLabel()
153*c1010edaSGreg Roach    {
154a25f0a04SGreg Roach        return $this->label;
155a25f0a04SGreg Roach    }
156a25f0a04SGreg Roach
157a25f0a04SGreg Roach    /**
15876692c8bSGreg Roach     * Set the label.
15976692c8bSGreg Roach     *
160a25f0a04SGreg Roach     * @param string $label
161a25f0a04SGreg Roach     *
162a25f0a04SGreg Roach     * @return $this
163a25f0a04SGreg Roach     */
164*c1010edaSGreg Roach    public function setLabel($label)
165*c1010edaSGreg Roach    {
166a25f0a04SGreg Roach        $this->label = $label;
167a25f0a04SGreg Roach
168a25f0a04SGreg Roach        return $this;
169a25f0a04SGreg Roach    }
170a25f0a04SGreg Roach
171a25f0a04SGreg Roach    /**
17276692c8bSGreg Roach     * Get the link.
1733cf92ae2SGreg Roach     *
174a25f0a04SGreg Roach     * @return string
175a25f0a04SGreg Roach     */
176*c1010edaSGreg Roach    public function getLink()
177*c1010edaSGreg Roach    {
178a25f0a04SGreg Roach        return $this->link;
179a25f0a04SGreg Roach    }
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach    /**
18276692c8bSGreg Roach     * Set the link.
18376692c8bSGreg Roach     *
184a25f0a04SGreg Roach     * @param string $link
185a25f0a04SGreg Roach     *
186a25f0a04SGreg Roach     * @return $this
187a25f0a04SGreg Roach     */
188*c1010edaSGreg Roach    public function setLink($link)
189*c1010edaSGreg Roach    {
190a25f0a04SGreg Roach        $this->link = $link;
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     */
202*c1010edaSGreg Roach    public function addSubmenu($menu)
203*c1010edaSGreg Roach    {
204a25f0a04SGreg Roach        $this->submenus[] = $menu;
205ecb5b5e9SGreg Roach
206ecb5b5e9SGreg Roach        return $this;
207a25f0a04SGreg Roach    }
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach    /**
210a25f0a04SGreg Roach     * Render this menu as an HTML list
211a25f0a04SGreg Roach     *
212a25f0a04SGreg Roach     * @return string
213a25f0a04SGreg Roach     */
214*c1010edaSGreg Roach    public function getMenuAsList()
215*c1010edaSGreg Roach    {
2163cf92ae2SGreg Roach        $attrs = '';
2173cf92ae2SGreg Roach        foreach ($this->attrs as $key => $value) {
218d53324c9SGreg Roach            $attrs .= ' ' . $key . '="' . e($value) . '"';
219a25f0a04SGreg Roach        }
220a25f0a04SGreg Roach        if ($this->link) {
22125b2dde3SGreg Roach            $link = ' href="' . e($this->link) . '"';
222a25f0a04SGreg Roach        } else {
223a25f0a04SGreg Roach            $link = '';
224a25f0a04SGreg Roach        }
2253cf92ae2SGreg Roach        $html = '<a' . $link . $attrs . '>' . $this->label . '</a>';
226a25f0a04SGreg Roach        if ($this->submenus) {
227a25f0a04SGreg Roach            $html .= '<ul>';
228a25f0a04SGreg Roach            foreach ($this->submenus as $submenu) {
229a25f0a04SGreg Roach                $html .= $submenu->getMenuAsList();
230a25f0a04SGreg Roach            }
231a25f0a04SGreg Roach            $html .= '</ul>';
232a25f0a04SGreg Roach        }
233a25f0a04SGreg Roach
2343d0a6fa1SGreg Roach        return '<li class="' . $this->class . '">' . $html . '</li>';
235a25f0a04SGreg Roach    }
236a25f0a04SGreg Roach
237a25f0a04SGreg Roach    /**
23876692c8bSGreg Roach     * Get the sub-menus.
23976692c8bSGreg Roach     *
240a25f0a04SGreg Roach     * @return Menu[]
241a25f0a04SGreg Roach     */
242*c1010edaSGreg Roach    public function getSubmenus()
243*c1010edaSGreg Roach    {
244a25f0a04SGreg Roach        return $this->submenus;
245a25f0a04SGreg Roach    }
246a25f0a04SGreg Roach
247a25f0a04SGreg Roach    /**
24876692c8bSGreg Roach     * Set the sub-menus.
24976692c8bSGreg Roach     *
250a25f0a04SGreg Roach     * @param Menu[] $submenus
251a25f0a04SGreg Roach     *
252a25f0a04SGreg Roach     * @return $this
253a25f0a04SGreg Roach     */
254*c1010edaSGreg Roach    public function setSubmenus(array $submenus)
255*c1010edaSGreg Roach    {
256a25f0a04SGreg Roach        $this->submenus = $submenus;
257a25f0a04SGreg Roach
258a25f0a04SGreg Roach        return $this;
259a25f0a04SGreg Roach    }
260a25f0a04SGreg Roach}
261