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