xref: /webtrees/app/Http/RequestHandlers/MapDataAdd.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
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 route;
32
33/**
34 * Edit location data.
35 */
36class MapDataAdd 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        $parent_id = $request->getAttribute('parent_id');
63
64        if ($parent_id === null) {
65            $parent = new PlaceLocation('');
66        } else {
67            $parent = $this->map_data_service->findById((int) $parent_id);
68        }
69
70        if ($parent->id() === null) {
71            $title = I18N::translate('World');
72        } else {
73            $title = e($parent->locationName());
74        }
75
76        $title .= ' — ' . I18N::translate('Add');
77
78        // Build the breadcrumbs in reverse order
79        $breadcrumbs = [I18N::translate('Add')];
80
81        $tmp = $parent;
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        $map_bounds = $parent->boundingRectangle();
92
93        $marker_position = [
94            ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
95            ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
96        ];
97
98        return $this->viewResponse('admin/location-edit', [
99            'breadcrumbs'     => array_reverse($breadcrumbs, true),
100            'title'           => $title,
101            'location'        => new PlaceLocation(''),
102            'latitude'        => '',
103            'longitude'       => '',
104            'map_bounds'      => $map_bounds,
105            'marker_position' => $marker_position,
106            'parent'          => $parent,
107            'provider'        => [
108                'url'     => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
109                'options' => [
110                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
111                    'max_zoom'    => 19
112                ]
113            ],
114        ]);
115    }
116}
117