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