xref: /webtrees/app/Services/LeafletJsService.php (revision 94c90686865a25a3adc15abb52821f4e0453d633)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Http\Exceptions\HttpServiceUnavailableException;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\ModuleMapProviderInterface;
25
26/**
27 * Generate the configuration data needed to create a LeafletJs map.
28 */
29class LeafletJsService
30{
31    private ModuleService $module_service;
32
33    /**
34     * LeafletJsService constructor.
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            throw new HttpServiceUnavailableException(I18N::translate('No map providers enabled.'));
64        }
65
66        return (object) [
67            'i18n'         => [
68                'reset'   => I18N::translate('Reload map'),
69                'zoomIn'  => I18N::translate('Zoom in'),
70                'zoomOut' => I18N::translate('Zoom out'),
71            ],
72            'icons'        => [
73                'collapse' => view('icons/collapse'),
74                'expand'   => view('icons/expand'),
75            ],
76            'mapProviders' => $map_providers,
77        ];
78    }
79}
80