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