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