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