xref: /webtrees/app/Module/LocationListModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
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\Contracts\UserInterface;
25e8ded2caSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
26e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
27e8ded2caSGreg Roachuse Fisharebest\Webtrees\Location;
28e8ded2caSGreg Roachuse Fisharebest\Webtrees\Registry;
29e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
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    {
58e8ded2caSGreg Roach        $router_container = app(RouterContainer::class);
59e8ded2caSGreg Roach        assert($router_container instanceof RouterContainer);
60e8ded2caSGreg Roach
61e8ded2caSGreg Roach        $router_container->getMap()
62e8ded2caSGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
63e8ded2caSGreg Roach    }
64e8ded2caSGreg Roach
65e8ded2caSGreg Roach    /**
66e8ded2caSGreg Roach     * How should this module be identified in the control panel, etc.?
67e8ded2caSGreg Roach     *
68e8ded2caSGreg Roach     * @return string
69e8ded2caSGreg Roach     */
70e8ded2caSGreg Roach    public function title(): string
71e8ded2caSGreg Roach    {
72e8ded2caSGreg Roach        /* I18N: Name of a module/list */
73e8ded2caSGreg Roach        return I18N::translate('Locations');
74e8ded2caSGreg Roach    }
75e8ded2caSGreg Roach
76e8ded2caSGreg Roach    /**
77e8ded2caSGreg Roach     * A sentence describing what this module does.
78e8ded2caSGreg Roach     *
79e8ded2caSGreg Roach     * @return string
80e8ded2caSGreg Roach     */
81e8ded2caSGreg Roach    public function description(): string
82e8ded2caSGreg Roach    {
8396a1b88cSGreg Roach        /* I18N: Description of the “Locations” module */
84e8ded2caSGreg Roach        return I18N::translate('A list of locations.');
85e8ded2caSGreg Roach    }
86e8ded2caSGreg Roach
87e8ded2caSGreg Roach    /**
88e8ded2caSGreg Roach     * CSS class for the URL.
89e8ded2caSGreg Roach     *
90e8ded2caSGreg Roach     * @return string
91e8ded2caSGreg Roach     */
92e8ded2caSGreg Roach    public function listMenuClass(): string
93e8ded2caSGreg Roach    {
94e8ded2caSGreg Roach        return 'menu-list-loc';
95e8ded2caSGreg Roach    }
96e8ded2caSGreg Roach
97e8ded2caSGreg Roach    /**
98e8ded2caSGreg Roach     * @return array<string>
99e8ded2caSGreg Roach     */
100e8ded2caSGreg Roach    public function listUrlAttributes(): array
101e8ded2caSGreg Roach    {
102e8ded2caSGreg Roach        return [];
103e8ded2caSGreg Roach    }
104e8ded2caSGreg Roach
105e8ded2caSGreg Roach    /**
106e8ded2caSGreg Roach     * @param Tree $tree
107e8ded2caSGreg Roach     *
108e8ded2caSGreg Roach     * @return bool
109e8ded2caSGreg Roach     */
110e8ded2caSGreg Roach    public function listIsEmpty(Tree $tree): bool
111e8ded2caSGreg Roach    {
112e8ded2caSGreg Roach        return !DB::table('other')
113e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
114e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
115e8ded2caSGreg Roach            ->exists();
116e8ded2caSGreg Roach    }
117e8ded2caSGreg Roach
118e8ded2caSGreg Roach    /**
119e8ded2caSGreg Roach     * Handle URLs generated by older versions of webtrees
120e8ded2caSGreg Roach     *
121e8ded2caSGreg Roach     * @param ServerRequestInterface $request
122e8ded2caSGreg Roach     *
123e8ded2caSGreg Roach     * @return ResponseInterface
124e8ded2caSGreg Roach     */
125e8ded2caSGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
126e8ded2caSGreg Roach    {
127e8ded2caSGreg Roach        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
128e8ded2caSGreg Roach    }
129e8ded2caSGreg Roach
130e8ded2caSGreg Roach    /**
131e8ded2caSGreg Roach     * @param Tree                                      $tree
132*76d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
133e8ded2caSGreg Roach     *
134e8ded2caSGreg Roach     * @return string
135e8ded2caSGreg Roach     */
136e8ded2caSGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
137e8ded2caSGreg Roach    {
138e8ded2caSGreg Roach        $parameters['tree'] = $tree->name();
139e8ded2caSGreg Roach
140e8ded2caSGreg Roach        return route(static::class, $parameters);
141e8ded2caSGreg Roach    }
142e8ded2caSGreg Roach
143e8ded2caSGreg Roach    /**
144e8ded2caSGreg Roach     * @param ServerRequestInterface $request
145e8ded2caSGreg Roach     *
146e8ded2caSGreg Roach     * @return ResponseInterface
147e8ded2caSGreg Roach     */
148e8ded2caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
149e8ded2caSGreg Roach    {
150e8ded2caSGreg Roach        $tree = $request->getAttribute('tree');
151e8ded2caSGreg Roach        assert($tree instanceof Tree);
152e8ded2caSGreg Roach
153e8ded2caSGreg Roach        $user = $request->getAttribute('user');
154e8ded2caSGreg Roach        assert($user instanceof UserInterface);
155e8ded2caSGreg Roach
156e8ded2caSGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
157e8ded2caSGreg Roach
158e8ded2caSGreg Roach        $locations = DB::table('other')
159e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
160e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
161e8ded2caSGreg Roach            ->get()
162e8ded2caSGreg Roach            ->map(Registry::locationFactory()->mapper($tree))
163e8ded2caSGreg Roach            ->filter(GedcomRecord::accessFilter());
164e8ded2caSGreg Roach
165e8ded2caSGreg Roach        return $this->viewResponse('modules/location-list/page', [
166e8ded2caSGreg Roach            'locations' => $locations,
167e8ded2caSGreg Roach            'title'     => I18N::translate('Locations'),
168e8ded2caSGreg Roach            'tree'      => $tree,
169e8ded2caSGreg Roach        ]);
170e8ded2caSGreg Roach    }
171e8ded2caSGreg Roach}
172