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