xref: /webtrees/app/Services/LeafletJsService.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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 fn(ModuleMapProviderInterface $map_provider): object => (object) [
53                'children'  => $map_provider->leafletJsTileLayers(),
54                'collapsed' => true,
55                'default'   => $map_provider->name() === $default,
56                'label'     => $map_provider->title(),
57            ])
58            ->values();
59
60        if ($map_providers->isEmpty()) {
61            $message = I18N::translate('To display a map, you need to enable a map-provider in the control panel.');
62
63            if (Auth::isAdmin()) {
64                $url = route(ModulesMapProvidersPage::class);
65                $message .= ' — <a class="alert-link" href="' . e($url) . '">' . I18N::translate('Map providers') . '</a>';
66            }
67
68            throw new HttpServiceUnavailableException($message);
69        }
70
71        $enter_fullscreen_icon = '<span title="' . I18N::translate('Enter fullscreen') . '">' . view('icons/enter-fullscreen') . '</span>';
72        $exit_fullscreen_icon  = '<span title="' . I18N::translate('Exit fullscreen') . '">' . view('icons/exit-fullscreen') . '</span>';
73
74        return (object) [
75            'i18n'         => [
76                'reset'   => I18N::translate('Reload map'),
77                'zoomIn'  => I18N::translate('Zoom in'),
78                'zoomOut' => I18N::translate('Zoom out'),
79            ],
80            'icons'        => [
81                'collapse'   => view('icons/collapse'),
82                'expand'     => view('icons/expand'),
83                'reset'      => view('icons/undo'),
84                'fullScreen' => $enter_fullscreen_icon . $exit_fullscreen_icon,
85            ],
86            'mapProviders' => $map_providers,
87        ];
88    }
89}
90