xref: /webtrees/app/Http/RequestHandlers/MapDataList.php (revision c43c166ecb395e5c9f57dc8e20d9f6f7a27bf410)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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\Services\MapDataService;
26use Fisharebest\Webtrees\Services\ModuleService;
27use Fisharebest\Webtrees\Services\TreeService;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_reverse;
33use function redirect;
34use function route;
35
36/**
37 * Show a list of map data.
38 */
39class MapDataList implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    /** @var MapDataService */
44    private $map_data_service;
45
46    /** @var ModuleService */
47    private $module_service;
48
49    /** @var TreeService */
50    private $tree_service;
51
52    /**
53     * Dependency injection.
54     *
55     * @param MapDataService $map_data_service
56     * @param ModuleService  $module_service
57     * @param TreeService    $tree_service
58     */
59    public function __construct(
60        MapDataService $map_data_service,
61        ModuleService $module_service,
62        TreeService $tree_service
63    ) {
64        $this->map_data_service = $map_data_service;
65        $this->module_service   = $module_service;
66        $this->tree_service     = $tree_service;
67    }
68
69    /**
70     * @param ServerRequestInterface $request
71     *
72     * @return ResponseInterface
73     */
74    public function handle(ServerRequestInterface $request): ResponseInterface
75    {
76        $parent_id   = (int) ($request->getQueryParams()['parent_id'] ?? 0);
77        $title       = I18N::translate('Geographic data');
78        $parent      = $this->map_data_service->findById($parent_id);
79
80        // Request for a non-existent location?
81        if ($parent_id !== $parent->id()) {
82            return redirect(route(__CLASS__));
83        }
84
85        // Automatically import any new/missing places.
86        $this->map_data_service->importMissingLocations();
87
88        $breadcrumbs = [$parent->locationName()];
89
90        $tmp = $parent->parent();
91
92        while ($tmp->id() !== 0) {
93            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
94
95            $tmp = $tmp->parent();
96        }
97
98        $breadcrumbs[route(__CLASS__)]           = $title;
99        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
100
101        $list_module = $this->module_service
102            ->findByInterface(PlaceHierarchyListModule::class)
103            ->first();
104
105        $this->layout = 'layouts/administration';
106
107        return $this->viewResponse('admin/locations', [
108            'active'       => $this->map_data_service->activePlaces($parent),
109            'all_trees'    => $this->tree_service->all(),
110            'breadcrumbs'  => array_reverse($breadcrumbs),
111            'parent_id'    => $parent_id,
112            'placelist'    => $this->map_data_service->getPlaceListLocation($parent_id),
113            'list_module'  => $list_module,
114            'title'        => $title,
115        ]);
116    }
117}
118