xref: /webtrees/app/Module/ModuleListTrait.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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     * @return string
32     */
33    abstract public function name(): string;
34
35    /**
36     * @return string
37     */
38    abstract public function title(): string;
39
40    /**
41     * A main menu item for this list, or null if the list is empty.
42     *
43     * @param Tree $tree
44     *
45     * @return Menu|null
46     */
47    public function listMenu(Tree $tree): ?Menu
48    {
49        if ($this->listIsEmpty($tree)) {
50            return null;
51        }
52
53        return new Menu(
54            $this->listTitle(),
55            $this->listUrl($tree),
56            $this->listMenuClass(),
57            $this->listUrlAttributes()
58        );
59    }
60
61    /**
62     * CSS class for the menu.
63     *
64     * @return string
65     */
66    public function listMenuClass(): string
67    {
68        return '';
69    }
70
71    /**
72     * The title for a specific instance of this list.
73     *
74     * @return string
75     */
76    public function listTitle(): string
77    {
78        return $this->title();
79    }
80
81    /**
82     * The URL for a page showing list options.
83     *
84     * @param Tree    $tree
85     * @param mixed[] $parameters
86     *
87     * @return string
88     */
89    public function listUrl(Tree $tree, array $parameters = []): string
90    {
91        return route('module', [
92                'module' => $this->name(),
93                'action' => 'List',
94                'tree'    => $tree->name(),
95        ] + $parameters);
96    }
97
98    /**
99     * Attributes for the URL.
100     *
101     * @return string[]
102     */
103    public function listUrlAttributes(): array
104    {
105        return ['rel' => 'nofollow'];
106    }
107
108    /**
109     * @param Tree $tree
110     *
111     * @return bool
112     */
113    public function listIsEmpty(Tree $tree): bool
114    {
115        return false;
116    }
117}
118