xref: /webtrees/app/Http/RequestHandlers/MapDataEdit.php (revision 455a30fe529581f1d53045f9c38a0674472d498f)
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\PlaceLocation;
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    /** @var MapDataService */
42    private $map_data_service;
43
44    /**
45     * Dependency injection.
46     *
47     * @param MapDataService $map_data_service
48     */
49    public function __construct(MapDataService $map_data_service)
50    {
51        $this->map_data_service = $map_data_service;
52    }
53
54    /**
55     * @param ServerRequestInterface $request
56     *
57     * @return ResponseInterface
58     */
59    public function handle(ServerRequestInterface $request): ResponseInterface
60    {
61        $this->layout = 'layouts/administration';
62
63        $place_id = (int) $request->getAttribute('place_id');
64        $location = $this->map_data_service->findById($place_id);
65
66        if ($location->id() === null) {
67            return redirect(route(MapDataList::class));
68        }
69
70        $title = e($location->locationName()) . ' — ' . I18N::translate('Edit');
71
72        // Build the breadcrumbs in reverse order
73        $breadcrumbs = [I18N::translate('Edit')];
74
75        $tmp = $location;
76        while ($tmp->id() !== null) {
77            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
78
79            $tmp = $tmp->parent();
80        }
81
82        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
83        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
84
85        $latitude      = $location->latitude();
86        $longitude     = $location->longitude();
87        $map_bounds    = $location->boundingRectangle();
88
89        // If the current co-ordinates are unknown, leave the input fields empty,
90        // and show a marker in the middle of the map.
91        if ($latitude === null || $longitude === null) {
92            $latitude  = '';
93            $longitude = '';
94
95            $marker_position = [
96                ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
97                ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
98            ];
99        } else {
100            $marker_position = [$latitude, $longitude];
101        }
102
103        return $this->viewResponse('admin/location-edit', [
104            'breadcrumbs'     => array_reverse($breadcrumbs, true),
105            'title'           => $title,
106            'location'        => $location,
107            'latitude'        => $latitude,
108            'longitude'       => $longitude,
109            'map_bounds'      => $map_bounds,
110            'marker_position' => $marker_position,
111            'parent'          => $location->parent(),
112            'provider'        => [
113                'url'     => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
114                'options' => [
115                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
116                    'max_zoom'    => 19
117                ]
118            ],
119        ]);
120    }
121}
122