xref: /webtrees/app/Module/LocationListModule.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\DB;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Location;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\Validator;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34/**
35 * Class LocationListModule
36 */
37class LocationListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
38{
39    use ModuleListTrait;
40
41    protected const ROUTE_URL = '/tree/{tree}/location-list';
42
43    /** @var int The default access level for this module.  It can be changed in the control panel. */
44    protected int $access_level = Auth::PRIV_USER;
45
46    /**
47     * Initialization.
48     *
49     * @return void
50     */
51    public function boot(): void
52    {
53        Registry::routeFactory()->routeMap()
54            ->get(static::class, static::ROUTE_URL, $this);
55    }
56
57    /**
58     * How should this module be identified in the control panel, etc.?
59     *
60     * @return string
61     */
62    public function title(): string
63    {
64        /* I18N: Name of a module/list */
65        return I18N::translate('Locations');
66    }
67
68    /**
69     * A sentence describing what this module does.
70     *
71     * @return string
72     */
73    public function description(): string
74    {
75        /* I18N: Description of the “Locations” module */
76        return I18N::translate('A list of locations.');
77    }
78
79    /**
80     * CSS class for the URL.
81     *
82     * @return string
83     */
84    public function listMenuClass(): string
85    {
86        return 'menu-list-loc';
87    }
88
89    /**
90     * @return array<string>
91     */
92    public function listUrlAttributes(): array
93    {
94        return [];
95    }
96
97    /**
98     * @param Tree $tree
99     *
100     * @return bool
101     */
102    public function listIsEmpty(Tree $tree): bool
103    {
104        return !DB::table('other')
105            ->where('o_file', '=', $tree->id())
106            ->where('o_type', '=', Location::RECORD_TYPE)
107            ->exists();
108    }
109
110    /**
111     * @param Tree                                      $tree
112     * @param array<bool|int|string|array<string>|null> $parameters
113     *
114     * @return string
115     */
116    public function listUrl(Tree $tree, array $parameters = []): string
117    {
118        $parameters['tree'] = $tree->name();
119
120        return route(static::class, $parameters);
121    }
122
123    /**
124     * @param ServerRequestInterface $request
125     *
126     * @return ResponseInterface
127     */
128    public function handle(ServerRequestInterface $request): ResponseInterface
129    {
130        $tree = Validator::attributes($request)->tree();
131        $user = Validator::attributes($request)->user();
132
133        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
134
135        $locations = DB::table('other')
136            ->where('o_file', '=', $tree->id())
137            ->where('o_type', '=', Location::RECORD_TYPE)
138            ->get()
139            ->map(Registry::locationFactory()->mapper($tree))
140            ->filter(GedcomRecord::accessFilter());
141
142        return $this->viewResponse('modules/location-list/page', [
143            'locations' => $locations,
144            'title'     => I18N::translate('Locations'),
145            'tree'      => $tree,
146        ]);
147    }
148}
149