xref: /webtrees/app/Services/LeafletJsService.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6c9c6f2ecSGreg Roach * This program is free software: you can redistribute it and/or modify
7c9c6f2ecSGreg Roach * it under the terms of the GNU General Public License as published by
8c9c6f2ecSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c9c6f2ecSGreg Roach * (at your option) any later version.
10c9c6f2ecSGreg Roach * This program is distributed in the hope that it will be useful,
11c9c6f2ecSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c9c6f2ecSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c9c6f2ecSGreg Roach * GNU General Public License for more details.
14c9c6f2ecSGreg Roach * You should have received a copy of the GNU General Public License
15c9c6f2ecSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c9c6f2ecSGreg Roach */
17c9c6f2ecSGreg Roach
18c9c6f2ecSGreg Roachdeclare(strict_types=1);
19c9c6f2ecSGreg Roach
20c9c6f2ecSGreg Roachnamespace Fisharebest\Webtrees\Services;
21c9c6f2ecSGreg Roach
2261e54f5dSGreg Roachuse Fisharebest\Webtrees\Auth;
2394c90686SDavid Druryuse Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException;
2461e54f5dSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ModulesMapProvidersPage;
25c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\I18N;
26c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMapProviderInterface;
27c9c6f2ecSGreg Roach
28c9c6f2ecSGreg Roach/**
29c9c6f2ecSGreg Roach * Generate the configuration data needed to create a LeafletJs map.
30c9c6f2ecSGreg Roach */
31c9c6f2ecSGreg Roachclass LeafletJsService
32c9c6f2ecSGreg Roach{
33c9c6f2ecSGreg Roach    private ModuleService $module_service;
34c9c6f2ecSGreg Roach
35c9c6f2ecSGreg Roach    /**
36c9c6f2ecSGreg Roach     * @param ModuleService $module_service
37c9c6f2ecSGreg Roach     */
38c9c6f2ecSGreg Roach    public function __construct(ModuleService $module_service)
39c9c6f2ecSGreg Roach    {
40c9c6f2ecSGreg Roach        $this->module_service = $module_service;
41c9c6f2ecSGreg Roach    }
42c9c6f2ecSGreg Roach
43c9c6f2ecSGreg Roach    /**
44c9c6f2ecSGreg Roach     * @return object
45c9c6f2ecSGreg Roach     */
46c9c6f2ecSGreg Roach    public function config(): object
47c9c6f2ecSGreg Roach    {
48c9c6f2ecSGreg Roach        $default = 'openstreetmap';
49c9c6f2ecSGreg Roach
50c9c6f2ecSGreg Roach        $map_providers = $this->module_service
51c9c6f2ecSGreg Roach            ->findByInterface(ModuleMapProviderInterface::class)
52*f25fc0f9SGreg Roach            ->map(static fn (ModuleMapProviderInterface $map_provider): object => (object) [
53c9c6f2ecSGreg Roach                'children'  => $map_provider->leafletJsTileLayers(),
54c9c6f2ecSGreg Roach                'collapsed' => true,
55c9c6f2ecSGreg Roach                'default'   => $map_provider->name() === $default,
56c9c6f2ecSGreg Roach                'label'     => $map_provider->title(),
57*f25fc0f9SGreg Roach            ])
58c9c6f2ecSGreg Roach            ->values();
59c9c6f2ecSGreg Roach
6094c90686SDavid Drury        if ($map_providers->isEmpty()) {
6161e54f5dSGreg Roach            $message = I18N::translate('To display a map, you need to enable a map-provider in the control panel.');
6261e54f5dSGreg Roach
6361e54f5dSGreg Roach            if (Auth::isAdmin()) {
6461e54f5dSGreg Roach                $url = route(ModulesMapProvidersPage::class);
6561e54f5dSGreg Roach                $message .= ' — <a class="alert-link" href="' . e($url) . '">' . I18N::translate('Map providers') . '</a>';
6661e54f5dSGreg Roach            }
6761e54f5dSGreg Roach
6861e54f5dSGreg Roach            throw new HttpServiceUnavailableException($message);
6994c90686SDavid Drury        }
7094c90686SDavid Drury
7155ea0389SGreg Roach        $enter_fullscreen_icon = '<span title="' . I18N::translate('Enter fullscreen') . '">' . view('icons/enter-fullscreen') . '</span>';
7255ea0389SGreg Roach        $exit_fullscreen_icon  = '<span title="' . I18N::translate('Exit fullscreen') . '">' . view('icons/exit-fullscreen') . '</span>';
7355ea0389SGreg Roach
74c9c6f2ecSGreg Roach        return (object) [
75c9c6f2ecSGreg Roach            'i18n'         => [
76c9c6f2ecSGreg Roach                'reset'   => I18N::translate('Reload map'),
77c9c6f2ecSGreg Roach                'zoomIn'  => I18N::translate('Zoom in'),
78c9c6f2ecSGreg Roach                'zoomOut' => I18N::translate('Zoom out'),
79c9c6f2ecSGreg Roach            ],
80c9c6f2ecSGreg Roach            'icons'        => [
81c9c6f2ecSGreg Roach                'collapse'   => view('icons/collapse'),
82c9c6f2ecSGreg Roach                'expand'     => view('icons/expand'),
83b2e5f20eSGreg Roach                'reset'      => view('icons/undo'),
8455ea0389SGreg Roach                'fullScreen' => $enter_fullscreen_icon . $exit_fullscreen_icon,
85c9c6f2ecSGreg Roach            ],
86c9c6f2ecSGreg Roach            'mapProviders' => $map_providers,
87c9c6f2ecSGreg Roach        ];
88c9c6f2ecSGreg Roach    }
89c9c6f2ecSGreg Roach}
90