xref: /webtrees/app/Http/RequestHandlers/MapDataList.php (revision d11be7027e34e3121be11cc025421873364403f9)
194e35917SGreg Roach<?php
294e35917SGreg Roach
394e35917SGreg Roach/**
494e35917SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
694e35917SGreg Roach * This program is free software: you can redistribute it and/or modify
794e35917SGreg Roach * it under the terms of the GNU General Public License as published by
894e35917SGreg Roach * the Free Software Foundation, either version 3 of the License, or
994e35917SGreg Roach * (at your option) any later version.
1094e35917SGreg Roach * This program is distributed in the hope that it will be useful,
1194e35917SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1294e35917SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1394e35917SGreg Roach * GNU General Public License for more details.
1494e35917SGreg 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/>.
1694e35917SGreg Roach */
1794e35917SGreg Roach
1894e35917SGreg Roachdeclare(strict_types=1);
1994e35917SGreg Roach
2094e35917SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2194e35917SGreg Roach
2294e35917SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2394e35917SGreg Roachuse Fisharebest\Webtrees\I18N;
248504fb2dSGreg Roachuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
2590949315SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
2694e35917SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService;
278504fb2dSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
28ac34be60SRichard Cisséeuse Fisharebest\Webtrees\Services\TreeService;
2994e35917SGreg Roachuse Psr\Http\Message\ResponseInterface;
3094e35917SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3194e35917SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32ed612bd3SGreg Roach
3394e35917SGreg Roachuse function array_reverse;
3490949315SGreg Roachuse function e;
3594e35917SGreg Roachuse function redirect;
3694e35917SGreg Roachuse function route;
3794e35917SGreg Roach
3894e35917SGreg Roach/**
3994e35917SGreg Roach * Show a list of map data.
4094e35917SGreg Roach */
4194e35917SGreg Roachclass MapDataList implements RequestHandlerInterface
4294e35917SGreg Roach{
4394e35917SGreg Roach    use ViewResponseTrait;
4494e35917SGreg Roach
45c4943cffSGreg Roach    private MapDataService $map_data_service;
4694e35917SGreg Roach
47c4943cffSGreg Roach    private ModuleService $module_service;
488504fb2dSGreg Roach
49c4943cffSGreg Roach    private TreeService $tree_service;
50ac34be60SRichard Cissée
5194e35917SGreg Roach    /**
5294e35917SGreg Roach     * Dependency injection.
5394e35917SGreg Roach     *
5494e35917SGreg Roach     * @param MapDataService $map_data_service
558504fb2dSGreg Roach     * @param ModuleService  $module_service
56ed612bd3SGreg Roach     * @param TreeService    $tree_service
5794e35917SGreg Roach     */
58ed612bd3SGreg Roach    public function __construct(
59ed612bd3SGreg Roach        MapDataService $map_data_service,
60ed612bd3SGreg Roach        ModuleService $module_service,
61ed612bd3SGreg Roach        TreeService $tree_service
62ed612bd3SGreg Roach    ) {
6394e35917SGreg Roach        $this->map_data_service = $map_data_service;
648504fb2dSGreg Roach        $this->module_service   = $module_service;
65ac34be60SRichard Cissée        $this->tree_service     = $tree_service;
6694e35917SGreg Roach    }
6794e35917SGreg Roach
6894e35917SGreg Roach    /**
6994e35917SGreg Roach     * @param ServerRequestInterface $request
7094e35917SGreg Roach     *
7194e35917SGreg Roach     * @return ResponseInterface
7294e35917SGreg Roach     */
7394e35917SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
7494e35917SGreg Roach    {
7590949315SGreg Roach        $parent_id = $request->getAttribute('parent_id');
7690949315SGreg Roach
7790949315SGreg Roach        if ($parent_id === null) {
7890949315SGreg Roach            $parent = new PlaceLocation('');
7990949315SGreg Roach        } else {
8090949315SGreg Roach            $parent_id = (int) $parent_id;
8194e35917SGreg Roach            $parent = $this->map_data_service->findById($parent_id);
8290949315SGreg Roach        }
8394e35917SGreg Roach
8494e35917SGreg Roach        // Request for a non-existent location?
8590949315SGreg Roach        if ($parent_id !== null && $parent->id() === null) {
869991924fSGreg Roach            return redirect(route(self::class));
8794e35917SGreg Roach        }
8894e35917SGreg Roach
8994e35917SGreg Roach        // Automatically import any new/missing places.
900e50635cSGreg Roach        $this->map_data_service->importMissingLocations();
9194e35917SGreg Roach
9290949315SGreg Roach        $breadcrumbs = [];
9390949315SGreg Roach
9490949315SGreg Roach        if ($parent->id() !== null) {
9590949315SGreg Roach            $breadcrumbs[] = e($parent->locationName());
9690949315SGreg Roach        }
979ee82875SGreg Roach
989ee82875SGreg Roach        $tmp = $parent->parent();
9994e35917SGreg Roach
10090949315SGreg Roach        while ($tmp->id() !== null) {
1019991924fSGreg Roach            $breadcrumbs[route(self::class, ['parent_id' => $tmp->id()])] = $tmp->locationName();
10294e35917SGreg Roach
10394e35917SGreg Roach            $tmp = $tmp->parent();
10494e35917SGreg Roach        }
10594e35917SGreg Roach
10690949315SGreg Roach        $title = I18N::translate('Geographic data');
10790949315SGreg Roach
1089991924fSGreg Roach        $breadcrumbs[route(self::class)] = $title;
10990949315SGreg Roach
11094e35917SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
11194e35917SGreg Roach
112ed612bd3SGreg Roach        $list_module = $this->module_service
113ed612bd3SGreg Roach            ->findByInterface(PlaceHierarchyListModule::class)
114ed612bd3SGreg Roach            ->first();
1158504fb2dSGreg Roach
11694e35917SGreg Roach        $this->layout = 'layouts/administration';
11794e35917SGreg Roach
11894e35917SGreg Roach        return $this->viewResponse('admin/locations', [
11994e35917SGreg Roach            'active'       => $this->map_data_service->activePlaces($parent),
120ed612bd3SGreg Roach            'all_trees'    => $this->tree_service->all(),
12194e35917SGreg Roach            'breadcrumbs'  => array_reverse($breadcrumbs),
12294e35917SGreg Roach            'parent_id'    => $parent_id,
1230e50635cSGreg Roach            'placelist'    => $this->map_data_service->getPlaceListLocation($parent_id),
124ed612bd3SGreg Roach            'list_module'  => $list_module,
1258504fb2dSGreg Roach            'title'        => $title,
12694e35917SGreg Roach        ]);
12711464183SGreg Roach    }
12894e35917SGreg Roach}
129