xref: /webtrees/app/Http/RequestHandlers/MapDataEdit.php (revision d11be7027e34e3121be11cc025421873364403f9)
190949315SGreg Roach<?php
290949315SGreg Roach
390949315SGreg Roach/**
490949315SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
24c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
2590949315SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService;
260e54db38SGreg Roachuse Fisharebest\Webtrees\Validator;
2790949315SGreg Roachuse Psr\Http\Message\ResponseInterface;
2890949315SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2990949315SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3090949315SGreg Roach
3190949315SGreg Roachuse function e;
3290949315SGreg Roachuse function redirect;
3390949315SGreg Roachuse function route;
3490949315SGreg Roach
3590949315SGreg Roach/**
3690949315SGreg Roach * Edit location data.
3790949315SGreg Roach */
3890949315SGreg Roachclass MapDataEdit implements RequestHandlerInterface
3990949315SGreg Roach{
4090949315SGreg Roach    use ViewResponseTrait;
4190949315SGreg Roach
42c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
43c9c6f2ecSGreg Roach
44c9c6f2ecSGreg Roach    private MapDataService $map_data_service;
4590949315SGreg Roach
4690949315SGreg Roach    /**
4790949315SGreg Roach     * Dependency injection.
4890949315SGreg Roach     *
49c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
5090949315SGreg Roach     * @param MapDataService   $map_data_service
5190949315SGreg Roach     */
52c9c6f2ecSGreg Roach    public function __construct(LeafletJsService $leaflet_js_service, MapDataService $map_data_service)
5390949315SGreg Roach    {
54c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
5590949315SGreg Roach        $this->map_data_service   = $map_data_service;
5690949315SGreg Roach    }
5790949315SGreg Roach
5890949315SGreg Roach    /**
5990949315SGreg Roach     * @param ServerRequestInterface $request
6090949315SGreg Roach     *
6190949315SGreg Roach     * @return ResponseInterface
6290949315SGreg Roach     */
6390949315SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6490949315SGreg Roach    {
6590949315SGreg Roach        $this->layout = 'layouts/administration';
6690949315SGreg Roach
670e54db38SGreg Roach        $location_id = Validator::attributes($request)->integer('location_id');
680e54db38SGreg Roach        $location    = $this->map_data_service->findById($location_id);
690e54db38SGreg Roach        $default_url = route(MapDataList::class, ['parent_id' => $location->parent()->id()]);
700e54db38SGreg Roach        $url         = Validator::queryParams($request)->isLocalUrl()->string('url', $default_url);
7190949315SGreg Roach
7290949315SGreg Roach        if ($location->id() === null) {
7390949315SGreg Roach            return redirect(route(MapDataList::class));
7490949315SGreg Roach        }
7590949315SGreg Roach
7690949315SGreg Roach        $title = e($location->locationName()) . ' — ' . I18N::translate('Edit');
7790949315SGreg Roach
7890949315SGreg Roach        // Build the breadcrumbs in reverse order
7990949315SGreg Roach        $breadcrumbs = [I18N::translate('Edit')];
8090949315SGreg Roach
8190949315SGreg Roach        $tmp = $location;
8290949315SGreg Roach        while ($tmp->id() !== null) {
8390949315SGreg Roach            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
8490949315SGreg Roach
8590949315SGreg Roach            $tmp = $tmp->parent();
8690949315SGreg Roach        }
8790949315SGreg Roach
8890949315SGreg Roach        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
8960c799a3SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
9090949315SGreg Roach
9190949315SGreg Roach        $latitude   = $location->latitude();
9290949315SGreg Roach        $longitude  = $location->longitude();
9390949315SGreg Roach        $map_bounds = $location->boundingRectangle();
9490949315SGreg Roach
9590949315SGreg Roach        // If the current co-ordinates are unknown, leave the input fields empty,
9690949315SGreg Roach        // and show a marker in the middle of the map.
9790949315SGreg Roach        if ($latitude === null || $longitude === null) {
9890949315SGreg Roach            $latitude  = '';
9990949315SGreg Roach            $longitude = '';
10090949315SGreg Roach
10190949315SGreg Roach            $marker_position = [
10290949315SGreg Roach                ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
10390949315SGreg Roach                ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
10490949315SGreg Roach            ];
10590949315SGreg Roach        } else {
10690949315SGreg Roach            $marker_position = [$latitude, $longitude];
10790949315SGreg Roach        }
10890949315SGreg Roach
10990949315SGreg Roach        return $this->viewResponse('admin/location-edit', [
11060c799a3SGreg Roach            'breadcrumbs'     => array_reverse($breadcrumbs, true),
11190949315SGreg Roach            'title'           => $title,
11290949315SGreg Roach            'location'        => $location,
11390949315SGreg Roach            'latitude'        => $latitude,
11490949315SGreg Roach            'longitude'       => $longitude,
11590949315SGreg Roach            'map_bounds'      => $map_bounds,
11690949315SGreg Roach            'marker_position' => $marker_position,
11790949315SGreg Roach            'parent'          => $location->parent(),
118c9c6f2ecSGreg Roach            'leaflet_config'  => $this->leaflet_js_service->config(),
1190e54db38SGreg Roach            'url'             => $url,
12090949315SGreg Roach        ]);
12190949315SGreg Roach    }
12290949315SGreg Roach}
123