1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Services; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException; 24use Fisharebest\Webtrees\Http\RequestHandlers\ModulesMapProvidersPage; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Module\ModuleMapProviderInterface; 27 28/** 29 * Generate the configuration data needed to create a LeafletJs map. 30 */ 31class LeafletJsService 32{ 33 private ModuleService $module_service; 34 35 /** 36 * @param ModuleService $module_service 37 */ 38 public function __construct(ModuleService $module_service) 39 { 40 $this->module_service = $module_service; 41 } 42 43 /** 44 * @return object 45 */ 46 public function config(): object 47 { 48 $default = 'openstreetmap'; 49 50 $map_providers = $this->module_service 51 ->findByInterface(ModuleMapProviderInterface::class) 52 ->map(static function (ModuleMapProviderInterface $map_provider) use ($default): object { 53 return (object) [ 54 'children' => $map_provider->leafletJsTileLayers(), 55 'collapsed' => true, 56 'default' => $map_provider->name() === $default, 57 'label' => $map_provider->title(), 58 ]; 59 }) 60 ->values(); 61 62 if ($map_providers->isEmpty()) { 63 $message = I18N::translate('To display a map, you need to enable a map-provider in the control panel.'); 64 65 if (Auth::isAdmin()) { 66 $url = route(ModulesMapProvidersPage::class); 67 $message .= ' — <a class="alert-link" href="' . e($url) . '">' . I18N::translate('Map providers') . '</a>'; 68 } 69 70 throw new HttpServiceUnavailableException($message); 71 } 72 73 $enter_fullscreen_icon = '<span title="' . I18N::translate('Enter fullscreen') . '">' . view('icons/enter-fullscreen') . '</span>'; 74 $exit_fullscreen_icon = '<span title="' . I18N::translate('Exit fullscreen') . '">' . view('icons/exit-fullscreen') . '</span>'; 75 76 return (object) [ 77 'i18n' => [ 78 'reset' => I18N::translate('Reload map'), 79 'zoomIn' => I18N::translate('Zoom in'), 80 'zoomOut' => I18N::translate('Zoom out'), 81 ], 82 'icons' => [ 83 'collapse' => view('icons/collapse'), 84 'expand' => view('icons/expand'), 85 'reset' => view('icons/undo'), 86 'fullScreen' => $enter_fullscreen_icon . $exit_fullscreen_icon, 87 ], 88 'mapProviders' => $map_providers, 89 ]; 90 } 91} 92