xref: /webtrees/app/Module/LocationListModule.php (revision 158900c2b212361851fcf0964e99084458ce1b3a)
1e8ded2caSGreg Roach<?php
2e8ded2caSGreg Roach
3e8ded2caSGreg Roach/**
4e8ded2caSGreg Roach * webtrees: online genealogy
5e8ded2caSGreg Roach * Copyright (C) 2021 webtrees development team
6e8ded2caSGreg Roach * This program is free software: you can redistribute it and/or modify
7e8ded2caSGreg Roach * it under the terms of the GNU General Public License as published by
8e8ded2caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e8ded2caSGreg Roach * (at your option) any later version.
10e8ded2caSGreg Roach * This program is distributed in the hope that it will be useful,
11e8ded2caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e8ded2caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e8ded2caSGreg Roach * GNU General Public License for more details.
14e8ded2caSGreg Roach * You should have received a copy of the GNU General Public License
15e8ded2caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e8ded2caSGreg Roach */
17e8ded2caSGreg Roach
18e8ded2caSGreg Roachdeclare(strict_types=1);
19e8ded2caSGreg Roach
20e8ded2caSGreg Roachnamespace Fisharebest\Webtrees\Module;
21e8ded2caSGreg Roach
22e8ded2caSGreg Roachuse Aura\Router\RouterContainer;
23e8ded2caSGreg Roachuse Fisharebest\Webtrees\Auth;
24e8ded2caSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
25e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
26e8ded2caSGreg Roachuse Fisharebest\Webtrees\Location;
27e8ded2caSGreg Roachuse Fisharebest\Webtrees\Registry;
28e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
30e8ded2caSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
31e8ded2caSGreg Roachuse Psr\Http\Message\ResponseInterface;
32e8ded2caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33e8ded2caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
34e8ded2caSGreg Roach
35e8ded2caSGreg Roachuse function app;
36e8ded2caSGreg Roachuse function assert;
37e8ded2caSGreg Roachuse function redirect;
38e8ded2caSGreg Roach
39e8ded2caSGreg Roach/**
40e8ded2caSGreg Roach * Class LocationListModule
41e8ded2caSGreg Roach */
42e8ded2caSGreg Roachclass LocationListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
43e8ded2caSGreg Roach{
44e8ded2caSGreg Roach    use ModuleListTrait;
45e8ded2caSGreg Roach
46e8ded2caSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/location-list';
47e8ded2caSGreg Roach
48e8ded2caSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
4933c746f1SGreg Roach    protected int $access_level = Auth::PRIV_USER;
50e8ded2caSGreg Roach
51e8ded2caSGreg Roach    /**
52e8ded2caSGreg Roach     * Initialization.
53e8ded2caSGreg Roach     *
54e8ded2caSGreg Roach     * @return void
55e8ded2caSGreg Roach     */
56e8ded2caSGreg Roach    public function boot(): void
57e8ded2caSGreg Roach    {
58*158900c2SGreg Roach        Registry::routeFactory()->routeMap()
59e8ded2caSGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
60e8ded2caSGreg Roach    }
61e8ded2caSGreg Roach
62e8ded2caSGreg Roach    /**
63e8ded2caSGreg Roach     * How should this module be identified in the control panel, etc.?
64e8ded2caSGreg Roach     *
65e8ded2caSGreg Roach     * @return string
66e8ded2caSGreg Roach     */
67e8ded2caSGreg Roach    public function title(): string
68e8ded2caSGreg Roach    {
69e8ded2caSGreg Roach        /* I18N: Name of a module/list */
70e8ded2caSGreg Roach        return I18N::translate('Locations');
71e8ded2caSGreg Roach    }
72e8ded2caSGreg Roach
73e8ded2caSGreg Roach    /**
74e8ded2caSGreg Roach     * A sentence describing what this module does.
75e8ded2caSGreg Roach     *
76e8ded2caSGreg Roach     * @return string
77e8ded2caSGreg Roach     */
78e8ded2caSGreg Roach    public function description(): string
79e8ded2caSGreg Roach    {
8096a1b88cSGreg Roach        /* I18N: Description of the “Locations” module */
81e8ded2caSGreg Roach        return I18N::translate('A list of locations.');
82e8ded2caSGreg Roach    }
83e8ded2caSGreg Roach
84e8ded2caSGreg Roach    /**
85e8ded2caSGreg Roach     * CSS class for the URL.
86e8ded2caSGreg Roach     *
87e8ded2caSGreg Roach     * @return string
88e8ded2caSGreg Roach     */
89e8ded2caSGreg Roach    public function listMenuClass(): string
90e8ded2caSGreg Roach    {
91e8ded2caSGreg Roach        return 'menu-list-loc';
92e8ded2caSGreg Roach    }
93e8ded2caSGreg Roach
94e8ded2caSGreg Roach    /**
95e8ded2caSGreg Roach     * @return array<string>
96e8ded2caSGreg Roach     */
97e8ded2caSGreg Roach    public function listUrlAttributes(): array
98e8ded2caSGreg Roach    {
99e8ded2caSGreg Roach        return [];
100e8ded2caSGreg Roach    }
101e8ded2caSGreg Roach
102e8ded2caSGreg Roach    /**
103e8ded2caSGreg Roach     * @param Tree $tree
104e8ded2caSGreg Roach     *
105e8ded2caSGreg Roach     * @return bool
106e8ded2caSGreg Roach     */
107e8ded2caSGreg Roach    public function listIsEmpty(Tree $tree): bool
108e8ded2caSGreg Roach    {
109e8ded2caSGreg Roach        return !DB::table('other')
110e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
111e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
112e8ded2caSGreg Roach            ->exists();
113e8ded2caSGreg Roach    }
114e8ded2caSGreg Roach
115e8ded2caSGreg Roach    /**
116e8ded2caSGreg Roach     * Handle URLs generated by older versions of webtrees
117e8ded2caSGreg Roach     *
118e8ded2caSGreg Roach     * @param ServerRequestInterface $request
119e8ded2caSGreg Roach     *
120e8ded2caSGreg Roach     * @return ResponseInterface
121e8ded2caSGreg Roach     */
122e8ded2caSGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
123e8ded2caSGreg Roach    {
124b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
125b55cbc6bSGreg Roach
126b55cbc6bSGreg Roach        return redirect($this->listUrl($tree, $request->getQueryParams()));
127e8ded2caSGreg Roach    }
128e8ded2caSGreg Roach
129e8ded2caSGreg Roach    /**
130e8ded2caSGreg Roach     * @param Tree                                      $tree
13176d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
132e8ded2caSGreg Roach     *
133e8ded2caSGreg Roach     * @return string
134e8ded2caSGreg Roach     */
135e8ded2caSGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
136e8ded2caSGreg Roach    {
137e8ded2caSGreg Roach        $parameters['tree'] = $tree->name();
138e8ded2caSGreg Roach
139e8ded2caSGreg Roach        return route(static::class, $parameters);
140e8ded2caSGreg Roach    }
141e8ded2caSGreg Roach
142e8ded2caSGreg Roach    /**
143e8ded2caSGreg Roach     * @param ServerRequestInterface $request
144e8ded2caSGreg Roach     *
145e8ded2caSGreg Roach     * @return ResponseInterface
146e8ded2caSGreg Roach     */
147e8ded2caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
148e8ded2caSGreg Roach    {
149b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
150b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
151e8ded2caSGreg Roach
152e8ded2caSGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
153e8ded2caSGreg Roach
154e8ded2caSGreg Roach        $locations = DB::table('other')
155e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
156e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
157e8ded2caSGreg Roach            ->get()
158e8ded2caSGreg Roach            ->map(Registry::locationFactory()->mapper($tree))
159e8ded2caSGreg Roach            ->filter(GedcomRecord::accessFilter());
160e8ded2caSGreg Roach
161e8ded2caSGreg Roach        return $this->viewResponse('modules/location-list/page', [
162e8ded2caSGreg Roach            'locations' => $locations,
163e8ded2caSGreg Roach            'title'     => I18N::translate('Locations'),
164e8ded2caSGreg Roach            'tree'      => $tree,
165e8ded2caSGreg Roach        ]);
166e8ded2caSGreg Roach    }
167e8ded2caSGreg Roach}
168