xref: /webtrees/app/Http/RequestHandlers/MapDataAdd.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;
2490949315SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
25c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
2690949315SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService;
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 route;
3390949315SGreg Roach
3490949315SGreg Roach/**
3590949315SGreg Roach * Edit location data.
3690949315SGreg Roach */
3790949315SGreg Roachclass MapDataAdd implements RequestHandlerInterface
3890949315SGreg Roach{
3990949315SGreg Roach    use ViewResponseTrait;
4090949315SGreg Roach
41c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
42c9c6f2ecSGreg Roach
43c9c6f2ecSGreg Roach    private MapDataService $map_data_service;
4490949315SGreg Roach
4590949315SGreg Roach    /**
4690949315SGreg Roach     * Dependency injection.
4790949315SGreg Roach     *
48c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
4990949315SGreg Roach     * @param MapDataService   $map_data_service
5090949315SGreg Roach     */
51c9c6f2ecSGreg Roach    public function __construct(LeafletJsService $leaflet_js_service, MapDataService $map_data_service)
5290949315SGreg Roach    {
53c9c6f2ecSGreg 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        $parent_id = $request->getAttribute('parent_id');
6790949315SGreg Roach
6890949315SGreg Roach        if ($parent_id === null) {
6990949315SGreg Roach            $parent = new PlaceLocation('');
7090949315SGreg Roach        } else {
7190949315SGreg Roach            $parent = $this->map_data_service->findById((int) $parent_id);
7290949315SGreg Roach        }
7390949315SGreg Roach
7490949315SGreg Roach        if ($parent->id() === null) {
7590949315SGreg Roach            $title = I18N::translate('World');
7690949315SGreg Roach        } else {
7790949315SGreg Roach            $title = e($parent->locationName());
7890949315SGreg Roach        }
7990949315SGreg Roach
8090949315SGreg Roach        $title .= ' — ' . I18N::translate('Add');
8190949315SGreg Roach
8290949315SGreg Roach        // Build the breadcrumbs in reverse order
8390949315SGreg Roach        $breadcrumbs = [I18N::translate('Add')];
8490949315SGreg Roach
8590949315SGreg Roach        $tmp = $parent;
8690949315SGreg Roach        while ($tmp->id() !== null) {
8790949315SGreg Roach            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
8890949315SGreg Roach
8990949315SGreg Roach            $tmp = $tmp->parent();
9090949315SGreg Roach        }
9190949315SGreg Roach
9290949315SGreg Roach        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
9360c799a3SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
9490949315SGreg Roach
9590949315SGreg Roach        $map_bounds = $parent->boundingRectangle();
9690949315SGreg Roach
9790949315SGreg Roach        $marker_position = [
9890949315SGreg Roach            ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
9990949315SGreg Roach            ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
10090949315SGreg Roach        ];
10190949315SGreg Roach
10290949315SGreg Roach        return $this->viewResponse('admin/location-edit', [
10360c799a3SGreg Roach            'breadcrumbs'     => array_reverse($breadcrumbs, true),
10490949315SGreg Roach            'title'           => $title,
10590949315SGreg Roach            'location'        => new PlaceLocation(''),
10690949315SGreg Roach            'latitude'        => '',
10790949315SGreg Roach            'longitude'       => '',
10890949315SGreg Roach            'map_bounds'      => $map_bounds,
10990949315SGreg Roach            'marker_position' => $marker_position,
11090949315SGreg Roach            'parent'          => $parent,
111c9c6f2ecSGreg Roach            'leaflet_config'  => $this->leaflet_js_service->config(),
112152e08bbSGreg Roach            'url'             => route(MapDataList::class, ['parent_id' => $parent->id()])
11390949315SGreg Roach        ]);
11490949315SGreg Roach    }
11590949315SGreg Roach}
116