Passed
Pull Request — master (#3706)
by
unknown
06:53
created

MapDataEdit   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 81
rs 10
wmc 6

2 Methods

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