xref: /webtrees/app/Http/RequestHandlers/MapDataEdit.php (revision c9c6f2ec6e88594e58f14a6418e0de5aaa1484bd)
190949315SGreg Roach<?php
290949315SGreg Roach
390949315SGreg Roach/**
490949315SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 webtrees development team
690949315SGreg Roach * This program is free software: you can redistribute it and/or modify
790949315SGreg Roach * it under the terms of the GNU General Public License as published by
890949315SGreg Roach * the Free Software Foundation, either version 3 of the License, or
990949315SGreg Roach * (at your option) any later version.
1090949315SGreg Roach * This program is distributed in the hope that it will be useful,
1190949315SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1290949315SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1390949315SGreg Roach * GNU General Public License for more details.
1490949315SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1690949315SGreg Roach */
1790949315SGreg Roach
1890949315SGreg Roachdeclare(strict_types=1);
1990949315SGreg Roach
2090949315SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2190949315SGreg Roach
2290949315SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2390949315SGreg Roachuse Fisharebest\Webtrees\I18N;
24*c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
2590949315SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService;
2690949315SGreg Roachuse Psr\Http\Message\ResponseInterface;
2790949315SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2890949315SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2990949315SGreg Roach
3090949315SGreg Roachuse function e;
3190949315SGreg Roachuse function redirect;
3290949315SGreg Roachuse function route;
3390949315SGreg Roach
3490949315SGreg Roach/**
3590949315SGreg Roach * Edit location data.
3690949315SGreg Roach */
3790949315SGreg Roachclass MapDataEdit implements RequestHandlerInterface
3890949315SGreg Roach{
3990949315SGreg Roach    use ViewResponseTrait;
4090949315SGreg Roach
41*c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
42*c9c6f2ecSGreg Roach
43*c9c6f2ecSGreg Roach    private MapDataService $map_data_service;
4490949315SGreg Roach
4590949315SGreg Roach    /**
4690949315SGreg Roach     * Dependency injection.
4790949315SGreg Roach     *
48*c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
4990949315SGreg Roach     * @param MapDataService   $map_data_service
5090949315SGreg Roach     */
51*c9c6f2ecSGreg Roach    public function __construct(LeafletJsService $leaflet_js_service, MapDataService $map_data_service)
5290949315SGreg Roach    {
53*c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
5490949315SGreg Roach        $this->map_data_service   = $map_data_service;
5590949315SGreg Roach    }
5690949315SGreg Roach
5790949315SGreg Roach    /**
5890949315SGreg Roach     * @param ServerRequestInterface $request
5990949315SGreg Roach     *
6090949315SGreg Roach     * @return ResponseInterface
6190949315SGreg Roach     */
6290949315SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6390949315SGreg Roach    {
6490949315SGreg Roach        $this->layout = 'layouts/administration';
6590949315SGreg Roach
6690949315SGreg Roach        $place_id = (int) $request->getAttribute('place_id');
6790949315SGreg Roach        $location = $this->map_data_service->findById($place_id);
6890949315SGreg Roach
6990949315SGreg Roach        if ($location->id() === null) {
7090949315SGreg Roach            return redirect(route(MapDataList::class));
7190949315SGreg Roach        }
7290949315SGreg Roach
7390949315SGreg Roach        $title = e($location->locationName()) . ' — ' . I18N::translate('Edit');
7490949315SGreg Roach
7590949315SGreg Roach        // Build the breadcrumbs in reverse order
7690949315SGreg Roach        $breadcrumbs = [I18N::translate('Edit')];
7790949315SGreg Roach
7890949315SGreg Roach        $tmp = $location;
7990949315SGreg Roach        while ($tmp->id() !== null) {
8090949315SGreg Roach            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
8190949315SGreg Roach
8290949315SGreg Roach            $tmp = $tmp->parent();
8390949315SGreg Roach        }
8490949315SGreg Roach
8590949315SGreg Roach        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
8660c799a3SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
8790949315SGreg Roach
8890949315SGreg Roach        $latitude   = $location->latitude();
8990949315SGreg Roach        $longitude  = $location->longitude();
9090949315SGreg Roach        $map_bounds = $location->boundingRectangle();
9190949315SGreg Roach
9290949315SGreg Roach        // If the current co-ordinates are unknown, leave the input fields empty,
9390949315SGreg Roach        // and show a marker in the middle of the map.
9490949315SGreg Roach        if ($latitude === null || $longitude === null) {
9590949315SGreg Roach            $latitude  = '';
9690949315SGreg Roach            $longitude = '';
9790949315SGreg Roach
9890949315SGreg Roach            $marker_position = [
9990949315SGreg Roach                ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
10090949315SGreg Roach                ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
10190949315SGreg Roach            ];
10290949315SGreg Roach        } else {
10390949315SGreg Roach            $marker_position = [$latitude, $longitude];
10490949315SGreg Roach        }
10590949315SGreg Roach
10690949315SGreg Roach        return $this->viewResponse('admin/location-edit', [
10760c799a3SGreg Roach            'breadcrumbs'     => array_reverse($breadcrumbs, true),
10890949315SGreg Roach            'title'           => $title,
10990949315SGreg Roach            'location'        => $location,
11090949315SGreg Roach            'latitude'        => $latitude,
11190949315SGreg Roach            'longitude'       => $longitude,
11290949315SGreg Roach            'map_bounds'      => $map_bounds,
11390949315SGreg Roach            'marker_position' => $marker_position,
11490949315SGreg Roach            'parent'          => $location->parent(),
115*c9c6f2ecSGreg Roach            'leaflet_config'  => $this->leaflet_js_service->config(),
11690949315SGreg Roach        ]);
11790949315SGreg Roach    }
11890949315SGreg Roach}
119