xref: /webtrees/app/Services/LeafletJsService.php (revision b2e5f20ea75b7378d8949eac2d8ec43fb9552b6f)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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     * LeafletJsService constructor.
37c9c6f2ecSGreg Roach     *
38c9c6f2ecSGreg Roach     * @param ModuleService $module_service
39c9c6f2ecSGreg Roach     */
40c9c6f2ecSGreg Roach    public function __construct(ModuleService $module_service)
41c9c6f2ecSGreg Roach    {
42c9c6f2ecSGreg Roach        $this->module_service = $module_service;
43c9c6f2ecSGreg Roach    }
44c9c6f2ecSGreg Roach
45c9c6f2ecSGreg Roach    /**
46c9c6f2ecSGreg Roach     * @return object
47c9c6f2ecSGreg Roach     */
48c9c6f2ecSGreg Roach    public function config(): object
49c9c6f2ecSGreg Roach    {
50c9c6f2ecSGreg Roach        $default = 'openstreetmap';
51c9c6f2ecSGreg Roach
52c9c6f2ecSGreg Roach        $map_providers = $this->module_service
53c9c6f2ecSGreg Roach            ->findByInterface(ModuleMapProviderInterface::class)
54c9c6f2ecSGreg Roach            ->map(static function (ModuleMapProviderInterface $map_provider) use ($default): object {
55c9c6f2ecSGreg Roach                return (object) [
56c9c6f2ecSGreg Roach                    'children'  => $map_provider->leafletJsTileLayers(),
57c9c6f2ecSGreg Roach                    'collapsed' => true,
58c9c6f2ecSGreg Roach                    'default'   => $map_provider->name() === $default,
59c9c6f2ecSGreg Roach                    'label'     => $map_provider->title(),
60c9c6f2ecSGreg Roach                ];
61c9c6f2ecSGreg Roach            })
62c9c6f2ecSGreg Roach            ->values();
63c9c6f2ecSGreg Roach
6494c90686SDavid Drury        if ($map_providers->isEmpty()) {
6561e54f5dSGreg Roach            $message = I18N::translate('To display a map, you need to enable a map-provider in the control panel.');
6661e54f5dSGreg Roach
6761e54f5dSGreg Roach            if (Auth::isAdmin()) {
6861e54f5dSGreg Roach                $url = route(ModulesMapProvidersPage::class);
6961e54f5dSGreg Roach                $message .= ' — <a class="alert-link" href="' . e($url) . '">' . I18N::translate('Map providers') . '</a>';
7061e54f5dSGreg Roach            }
7161e54f5dSGreg Roach
7261e54f5dSGreg Roach            throw new HttpServiceUnavailableException($message);
7394c90686SDavid Drury        }
7494c90686SDavid Drury
75c9c6f2ecSGreg Roach        return (object) [
76c9c6f2ecSGreg Roach            'i18n'         => [
77c9c6f2ecSGreg Roach                'reset'   => I18N::translate('Reload map'),
78c9c6f2ecSGreg Roach                'zoomIn'  => I18N::translate('Zoom in'),
79c9c6f2ecSGreg Roach                'zoomOut' => I18N::translate('Zoom out'),
80c9c6f2ecSGreg Roach            ],
81c9c6f2ecSGreg Roach            'icons'        => [
82c9c6f2ecSGreg Roach                'collapse' => view('icons/collapse'),
83c9c6f2ecSGreg Roach                'expand'   => view('icons/expand'),
84*b2e5f20eSGreg Roach                'reset'    => view('icons/undo'),
85c9c6f2ecSGreg Roach            ],
86c9c6f2ecSGreg Roach            'mapProviders' => $map_providers,
87c9c6f2ecSGreg Roach        ];
88c9c6f2ecSGreg Roach    }
89c9c6f2ecSGreg Roach}
90