Passed
Push — develop ( 5f211d...930448 )
by Greg
13:36 queued 06:57
created

MapDataEdit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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