xref: /webtrees/app/Menu.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22f78837dcSGreg Roachuse function trigger_error;
23f78837dcSGreg Roach
24f78837dcSGreg Roachuse const E_USER_DEPRECATED;
25f78837dcSGreg Roach
26a25f0a04SGreg Roach/**
2776692c8bSGreg Roach * System for generating menus.
28a25f0a04SGreg Roach */
29c1010edaSGreg Roachclass Menu
30c1010edaSGreg Roach{
31fceda430SGreg Roach    /** @var string The text to be displayed in the menu */
32a25f0a04SGreg Roach    private $label;
33a25f0a04SGreg Roach
34a25f0a04SGreg Roach    /** @var string The target URL or href */
35a25f0a04SGreg Roach    private $link;
36a25f0a04SGreg Roach
373d0a6fa1SGreg Roach    /** @var string The CSS class used to style this menu item */
383d0a6fa1SGreg Roach    private $class;
39a25f0a04SGreg Roach
403cf92ae2SGreg Roach    /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */
413cf92ae2SGreg Roach    private $attrs;
42a25f0a04SGreg Roach
4376692c8bSGreg Roach    /** @var Menu[] An optional list of sub-menus. */
44a25f0a04SGreg Roach    private $submenus;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach    /**
47a25f0a04SGreg Roach     * Constructor for the menu class
48a25f0a04SGreg Roach     *
49a25f0a04SGreg Roach     * @param string   $label    The label for the menu item
50a25f0a04SGreg Roach     * @param string   $link     The target URL
513cf92ae2SGreg Roach     * @param string   $class    A CSS class
523cf92ae2SGreg Roach     * @param string[] $attrs    Optional attributes, such as onclick or data-xxx
53a25f0a04SGreg Roach     * @param Menu[]   $submenus Any submenus
54a25f0a04SGreg Roach     */
55c1010edaSGreg Roach    public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = [])
56c1010edaSGreg Roach    {
57a25f0a04SGreg Roach        $this
58a25f0a04SGreg Roach            ->setLabel($label)
59a25f0a04SGreg Roach            ->setLink($link)
603d0a6fa1SGreg Roach            ->setClass($class)
613cf92ae2SGreg Roach            ->setAttrs($attrs)
62a25f0a04SGreg Roach            ->setSubmenus($submenus);
63a25f0a04SGreg Roach    }
64a25f0a04SGreg Roach
65a25f0a04SGreg Roach    /**
6615d603e7SGreg Roach     * Render this menu using Bootstrap4 markup
67a25f0a04SGreg Roach     *
68a25f0a04SGreg Roach     * @return string
69f78837dcSGreg Roach     *
70f78837dcSGreg Roach     * @deprecated since 2.0.2.  Will be removed in 2.1.0
71a25f0a04SGreg Roach     */
72e364afe4SGreg Roach    public function bootstrap4(): string
73c1010edaSGreg Roach    {
74f78837dcSGreg Roach        trigger_error(
75f78837dcSGreg Roach            'Menu::bootstrap4() is deprecated.  Use the view(components/menu-item) instead',
76f78837dcSGreg Roach            E_USER_DEPRECATED
77f78837dcSGreg Roach        );
78a25f0a04SGreg Roach
79f78837dcSGreg Roach        return view('components/menu-item', ['menu' => $this]);
80a25f0a04SGreg Roach    }
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach    /**
833cf92ae2SGreg Roach     * Get the optional attributes.
843cf92ae2SGreg Roach     *
853cf92ae2SGreg Roach     * @return string[]
863cf92ae2SGreg Roach     */
878f53f488SRico Sonntag    public function getAttrs(): array
88c1010edaSGreg Roach    {
893cf92ae2SGreg Roach        return $this->attrs;
903cf92ae2SGreg Roach    }
913cf92ae2SGreg Roach
923cf92ae2SGreg Roach    /**
933cf92ae2SGreg Roach     * Set the optional attributes.
943cf92ae2SGreg Roach     *
953cf92ae2SGreg Roach     * @param string[] $attrs
963cf92ae2SGreg Roach     *
973cf92ae2SGreg Roach     * @return $this
983cf92ae2SGreg Roach     */
998f53f488SRico Sonntag    public function setAttrs(array $attrs): self
100c1010edaSGreg Roach    {
1013cf92ae2SGreg Roach        $this->attrs = $attrs;
1023cf92ae2SGreg Roach
1033cf92ae2SGreg Roach        return $this;
1043cf92ae2SGreg Roach    }
1053cf92ae2SGreg Roach
1063cf92ae2SGreg Roach    /**
10776692c8bSGreg Roach     * Get the class.
10876692c8bSGreg Roach     *
109a25f0a04SGreg Roach     * @return string
110a25f0a04SGreg Roach     */
1118f53f488SRico Sonntag    public function getClass(): string
112c1010edaSGreg Roach    {
1133d0a6fa1SGreg Roach        return $this->class;
114a25f0a04SGreg Roach    }
115a25f0a04SGreg Roach
116a25f0a04SGreg Roach    /**
11776692c8bSGreg Roach     * Set the class.
11876692c8bSGreg Roach     *
1193d0a6fa1SGreg Roach     * @param string $class
120a25f0a04SGreg Roach     *
121a25f0a04SGreg Roach     * @return $this
122a25f0a04SGreg Roach     */
1238f53f488SRico Sonntag    public function setClass($class): self
124c1010edaSGreg Roach    {
1253d0a6fa1SGreg Roach        $this->class = $class;
126a25f0a04SGreg Roach
127a25f0a04SGreg Roach        return $this;
128a25f0a04SGreg Roach    }
129a25f0a04SGreg Roach
130a25f0a04SGreg Roach    /**
13176692c8bSGreg Roach     * Get the label.
13276692c8bSGreg Roach     *
133a25f0a04SGreg Roach     * @return string
134a25f0a04SGreg Roach     */
1358f53f488SRico Sonntag    public function getLabel(): string
136c1010edaSGreg Roach    {
137a25f0a04SGreg Roach        return $this->label;
138a25f0a04SGreg Roach    }
139a25f0a04SGreg Roach
140a25f0a04SGreg Roach    /**
14176692c8bSGreg Roach     * Set the label.
14276692c8bSGreg Roach     *
143a25f0a04SGreg Roach     * @param string $label
144a25f0a04SGreg Roach     *
145a25f0a04SGreg Roach     * @return $this
146a25f0a04SGreg Roach     */
1478f53f488SRico Sonntag    public function setLabel($label): self
148c1010edaSGreg Roach    {
149a25f0a04SGreg Roach        $this->label = $label;
150a25f0a04SGreg Roach
151a25f0a04SGreg Roach        return $this;
152a25f0a04SGreg Roach    }
153a25f0a04SGreg Roach
154a25f0a04SGreg Roach    /**
15576692c8bSGreg Roach     * Get the link.
1563cf92ae2SGreg Roach     *
157a25f0a04SGreg Roach     * @return string
158a25f0a04SGreg Roach     */
1598f53f488SRico Sonntag    public function getLink(): string
160c1010edaSGreg Roach    {
161a25f0a04SGreg Roach        return $this->link;
162a25f0a04SGreg Roach    }
163a25f0a04SGreg Roach
164a25f0a04SGreg Roach    /**
16576692c8bSGreg Roach     * Set the link.
16676692c8bSGreg Roach     *
167a25f0a04SGreg Roach     * @param string $link
168a25f0a04SGreg Roach     *
169a25f0a04SGreg Roach     * @return $this
170a25f0a04SGreg Roach     */
1718f53f488SRico Sonntag    public function setLink($link): self
172c1010edaSGreg Roach    {
173a25f0a04SGreg Roach        $this->link = $link;
174a25f0a04SGreg Roach
175a25f0a04SGreg Roach        return $this;
176a25f0a04SGreg Roach    }
177a25f0a04SGreg Roach
178a25f0a04SGreg Roach    /**
179a25f0a04SGreg Roach     * Add a submenu to this menu
180a25f0a04SGreg Roach     *
181ecb5b5e9SGreg Roach     * @param Menu $menu
182ecb5b5e9SGreg Roach     *
183ecb5b5e9SGreg Roach     * @return $this
184a25f0a04SGreg Roach     */
1858f53f488SRico Sonntag    public function addSubmenu($menu): self
186c1010edaSGreg Roach    {
187a25f0a04SGreg Roach        $this->submenus[] = $menu;
188ecb5b5e9SGreg Roach
189ecb5b5e9SGreg Roach        return $this;
190a25f0a04SGreg Roach    }
191a25f0a04SGreg Roach
192a25f0a04SGreg Roach    /**
19376692c8bSGreg Roach     * Get the sub-menus.
19476692c8bSGreg Roach     *
195a25f0a04SGreg Roach     * @return Menu[]
196a25f0a04SGreg Roach     */
1978f53f488SRico Sonntag    public function getSubmenus(): array
198c1010edaSGreg Roach    {
199a25f0a04SGreg Roach        return $this->submenus;
200a25f0a04SGreg Roach    }
201a25f0a04SGreg Roach
202a25f0a04SGreg Roach    /**
20376692c8bSGreg Roach     * Set the sub-menus.
20476692c8bSGreg Roach     *
205a25f0a04SGreg Roach     * @param Menu[] $submenus
206a25f0a04SGreg Roach     *
207a25f0a04SGreg Roach     * @return $this
208a25f0a04SGreg Roach     */
2098f53f488SRico Sonntag    public function setSubmenus(array $submenus): self
210c1010edaSGreg Roach    {
211a25f0a04SGreg Roach        $this->submenus = $submenus;
212a25f0a04SGreg Roach
213a25f0a04SGreg Roach        return $this;
214a25f0a04SGreg Roach    }
215a25f0a04SGreg Roach}
216