xref: /webtrees/app/Http/RequestHandlers/MapDataList.php (revision 90949315619051b772509f417394730bd8b1bda0)
194e35917SGreg Roach<?php
294e35917SGreg Roach
394e35917SGreg Roach/**
494e35917SGreg Roach * webtrees: online genealogy
5*90949315SGreg Roach * Copyright (C) 2021 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
1594e35917SGreg Roach * along with this program. If not, see <http://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;
25*90949315SGreg 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;
34*90949315SGreg 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
4594e35917SGreg Roach    /** @var MapDataService */
4694e35917SGreg Roach    private $map_data_service;
4794e35917SGreg Roach
488504fb2dSGreg Roach    /** @var ModuleService */
498504fb2dSGreg Roach    private $module_service;
508504fb2dSGreg Roach
51ac34be60SRichard Cissée    /** @var TreeService */
52ac34be60SRichard Cissée    private $tree_service;
53ac34be60SRichard Cissée
5494e35917SGreg Roach    /**
5594e35917SGreg Roach     * Dependency injection.
5694e35917SGreg Roach     *
5794e35917SGreg Roach     * @param MapDataService $map_data_service
588504fb2dSGreg Roach     * @param ModuleService  $module_service
59ed612bd3SGreg Roach     * @param TreeService    $tree_service
6094e35917SGreg Roach     */
61ed612bd3SGreg Roach    public function __construct(
62ed612bd3SGreg Roach        MapDataService $map_data_service,
63ed612bd3SGreg Roach        ModuleService $module_service,
64ed612bd3SGreg Roach        TreeService $tree_service
65ed612bd3SGreg Roach    ) {
6694e35917SGreg Roach        $this->map_data_service = $map_data_service;
678504fb2dSGreg Roach        $this->module_service   = $module_service;
68ac34be60SRichard Cissée        $this->tree_service     = $tree_service;
6994e35917SGreg Roach    }
7094e35917SGreg Roach
7194e35917SGreg Roach    /**
7294e35917SGreg Roach     * @param ServerRequestInterface $request
7394e35917SGreg Roach     *
7494e35917SGreg Roach     * @return ResponseInterface
7594e35917SGreg Roach     */
7694e35917SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
7794e35917SGreg Roach    {
78*90949315SGreg Roach        $parent_id = $request->getAttribute('parent_id');
79*90949315SGreg Roach
80*90949315SGreg Roach        if ($parent_id === null) {
81*90949315SGreg Roach            $parent = new PlaceLocation('');
82*90949315SGreg Roach        } else {
83*90949315SGreg Roach            $parent_id = (int) $parent_id;
8494e35917SGreg Roach            $parent = $this->map_data_service->findById($parent_id);
85*90949315SGreg Roach        }
8694e35917SGreg Roach
8794e35917SGreg Roach        // Request for a non-existent location?
88*90949315SGreg Roach        if ($parent_id !== null &&  $parent->id() === null) {
8994e35917SGreg Roach            return redirect(route(__CLASS__));
9094e35917SGreg Roach        }
9194e35917SGreg Roach
9294e35917SGreg Roach        // Automatically import any new/missing places.
930e50635cSGreg Roach        $this->map_data_service->importMissingLocations();
9494e35917SGreg Roach
95*90949315SGreg Roach        $breadcrumbs = [];
96*90949315SGreg Roach
97*90949315SGreg Roach        if ($parent->id() !== null) {
98*90949315SGreg Roach            $breadcrumbs[] = e($parent->locationName());
99*90949315SGreg Roach        }
1009ee82875SGreg Roach
1019ee82875SGreg Roach        $tmp = $parent->parent();
10294e35917SGreg Roach
103*90949315SGreg Roach        while ($tmp->id() !== null) {
10494e35917SGreg Roach            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
10594e35917SGreg Roach
10694e35917SGreg Roach            $tmp = $tmp->parent();
10794e35917SGreg Roach        }
10894e35917SGreg Roach
109*90949315SGreg Roach        $title = I18N::translate('Geographic data');
110*90949315SGreg Roach
11194e35917SGreg Roach        $breadcrumbs[route(__CLASS__)] = $title;
112*90949315SGreg Roach
11394e35917SGreg Roach        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
11494e35917SGreg Roach
115ed612bd3SGreg Roach        $list_module = $this->module_service
116ed612bd3SGreg Roach            ->findByInterface(PlaceHierarchyListModule::class)
117ed612bd3SGreg Roach            ->first();
1188504fb2dSGreg Roach
11994e35917SGreg Roach        $this->layout = 'layouts/administration';
12094e35917SGreg Roach
12194e35917SGreg Roach        return $this->viewResponse('admin/locations', [
12294e35917SGreg Roach            'active'       => $this->map_data_service->activePlaces($parent),
123ed612bd3SGreg Roach            'all_trees'    => $this->tree_service->all(),
12494e35917SGreg Roach            'breadcrumbs'  => array_reverse($breadcrumbs),
12594e35917SGreg Roach            'parent_id'    => $parent_id,
1260e50635cSGreg Roach            'placelist'    => $this->map_data_service->getPlaceListLocation($parent_id),
127ed612bd3SGreg Roach            'list_module'  => $list_module,
1288504fb2dSGreg Roach            'title'        => $title,
12994e35917SGreg Roach        ]);
13011464183SGreg Roach    }
13194e35917SGreg Roach}
132