xref: /webtrees/app/Module/HereMaps.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Validator;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function e;
31use function redirect;
32
33/**
34 * Class HereMaps - use maps within webtrees
35 */
36class HereMaps extends AbstractModule implements ModuleConfigInterface, ModuleMapProviderInterface
37{
38    use ModuleConfigTrait;
39    use ModuleMapProviderTrait;
40
41    /**
42     * Name of the map provider.
43     *
44     * @return string
45     */
46    public function description(): string
47    {
48        $link = '<a href="https://www.here.com" dir="ltr">www.here.com</a>';
49
50        // I18N: %s is a link/URL
51        return I18N::translate('Create maps using %s.', $link);
52    }
53
54    /**
55     * Should this module be enabled when it is first installed?
56     *
57     * @return bool
58     */
59    public function isEnabledByDefault(): bool
60    {
61        return false;
62    }
63
64    /**
65     * @return ResponseInterface
66     */
67    public function getAdminAction(): ResponseInterface
68    {
69        $this->layout = 'layouts/administration';
70
71        $api_key = $this->getPreference('api_key');
72
73        if ($api_key === '') {
74            $message = I18N::translate('This service requires an API key.');
75
76            if (Auth::isAdmin()) {
77                $message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
78            }
79
80            throw new HttpServerErrorException($message);
81        }
82
83        return $this->viewResponse('modules/here-maps/config', [
84            'api_key' => $api_key,
85            'title'   => $this->title(),
86        ]);
87    }
88
89    /**
90     * Name of the map provider.
91     *
92     * @return string
93     */
94    public function title(): string
95    {
96        return /* I18N: https://wego.here.com */ I18N::translate('Here maps');
97    }
98
99    /**
100     * @param ServerRequestInterface $request
101     *
102     * @return ResponseInterface
103     */
104    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
105    {
106        $api_key = Validator::parsedBody($request)->string('api_key');
107
108        $this->setPreference('api_key', $api_key);
109
110        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
111
112        return redirect($this->getConfigLink());
113    }
114
115    /**
116     * Parameters to create a TileLayer in LeafletJs.
117     *
118     * @return array<object>
119     */
120    public function leafletJsTileLayers(): array
121    {
122        $api_key = $this->getPreference('api_key');
123
124        return [
125            (object) [
126                'apiKey'      => $api_key,
127                'attribution' => '<a href="https://legal.here.com/en/terms/serviceterms/us">Terms of use</a> ©1987-2021 HERE',
128                'base'        => 'base',
129                'format'      => 'png8',
130                'label'       => 'Normal',
131                'mapID'       => 'newest',
132                'maxZoom'     => 19,
133                'minZoom'     => 2,
134                'size'        => 512,
135                'subdomains'  => ['1', '2', '3', '4'],
136                'type'        => 'maptile',
137                'url'         => 'https://{s}.{base}.maps.ls.hereapi.com/maptile/2.1/{type}/{mapID}/{variant}/{z}/{x}/{y}/{size}/{format}?apiKey={apiKey}',
138                'variant'     => 'normal.day',
139            ],
140            (object) [
141                'apiKey'      => $api_key,
142                'attribution' => '<a href="https://legal.here.com/en/terms/serviceterms/us">Terms of use</a> ©1987-2021 HERE',
143                'base'        => 'base',
144                'format'      => 'png8',
145                'label'       => 'Grey',
146                'mapID'       => 'newest',
147                'maxZoom'     => 19,
148                'minZoom'     => 2,
149                'size'        => 512,
150                'subdomains'  => ['1', '2', '3', '4'],
151                'type'        => 'maptile',
152                'url'         => 'https://{s}.{base}.maps.ls.hereapi.com/maptile/2.1/{type}/{mapID}/{variant}/{z}/{x}/{y}/{size}/{format}?apiKey={apiKey}',
153                'variant'     => 'normal.day.grey',
154            ],
155            (object) [
156                'apiKey'      => $api_key,
157                'attribution' => '<a href="https://legal.here.com/en/terms/serviceterms/us">Terms of use</a> ©1987-2021 HERE',
158                'base'        => 'aerial',
159                'format'      => 'png8',
160                'label'       => 'Terrain',
161                'mapID'       => 'newest',
162                'maxZoom'     => 19,
163                'minZoom'     => 2,
164                'size'        => 512,
165                'subdomains'  => ['1', '2', '3', '4'],
166                'type'        => 'maptile',
167                'url'         => 'https://{s}.{base}.maps.ls.hereapi.com/maptile/2.1/{type}/{mapID}/{variant}/{z}/{x}/{y}/{size}/{format}?apiKey={apiKey}',
168                'variant'     => 'terrain.day',
169            ],
170        ];
171    }
172}
173