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