. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Module\PlaceHierarchyListModule; use Fisharebest\Webtrees\PlaceLocation; use Fisharebest\Webtrees\Services\MapDataService; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\TreeService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function array_reverse; use function e; use function redirect; use function route; /** * Show a list of map data. */ class MapDataList implements RequestHandlerInterface { use ViewResponseTrait; private MapDataService $map_data_service; private ModuleService $module_service; private TreeService $tree_service; /** * Dependency injection. * * @param MapDataService $map_data_service * @param ModuleService $module_service * @param TreeService $tree_service */ public function __construct( MapDataService $map_data_service, ModuleService $module_service, TreeService $tree_service ) { $this->map_data_service = $map_data_service; $this->module_service = $module_service; $this->tree_service = $tree_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $parent_id = $request->getAttribute('parent_id'); if ($parent_id === null) { $parent = new PlaceLocation(''); } else { $parent_id = (int) $parent_id; $parent = $this->map_data_service->findById($parent_id); } // Request for a non-existent location? if ($parent_id !== null && $parent->id() === null) { return redirect(route(__CLASS__)); } // Automatically import any new/missing places. $this->map_data_service->importMissingLocations(); $breadcrumbs = []; if ($parent->id() !== null) { $breadcrumbs[] = e($parent->locationName()); } $tmp = $parent->parent(); while ($tmp->id() !== null) { $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName(); $tmp = $tmp->parent(); } $title = I18N::translate('Geographic data'); $breadcrumbs[route(__CLASS__)] = $title; $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel'); $list_module = $this->module_service ->findByInterface(PlaceHierarchyListModule::class) ->first(); $this->layout = 'layouts/administration'; return $this->viewResponse('admin/locations', [ 'active' => $this->map_data_service->activePlaces($parent), 'all_trees' => $this->tree_service->all(), 'breadcrumbs' => array_reverse($breadcrumbs), 'parent_id' => $parent_id, 'placelist' => $this->map_data_service->getPlaceListLocation($parent_id), 'list_module' => $list_module, 'title' => $title, ]); } }