xref: /webtrees/app/Http/RequestHandlers/MapDataEdit.php (revision acb90a3897a0c2932393544961e7311805adb49a)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Services\LeafletJsService;
25use Fisharebest\Webtrees\Services\MapDataService;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function e;
31use function redirect;
32use function route;
33
34/**
35 * Edit location data.
36 */
37class MapDataEdit implements RequestHandlerInterface
38{
39    use ViewResponseTrait;
40
41    private LeafletJsService $leaflet_js_service;
42
43    private MapDataService $map_data_service;
44
45    /**
46     * Dependency injection.
47     *
48     * @param LeafletJsService $leaflet_js_service
49     * @param MapDataService   $map_data_service
50     */
51    public function __construct(LeafletJsService $leaflet_js_service, MapDataService $map_data_service)
52    {
53        $this->leaflet_js_service = $leaflet_js_service;
54        $this->map_data_service   = $map_data_service;
55    }
56
57    /**
58     * @param ServerRequestInterface $request
59     *
60     * @return ResponseInterface
61     */
62    public function handle(ServerRequestInterface $request): ResponseInterface
63    {
64        $this->layout = 'layouts/administration';
65
66        $place_id = (int) $request->getAttribute('place_id');
67        $location = $this->map_data_service->findById($place_id);
68
69        if ($location->id() === null) {
70            return redirect(route(MapDataList::class));
71        }
72
73        $title = e($location->locationName()) . ' — ' . I18N::translate('Edit');
74
75        // Build the breadcrumbs in reverse order
76        $breadcrumbs = [I18N::translate('Edit')];
77
78        $tmp = $location;
79        while ($tmp->id() !== null) {
80            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
81
82            $tmp = $tmp->parent();
83        }
84
85        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
86        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
87
88        $latitude   = $location->latitude();
89        $longitude  = $location->longitude();
90        $map_bounds = $location->boundingRectangle();
91
92        // If the current co-ordinates are unknown, leave the input fields empty,
93        // and show a marker in the middle of the map.
94        if ($latitude === null || $longitude === null) {
95            $latitude  = '';
96            $longitude = '';
97
98            $marker_position = [
99                ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
100                ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
101            ];
102        } else {
103            $marker_position = [$latitude, $longitude];
104        }
105
106        return $this->viewResponse('admin/location-edit', [
107            'breadcrumbs'     => array_reverse($breadcrumbs, true),
108            'title'           => $title,
109            'location'        => $location,
110            'latitude'        => $latitude,
111            'longitude'       => $longitude,
112            'map_bounds'      => $map_bounds,
113            'marker_position' => $marker_position,
114            'parent'          => $location->parent(),
115            'leaflet_config'  => $this->leaflet_js_service->config(),
116        ]);
117    }
118}
119