xref: /webtrees/app/Module/LocationListModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
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;
236f4ec3caSGreg 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
41*e873f434SGreg Roach    protected const string 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    public function description(): string
69e8ded2caSGreg Roach    {
7096a1b88cSGreg Roach        /* I18N: Description of the “Locations” module */
71e8ded2caSGreg Roach        return I18N::translate('A list of locations.');
72e8ded2caSGreg Roach    }
73e8ded2caSGreg Roach
74e8ded2caSGreg Roach    /**
75e8ded2caSGreg Roach     * CSS class for the URL.
76e8ded2caSGreg Roach     *
77e8ded2caSGreg Roach     * @return string
78e8ded2caSGreg Roach     */
79e8ded2caSGreg Roach    public function listMenuClass(): string
80e8ded2caSGreg Roach    {
81e8ded2caSGreg Roach        return 'menu-list-loc';
82e8ded2caSGreg Roach    }
83e8ded2caSGreg Roach
84e8ded2caSGreg Roach    /**
85e8ded2caSGreg Roach     * @return array<string>
86e8ded2caSGreg Roach     */
87e8ded2caSGreg Roach    public function listUrlAttributes(): array
88e8ded2caSGreg Roach    {
89e8ded2caSGreg Roach        return [];
90e8ded2caSGreg Roach    }
91e8ded2caSGreg Roach
92e8ded2caSGreg Roach    /**
93e8ded2caSGreg Roach     * @param Tree $tree
94e8ded2caSGreg Roach     *
95e8ded2caSGreg Roach     * @return bool
96e8ded2caSGreg Roach     */
97e8ded2caSGreg Roach    public function listIsEmpty(Tree $tree): bool
98e8ded2caSGreg Roach    {
99e8ded2caSGreg Roach        return !DB::table('other')
100e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
101e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
102e8ded2caSGreg Roach            ->exists();
103e8ded2caSGreg Roach    }
104e8ded2caSGreg Roach
105e8ded2caSGreg Roach    /**
106e8ded2caSGreg Roach     * @param Tree                                      $tree
10776d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
108e8ded2caSGreg Roach     *
109e8ded2caSGreg Roach     * @return string
110e8ded2caSGreg Roach     */
111e8ded2caSGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
112e8ded2caSGreg Roach    {
113e8ded2caSGreg Roach        $parameters['tree'] = $tree->name();
114e8ded2caSGreg Roach
115e8ded2caSGreg Roach        return route(static::class, $parameters);
116e8ded2caSGreg Roach    }
117e8ded2caSGreg Roach
118e8ded2caSGreg Roach    /**
119e8ded2caSGreg Roach     * @param ServerRequestInterface $request
120e8ded2caSGreg Roach     *
121e8ded2caSGreg Roach     * @return ResponseInterface
122e8ded2caSGreg Roach     */
123e8ded2caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
124e8ded2caSGreg Roach    {
125b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
126b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
127e8ded2caSGreg Roach
128e8ded2caSGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
129e8ded2caSGreg Roach
130e8ded2caSGreg Roach        $locations = DB::table('other')
131e8ded2caSGreg Roach            ->where('o_file', '=', $tree->id())
132e8ded2caSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
133e8ded2caSGreg Roach            ->get()
134e8ded2caSGreg Roach            ->map(Registry::locationFactory()->mapper($tree))
135e8ded2caSGreg Roach            ->filter(GedcomRecord::accessFilter());
136e8ded2caSGreg Roach
137e8ded2caSGreg Roach        return $this->viewResponse('modules/location-list/page', [
138e8ded2caSGreg Roach            'locations' => $locations,
139e8ded2caSGreg Roach            'title'     => I18N::translate('Locations'),
140e8ded2caSGreg Roach            'tree'      => $tree,
141e8ded2caSGreg Roach        ]);
142e8ded2caSGreg Roach    }
143e8ded2caSGreg Roach}
144