xref: /webtrees/app/Module/ModuleListTrait.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
567992b6aSRichard Cissee * Copyright (C) 2019 webtrees development team
667992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
767992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
867992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
967992b6aSRichard Cissee * (at your option) any later version.
1067992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1167992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1267992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1367992b6aSRichard Cissee * GNU General Public License for more details.
1467992b6aSRichard Cissee * You should have received a copy of the GNU General Public License
1567992b6aSRichard Cissee * along with this program. If not, see <http://www.gnu.org/licenses/>.
1667992b6aSRichard Cissee */
17*fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
2267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Menu;
2367992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
2467992b6aSRichard Cissee
2567992b6aSRichard Cissee/**
2667992b6aSRichard Cissee * Trait ModuleListTrait - default implementation of ModuleListInterface
2767992b6aSRichard Cissee */
2867992b6aSRichard Cisseetrait ModuleListTrait
2967992b6aSRichard Cissee{
3067992b6aSRichard Cissee    /**
31e284d69fSGreg Roach     * @return string
32e284d69fSGreg Roach     */
33e284d69fSGreg Roach    abstract public function name(): string;
34e284d69fSGreg Roach
35e284d69fSGreg Roach    /**
36e284d69fSGreg Roach     * @return string
37e284d69fSGreg Roach     */
38e284d69fSGreg Roach    abstract public function title(): string;
39e284d69fSGreg Roach
40e284d69fSGreg Roach    /**
4167992b6aSRichard Cissee     * A main menu item for this list, or null if the list is empty.
4267992b6aSRichard Cissee     *
4367992b6aSRichard Cissee     * @param Tree $tree
4467992b6aSRichard Cissee     *
4567992b6aSRichard Cissee     * @return Menu|null
4667992b6aSRichard Cissee     */
4767992b6aSRichard Cissee    public function listMenu(Tree $tree): ?Menu
4867992b6aSRichard Cissee    {
4967992b6aSRichard Cissee        if ($this->listIsEmpty($tree)) {
5067992b6aSRichard Cissee            return null;
5167992b6aSRichard Cissee        }
5267992b6aSRichard Cissee
5367992b6aSRichard Cissee        return new Menu(
5455f111b1SRichard Cissée            $this->listTitle(),
5567992b6aSRichard Cissee            $this->listUrl($tree),
5667992b6aSRichard Cissee            $this->listMenuClass(),
5767992b6aSRichard Cissee            $this->listUrlAttributes()
5867992b6aSRichard Cissee        );
5967992b6aSRichard Cissee    }
6067992b6aSRichard Cissee
6167992b6aSRichard Cissee    /**
6267992b6aSRichard Cissee     * CSS class for the menu.
6367992b6aSRichard Cissee     *
6467992b6aSRichard Cissee     * @return string
6567992b6aSRichard Cissee     */
6667992b6aSRichard Cissee    public function listMenuClass(): string
6767992b6aSRichard Cissee    {
6867992b6aSRichard Cissee        return '';
6967992b6aSRichard Cissee    }
7067992b6aSRichard Cissee
7167992b6aSRichard Cissee    /**
7267992b6aSRichard Cissee     * The title for a specific instance of this list.
7367992b6aSRichard Cissee     *
7467992b6aSRichard Cissee     * @return string
7567992b6aSRichard Cissee     */
7667992b6aSRichard Cissee    public function listTitle(): string
7767992b6aSRichard Cissee    {
7867992b6aSRichard Cissee        return $this->title();
7967992b6aSRichard Cissee    }
8067992b6aSRichard Cissee
8167992b6aSRichard Cissee    /**
8267992b6aSRichard Cissee     * The URL for a page showing list options.
8367992b6aSRichard Cissee     *
8467992b6aSRichard Cissee     * @param Tree       $tree
8567992b6aSRichard Cissee     * @param string[]   $parameters
8667992b6aSRichard Cissee     *
8767992b6aSRichard Cissee     * @return string
8867992b6aSRichard Cissee     */
8967992b6aSRichard Cissee    public function listUrl(Tree $tree, array $parameters = []): string
9067992b6aSRichard Cissee    {
9167992b6aSRichard Cissee        return route('module', [
9267992b6aSRichard Cissee                'module' => $this->name(),
9367992b6aSRichard Cissee                'action' => 'List',
949022ab66SGreg Roach                'tree'    => $tree->name(),
9567992b6aSRichard Cissee        ] + $parameters);
9667992b6aSRichard Cissee    }
9767992b6aSRichard Cissee
9867992b6aSRichard Cissee    /**
9967992b6aSRichard Cissee     * Attributes for the URL.
10067992b6aSRichard Cissee     *
10167992b6aSRichard Cissee     * @return string[]
10267992b6aSRichard Cissee     */
10367992b6aSRichard Cissee    public function listUrlAttributes(): array
10467992b6aSRichard Cissee    {
10567992b6aSRichard Cissee        return ['rel' => 'nofollow'];
10667992b6aSRichard Cissee    }
10767992b6aSRichard Cissee
1084db4b4a9SGreg Roach    /**
1094db4b4a9SGreg Roach     * @param Tree $tree
1104db4b4a9SGreg Roach     *
1114db4b4a9SGreg Roach     * @return bool
1124db4b4a9SGreg Roach     */
11367992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
11467992b6aSRichard Cissee    {
11567992b6aSRichard Cissee        return false;
11667992b6aSRichard Cissee    }
11767992b6aSRichard Cissee}
118