1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Http\ViewResponseTrait; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Module\PlaceHierarchyListModule; 25use Fisharebest\Webtrees\PlaceLocation; 26use Fisharebest\Webtrees\Services\MapDataService; 27use Fisharebest\Webtrees\Services\ModuleService; 28use Fisharebest\Webtrees\Services\TreeService; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31use Psr\Http\Server\RequestHandlerInterface; 32 33use function array_reverse; 34use function e; 35use function redirect; 36use function route; 37 38/** 39 * Show a list of map data. 40 */ 41class MapDataList implements RequestHandlerInterface 42{ 43 use ViewResponseTrait; 44 45 private MapDataService $map_data_service; 46 47 private ModuleService $module_service; 48 49 private TreeService $tree_service; 50 51 /** 52 * Dependency injection. 53 * 54 * @param MapDataService $map_data_service 55 * @param ModuleService $module_service 56 * @param TreeService $tree_service 57 */ 58 public function __construct( 59 MapDataService $map_data_service, 60 ModuleService $module_service, 61 TreeService $tree_service 62 ) { 63 $this->map_data_service = $map_data_service; 64 $this->module_service = $module_service; 65 $this->tree_service = $tree_service; 66 } 67 68 /** 69 * @param ServerRequestInterface $request 70 * 71 * @return ResponseInterface 72 */ 73 public function handle(ServerRequestInterface $request): ResponseInterface 74 { 75 $parent_id = $request->getAttribute('parent_id'); 76 77 if ($parent_id === null) { 78 $parent = new PlaceLocation(''); 79 } else { 80 $parent_id = (int) $parent_id; 81 $parent = $this->map_data_service->findById($parent_id); 82 } 83 84 // Request for a non-existent location? 85 if ($parent_id !== null && $parent->id() === null) { 86 return redirect(route(__CLASS__)); 87 } 88 89 // Automatically import any new/missing places. 90 $this->map_data_service->importMissingLocations(); 91 92 $breadcrumbs = []; 93 94 if ($parent->id() !== null) { 95 $breadcrumbs[] = e($parent->locationName()); 96 } 97 98 $tmp = $parent->parent(); 99 100 while ($tmp->id() !== null) { 101 $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName(); 102 103 $tmp = $tmp->parent(); 104 } 105 106 $title = I18N::translate('Geographic data'); 107 108 $breadcrumbs[route(__CLASS__)] = $title; 109 110 $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel'); 111 112 $list_module = $this->module_service 113 ->findByInterface(PlaceHierarchyListModule::class) 114 ->first(); 115 116 $this->layout = 'layouts/administration'; 117 118 return $this->viewResponse('admin/locations', [ 119 'active' => $this->map_data_service->activePlaces($parent), 120 'all_trees' => $this->tree_service->all(), 121 'breadcrumbs' => array_reverse($breadcrumbs), 122 'parent_id' => $parent_id, 123 'placelist' => $this->map_data_service->getPlaceListLocation($parent_id), 124 'list_module' => $list_module, 125 'title' => $title, 126 ]); 127 } 128} 129