xref: /webtrees/app/Module/BranchesListModule.php (revision 66ce3d2365f7eb099897303ad7dc49a8459ceb55)
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\Auth;
23use Fisharebest\Webtrees\Http\Controllers\BranchesController;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\ModuleService;
26use Fisharebest\Webtrees\Tree;
27use InvalidArgumentException;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function assert;
32use function redirect;
33use function route;
34
35/**
36 * Class BranchesListModule
37 */
38class BranchesListModule extends AbstractModule implements ModuleListInterface
39{
40    use ModuleListTrait;
41
42    /**
43     * How should this module be identified in the control panel, etc.?
44     *
45     * @return string
46     */
47    public function title(): string
48    {
49        /* I18N: Name of a module/list */
50        return I18N::translate('Branches');
51    }
52
53    /**
54     * A sentence describing what this module does.
55     *
56     * @return string
57     */
58    public function description(): string
59    {
60        /* I18N: Description of the “BranchesListModule” module */
61        return I18N::translate('A list of branches of a family.');
62    }
63
64    /**
65     * CSS class for the URL.
66     *
67     * @return string
68     */
69    public function listMenuClass(): string
70    {
71        return 'menu-branches';
72    }
73
74    /**
75     * @param Tree    $tree
76     * @param mixed[] $parameters
77     *
78     * @return string
79     */
80    public function listUrl(Tree $tree, array $parameters = []): string
81    {
82        return route('module', [
83                'module' => $this->name(),
84                'action' => 'Page',
85                'tree'    => $tree->name(),
86        ] + $parameters);
87    }
88
89    /**
90     * @param ServerRequestInterface $request
91     *
92     * @return ResponseInterface
93     */
94    public function getPageAction(ServerRequestInterface $request): ResponseInterface
95    {
96        $tree = $request->getAttribute('tree');
97        assert($tree instanceof Tree, new InvalidArgumentException());
98
99        $user = $request->getAttribute('user');
100
101        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
102
103        $listController = new BranchesController(app(ModuleService::class));
104        return $listController->page($request);
105    }
106
107    /**
108     * @param ServerRequestInterface $request
109     *
110     * @return ResponseInterface
111     */
112    public function postPageAction(ServerRequestInterface $request): ResponseInterface
113    {
114        $tree = $request->getAttribute('tree');
115        assert($tree instanceof Tree, new InvalidArgumentException());
116
117        return redirect(route('module', [
118            'module'      => $this->name(),
119            'action'      => 'Page',
120            'surname'     => $request->getParsedBody()['surname'] ?? '',
121            'soundex_dm'  => $request->getParsedBody()['soundex_dm'] ?? '',
122            'soundex_std' => $request->getParsedBody()['soundex_std'] ?? '',
123            'tree'        => $tree->name(),
124        ]));
125    }
126
127    /**
128     * @param ServerRequestInterface $request
129     *
130     * @return ResponseInterface
131     */
132    public function getListAction(ServerRequestInterface $request): ResponseInterface
133    {
134        $tree = $request->getAttribute('tree');
135        assert($tree instanceof Tree, new InvalidArgumentException());
136
137        $user = $request->getAttribute('user');
138
139        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
140
141        return app(BranchesController::class)->list($request);
142    }
143
144    /**
145     * @return string[]
146     */
147    public function listUrlAttributes(): array
148    {
149        return [];
150    }
151}
152