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