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