xref: /webtrees/app/Module/LocationListModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
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 string 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    public function description(): string
69    {
70        /* I18N: Description of the “Locations” module */
71        return I18N::translate('A list of locations.');
72    }
73
74    /**
75     * CSS class for the URL.
76     *
77     * @return string
78     */
79    public function listMenuClass(): string
80    {
81        return 'menu-list-loc';
82    }
83
84    /**
85     * @return array<string>
86     */
87    public function listUrlAttributes(): array
88    {
89        return [];
90    }
91
92    /**
93     * @param Tree $tree
94     *
95     * @return bool
96     */
97    public function listIsEmpty(Tree $tree): bool
98    {
99        return !DB::table('other')
100            ->where('o_file', '=', $tree->id())
101            ->where('o_type', '=', Location::RECORD_TYPE)
102            ->exists();
103    }
104
105    /**
106     * @param Tree                                      $tree
107     * @param array<bool|int|string|array<string>|null> $parameters
108     *
109     * @return string
110     */
111    public function listUrl(Tree $tree, array $parameters = []): string
112    {
113        $parameters['tree'] = $tree->name();
114
115        return route(static::class, $parameters);
116    }
117
118    /**
119     * @param ServerRequestInterface $request
120     *
121     * @return ResponseInterface
122     */
123    public function handle(ServerRequestInterface $request): ResponseInterface
124    {
125        $tree = Validator::attributes($request)->tree();
126        $user = Validator::attributes($request)->user();
127
128        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
129
130        $locations = DB::table('other')
131            ->where('o_file', '=', $tree->id())
132            ->where('o_type', '=', Location::RECORD_TYPE)
133            ->get()
134            ->map(Registry::locationFactory()->mapper($tree))
135            ->filter(GedcomRecord::accessFilter());
136
137        return $this->viewResponse('modules/location-list/page', [
138            'locations' => $locations,
139            'title'     => I18N::translate('Locations'),
140            'tree'      => $tree,
141        ]);
142    }
143}
144