xref: /webtrees/app/Http/RequestHandlers/MapDataList.php (revision 842a873c0b92c28d4af3fa9db0e9156057005569)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
25use Fisharebest\Webtrees\PlaceLocation;
26use Fisharebest\Webtrees\Services\MapDataService;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Services\TreeService;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function array_reverse;
34use function e;
35use function redirect;
36use function route;
37
38/**
39 * Show a list of map data.
40 */
41class MapDataList implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    /** @var MapDataService */
46    private $map_data_service;
47
48    /** @var ModuleService */
49    private $module_service;
50
51    /** @var TreeService */
52    private $tree_service;
53
54    /**
55     * Dependency injection.
56     *
57     * @param MapDataService $map_data_service
58     * @param ModuleService  $module_service
59     * @param TreeService    $tree_service
60     */
61    public function __construct(
62        MapDataService $map_data_service,
63        ModuleService $module_service,
64        TreeService $tree_service
65    ) {
66        $this->map_data_service = $map_data_service;
67        $this->module_service   = $module_service;
68        $this->tree_service     = $tree_service;
69    }
70
71    /**
72     * @param ServerRequestInterface $request
73     *
74     * @return ResponseInterface
75     */
76    public function handle(ServerRequestInterface $request): ResponseInterface
77    {
78        $parent_id = $request->getAttribute('parent_id');
79
80        if ($parent_id === null) {
81            $parent = new PlaceLocation('');
82        } else {
83            $parent_id = (int) $parent_id;
84            $parent = $this->map_data_service->findById($parent_id);
85        }
86
87        // Request for a non-existent location?
88        if ($parent_id !== null &&  $parent->id() === null) {
89            return redirect(route(__CLASS__));
90        }
91
92        // Automatically import any new/missing places.
93        $this->map_data_service->importMissingLocations();
94
95        $breadcrumbs = [];
96
97        if ($parent->id() !== null) {
98            $breadcrumbs[] = e($parent->locationName());
99        }
100
101        $tmp = $parent->parent();
102
103        while ($tmp->id() !== null) {
104            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
105
106            $tmp = $tmp->parent();
107        }
108
109        $title = I18N::translate('Geographic data');
110
111        $breadcrumbs[route(__CLASS__)] = $title;
112
113        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
114
115        $list_module = $this->module_service
116            ->findByInterface(PlaceHierarchyListModule::class)
117            ->first();
118
119        $this->layout = 'layouts/administration';
120
121        return $this->viewResponse('admin/locations', [
122            'active'       => $this->map_data_service->activePlaces($parent),
123            'all_trees'    => $this->tree_service->all(),
124            'breadcrumbs'  => array_reverse($breadcrumbs),
125            'parent_id'    => $parent_id,
126            'placelist'    => $this->map_data_service->getPlaceListLocation($parent_id),
127            'list_module'  => $list_module,
128            'title'        => $title,
129        ]);
130    }
131}
132