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