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