xref: /webtrees/app/Http/RequestHandlers/MapDataAdd.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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\PlaceLocation;
25use Fisharebest\Webtrees\Services\LeafletJsService;
26use Fisharebest\Webtrees\Services\MapDataService;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31use function e;
32use function route;
33
34/**
35 * Edit location data.
36 */
37class MapDataAdd 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        $parent_id = $request->getAttribute('parent_id');
67
68        if ($parent_id === null) {
69            $parent = new PlaceLocation('');
70        } else {
71            $parent = $this->map_data_service->findById((int) $parent_id);
72        }
73
74        if ($parent->id() === null) {
75            $title = I18N::translate('World');
76        } else {
77            $title = e($parent->locationName());
78        }
79
80        $title .= ' — ' . I18N::translate('Add');
81
82        // Build the breadcrumbs in reverse order
83        $breadcrumbs = [I18N::translate('Add')];
84
85        $tmp = $parent;
86        while ($tmp->id() !== null) {
87            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
88
89            $tmp = $tmp->parent();
90        }
91
92        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
93        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
94
95        $map_bounds = $parent->boundingRectangle();
96
97        $marker_position = [
98            ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
99            ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
100        ];
101
102        return $this->viewResponse('admin/location-edit', [
103            'breadcrumbs'     => array_reverse($breadcrumbs, true),
104            'title'           => $title,
105            'location'        => new PlaceLocation(''),
106            'latitude'        => '',
107            'longitude'       => '',
108            'map_bounds'      => $map_bounds,
109            'marker_position' => $marker_position,
110            'parent'          => $parent,
111            'leaflet_config'  => $this->leaflet_js_service->config(),
112            'url'             => route(MapDataList::class, ['parent_id' => $parent->id()])
113        ]);
114    }
115}
116