. */ 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\Services\MapDataService; use Fisharebest\Webtrees\Services\ModuleService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function array_reverse; use function redirect; use function route; /** * Show a list of map data. */ class MapDataList implements RequestHandlerInterface { use ViewResponseTrait; /** @var MapDataService */ private $map_data_service; /** @var ModuleService */ private $module_service; /** * Dependency injection. * * @param MapDataService $map_data_service * @param ModuleService $module_service */ public function __construct(MapDataService $map_data_service, ModuleService $module_service) { $this->map_data_service = $map_data_service; $this->module_service = $module_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $parent_id = (int) ($request->getQueryParams()['parent_id'] ?? 0); $title = I18N::translate('Geographic data'); $parent = $this->map_data_service->findById($parent_id); // Request for a non-existent location? if ($parent_id !== $parent->id()) { return redirect(route(__CLASS__)); } // Automatically import any new/missing places. $this->map_data_service->importMissingLocations(); $breadcrumbs = [$parent->locationName()]; $tmp = $parent->parent(); while ($tmp->id() !== 0) { $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName(); $tmp = $tmp->parent(); } $breadcrumbs[route(__CLASS__)] = $title; $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel'); $show_links = $this->module_service->findByInterface(PlaceHierarchyListModule::class)->isNotEmpty(); $this->layout = 'layouts/administration'; return $this->viewResponse('admin/locations', [ 'active' => $this->map_data_service->activePlaces($parent), 'breadcrumbs' => array_reverse($breadcrumbs), 'parent_id' => $parent_id, 'placelist' => $this->map_data_service->getPlaceListLocation($parent_id), 'show_links' => $show_links, 'title' => $title, ]); } }