xref: /webtrees/app/Menu.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg 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
1589f7189bSGreg 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     */
55*24f2a3afSGreg Roach    public function __construct(
56*24f2a3afSGreg Roach        string $label,
57*24f2a3afSGreg Roach        string $link = '#',
58*24f2a3afSGreg Roach        string $class = '',
59*24f2a3afSGreg Roach        array $attrs = [],
60*24f2a3afSGreg Roach        array $submenus = []
61*24f2a3afSGreg Roach    ) {
62a25f0a04SGreg Roach        $this
63a25f0a04SGreg Roach            ->setLabel($label)
64a25f0a04SGreg Roach            ->setLink($link)
653d0a6fa1SGreg Roach            ->setClass($class)
663cf92ae2SGreg Roach            ->setAttrs($attrs)
67a25f0a04SGreg Roach            ->setSubmenus($submenus);
68a25f0a04SGreg Roach    }
69a25f0a04SGreg Roach
70a25f0a04SGreg Roach    /**
7115d603e7SGreg Roach     * Render this menu using Bootstrap4 markup
72a25f0a04SGreg Roach     *
73a25f0a04SGreg Roach     * @return string
74f78837dcSGreg Roach     *
75f78837dcSGreg Roach     * @deprecated since 2.0.2.  Will be removed in 2.1.0
76a25f0a04SGreg Roach     */
77e364afe4SGreg Roach    public function bootstrap4(): string
78c1010edaSGreg Roach    {
79f78837dcSGreg Roach        trigger_error(
80f78837dcSGreg Roach            'Menu::bootstrap4() is deprecated.  Use the view(components/menu-item) instead',
81f78837dcSGreg Roach            E_USER_DEPRECATED
82f78837dcSGreg Roach        );
83a25f0a04SGreg Roach
84f78837dcSGreg Roach        return view('components/menu-item', ['menu' => $this]);
85a25f0a04SGreg Roach    }
86a25f0a04SGreg Roach
87a25f0a04SGreg Roach    /**
883cf92ae2SGreg Roach     * Get the optional attributes.
893cf92ae2SGreg Roach     *
90*24f2a3afSGreg Roach     * @return array<string>
913cf92ae2SGreg Roach     */
928f53f488SRico Sonntag    public function getAttrs(): array
93c1010edaSGreg Roach    {
943cf92ae2SGreg Roach        return $this->attrs;
953cf92ae2SGreg Roach    }
963cf92ae2SGreg Roach
973cf92ae2SGreg Roach    /**
983cf92ae2SGreg Roach     * Set the optional attributes.
993cf92ae2SGreg Roach     *
1003cf92ae2SGreg Roach     * @param string[] $attrs
1013cf92ae2SGreg Roach     *
1023cf92ae2SGreg Roach     * @return $this
1033cf92ae2SGreg Roach     */
1048f53f488SRico Sonntag    public function setAttrs(array $attrs): self
105c1010edaSGreg Roach    {
1063cf92ae2SGreg Roach        $this->attrs = $attrs;
1073cf92ae2SGreg Roach
1083cf92ae2SGreg Roach        return $this;
1093cf92ae2SGreg Roach    }
1103cf92ae2SGreg Roach
1113cf92ae2SGreg Roach    /**
11276692c8bSGreg Roach     * Get the class.
11376692c8bSGreg Roach     *
114a25f0a04SGreg Roach     * @return string
115a25f0a04SGreg Roach     */
1168f53f488SRico Sonntag    public function getClass(): string
117c1010edaSGreg Roach    {
1183d0a6fa1SGreg Roach        return $this->class;
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach    /**
12276692c8bSGreg Roach     * Set the class.
12376692c8bSGreg Roach     *
1243d0a6fa1SGreg Roach     * @param string $class
125a25f0a04SGreg Roach     *
126a25f0a04SGreg Roach     * @return $this
127a25f0a04SGreg Roach     */
128*24f2a3afSGreg Roach    public function setClass(string $class): self
129c1010edaSGreg Roach    {
1303d0a6fa1SGreg Roach        $this->class = $class;
131a25f0a04SGreg Roach
132a25f0a04SGreg Roach        return $this;
133a25f0a04SGreg Roach    }
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach    /**
13676692c8bSGreg Roach     * Get the label.
13776692c8bSGreg Roach     *
138a25f0a04SGreg Roach     * @return string
139a25f0a04SGreg Roach     */
1408f53f488SRico Sonntag    public function getLabel(): string
141c1010edaSGreg Roach    {
142a25f0a04SGreg Roach        return $this->label;
143a25f0a04SGreg Roach    }
144a25f0a04SGreg Roach
145a25f0a04SGreg Roach    /**
14676692c8bSGreg Roach     * Set the label.
14776692c8bSGreg Roach     *
148a25f0a04SGreg Roach     * @param string $label
149a25f0a04SGreg Roach     *
150a25f0a04SGreg Roach     * @return $this
151a25f0a04SGreg Roach     */
152*24f2a3afSGreg Roach    public function setLabel(string $label): self
153c1010edaSGreg Roach    {
154a25f0a04SGreg Roach        $this->label = $label;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach        return $this;
157a25f0a04SGreg Roach    }
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach    /**
16076692c8bSGreg Roach     * Get the link.
1613cf92ae2SGreg Roach     *
162a25f0a04SGreg Roach     * @return string
163a25f0a04SGreg Roach     */
1648f53f488SRico Sonntag    public function getLink(): string
165c1010edaSGreg Roach    {
166a25f0a04SGreg Roach        return $this->link;
167a25f0a04SGreg Roach    }
168a25f0a04SGreg Roach
169a25f0a04SGreg Roach    /**
17076692c8bSGreg Roach     * Set the link.
17176692c8bSGreg Roach     *
172a25f0a04SGreg Roach     * @param string $link
173a25f0a04SGreg Roach     *
174a25f0a04SGreg Roach     * @return $this
175a25f0a04SGreg Roach     */
176*24f2a3afSGreg Roach    public function setLink(string $link): self
177c1010edaSGreg Roach    {
178a25f0a04SGreg Roach        $this->link = $link;
179a25f0a04SGreg Roach
180a25f0a04SGreg Roach        return $this;
181a25f0a04SGreg Roach    }
182a25f0a04SGreg Roach
183a25f0a04SGreg Roach    /**
184a25f0a04SGreg Roach     * Add a submenu to this menu
185a25f0a04SGreg Roach     *
186ecb5b5e9SGreg Roach     * @param Menu $menu
187ecb5b5e9SGreg Roach     *
188ecb5b5e9SGreg Roach     * @return $this
189a25f0a04SGreg Roach     */
190*24f2a3afSGreg Roach    public function addSubmenu(Menu $menu): self
191c1010edaSGreg Roach    {
192a25f0a04SGreg Roach        $this->submenus[] = $menu;
193ecb5b5e9SGreg Roach
194ecb5b5e9SGreg Roach        return $this;
195a25f0a04SGreg Roach    }
196a25f0a04SGreg Roach
197a25f0a04SGreg Roach    /**
19876692c8bSGreg Roach     * Get the sub-menus.
19976692c8bSGreg Roach     *
200a25f0a04SGreg Roach     * @return Menu[]
201a25f0a04SGreg Roach     */
2028f53f488SRico Sonntag    public function getSubmenus(): array
203c1010edaSGreg Roach    {
204a25f0a04SGreg Roach        return $this->submenus;
205a25f0a04SGreg Roach    }
206a25f0a04SGreg Roach
207a25f0a04SGreg Roach    /**
20876692c8bSGreg Roach     * Set the sub-menus.
20976692c8bSGreg Roach     *
210a25f0a04SGreg Roach     * @param Menu[] $submenus
211a25f0a04SGreg Roach     *
212a25f0a04SGreg Roach     * @return $this
213a25f0a04SGreg Roach     */
2148f53f488SRico Sonntag    public function setSubmenus(array $submenus): self
215c1010edaSGreg Roach    {
216a25f0a04SGreg Roach        $this->submenus = $submenus;
217a25f0a04SGreg Roach
218a25f0a04SGreg Roach        return $this;
219a25f0a04SGreg Roach    }
220a25f0a04SGreg Roach}
221