xref: /webtrees/app/Module/ModuleListTrait.php (revision 81b514b4672980e5db010e9d89b55eaf131e798f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Menu;
23use Fisharebest\Webtrees\Tree;
24
25/**
26 * Trait ModuleListTrait - default implementation of ModuleListInterface
27 */
28trait ModuleListTrait
29{
30    /**
31     * A unique internal name for this module (based on the installation folder).
32     *
33     * @return string
34     */
35    abstract public function name(): string;
36
37    /**
38     * How should this module be identified in the control panel, etc.?
39     *
40     * @return string
41     */
42    abstract public function title(): string;
43
44    /**
45     * A main menu item for this list, or null if the list is empty.
46     *
47     * @param Tree $tree
48     *
49     * @return Menu|null
50     */
51    public function listMenu(Tree $tree): ?Menu
52    {
53        if ($this->listIsEmpty($tree)) {
54            return null;
55        }
56
57        return new Menu(
58            $this->listTitle(),
59            $this->listUrl($tree),
60            $this->listMenuClass(),
61            $this->listUrlAttributes()
62        );
63    }
64
65    /**
66     * CSS class for the menu.
67     *
68     * @return string
69     */
70    public function listMenuClass(): string
71    {
72        return '';
73    }
74
75    /**
76     * The title for a specific instance of this list.
77     *
78     * @return string
79     */
80    public function listTitle(): string
81    {
82        return $this->title();
83    }
84
85    /**
86     * The URL for a page showing list options.
87     *
88     * @param Tree                                      $tree
89     * @param array<bool|int|string|array<string>|null> $parameters
90     *
91     * @return string
92     */
93    public function listUrl(Tree $tree, array $parameters = []): string
94    {
95        return route('module', [
96                'module' => $this->name(),
97                'action' => 'List',
98                'tree'    => $tree->name(),
99        ] + $parameters);
100    }
101
102    /**
103     * Attributes for the URL.
104     *
105     * @return array<string>
106     */
107    public function listUrlAttributes(): array
108    {
109        return ['rel' => 'nofollow'];
110    }
111
112    /**
113     * @param Tree $tree
114     *
115     * @return bool
116     */
117    public function listIsEmpty(Tree $tree): bool
118    {
119        return false;
120    }
121}
122