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