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