xref: /webtrees/app/Http/RequestHandlers/MapDataAdd.php (revision 60c799a372155b7a66ff97ccd689adde28912740)
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
1590949315SGreg Roach * along with this program. If not, see <http://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;
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 route;
3290949315SGreg Roach
3390949315SGreg Roach/**
3490949315SGreg Roach * Edit location data.
3590949315SGreg Roach */
3690949315SGreg Roachclass MapDataAdd implements RequestHandlerInterface
3790949315SGreg Roach{
3890949315SGreg Roach    use ViewResponseTrait;
3990949315SGreg Roach
4090949315SGreg Roach    /** @var MapDataService */
4190949315SGreg Roach    private $map_data_service;
4290949315SGreg Roach
4390949315SGreg Roach    /**
4490949315SGreg Roach     * Dependency injection.
4590949315SGreg Roach     *
4690949315SGreg Roach     * @param MapDataService $map_data_service
4790949315SGreg Roach     */
4890949315SGreg Roach    public function __construct(MapDataService $map_data_service)
4990949315SGreg Roach    {
5090949315SGreg Roach        $this->map_data_service = $map_data_service;
5190949315SGreg Roach    }
5290949315SGreg Roach
5390949315SGreg Roach    /**
5490949315SGreg Roach     * @param ServerRequestInterface $request
5590949315SGreg Roach     *
5690949315SGreg Roach     * @return ResponseInterface
5790949315SGreg Roach     */
5890949315SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5990949315SGreg Roach    {
6090949315SGreg Roach        $this->layout = 'layouts/administration';
6190949315SGreg Roach
6290949315SGreg Roach        $parent_id = $request->getAttribute('parent_id');
6390949315SGreg Roach
6490949315SGreg Roach        if ($parent_id === null) {
6590949315SGreg Roach            $parent = new PlaceLocation('');
6690949315SGreg Roach        } else {
6790949315SGreg Roach            $parent = $this->map_data_service->findById((int) $parent_id);
6890949315SGreg Roach        }
6990949315SGreg Roach
7090949315SGreg Roach        if ($parent->id() === null) {
7190949315SGreg Roach            $title = I18N::translate('World');
7290949315SGreg Roach        } else {
7390949315SGreg Roach            $title = e($parent->locationName());
7490949315SGreg Roach        }
7590949315SGreg Roach
7690949315SGreg Roach        $title .= ' — ' . I18N::translate('Add');
7790949315SGreg Roach
7890949315SGreg Roach        // Build the breadcrumbs in reverse order
7990949315SGreg Roach        $breadcrumbs = [I18N::translate('Add')];
8090949315SGreg Roach
8190949315SGreg Roach        $tmp = $parent;
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');
89*60c799a3SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
9090949315SGreg Roach
9190949315SGreg Roach        $map_bounds = $parent->boundingRectangle();
9290949315SGreg Roach
9390949315SGreg Roach        $marker_position = [
9490949315SGreg Roach            ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
9590949315SGreg Roach            ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
9690949315SGreg Roach        ];
9790949315SGreg Roach
9890949315SGreg Roach        return $this->viewResponse('admin/location-edit', [
99*60c799a3SGreg Roach            'breadcrumbs'     => array_reverse($breadcrumbs, true),
10090949315SGreg Roach            'title'           => $title,
10190949315SGreg Roach            'location'        => new PlaceLocation(''),
10290949315SGreg Roach            'latitude'        => '',
10390949315SGreg Roach            'longitude'       => '',
10490949315SGreg Roach            'map_bounds'      => $map_bounds,
10590949315SGreg Roach            'marker_position' => $marker_position,
10690949315SGreg Roach            'parent'          => $parent,
10790949315SGreg Roach            'provider'        => [
10890949315SGreg Roach                'url'     => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
10990949315SGreg Roach                'options' => [
11090949315SGreg Roach                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
11190949315SGreg Roach                    'max_zoom'    => 19
11290949315SGreg Roach                ]
11390949315SGreg Roach            ],
11490949315SGreg Roach        ]);
11590949315SGreg Roach    }
11690949315SGreg Roach}
117