xref: /webtrees/app/Module/GoogleMaps.php (revision 6f5bd0bfab12b325068b1a1c83e0c767fb752964)
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\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 GoogleMaps - use maps within webtrees
35 */
36class GoogleMaps 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.google.com/maps" dir="ltr">www.google.com/maps</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        return $this->viewResponse('modules/google-maps/config', [
74            'api_key' => $api_key,
75            'title'   => $this->title(),
76        ]);
77    }
78
79    /**
80     * Name of the map provider.
81     *
82     * @return string
83     */
84    public function title(): string
85    {
86        return I18N::translate('Google™ maps');
87    }
88
89    /**
90     * @param ServerRequestInterface $request
91     *
92     * @return ResponseInterface
93     */
94    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
95    {
96        $api_key = Validator::parsedBody($request)->string('api_key');
97
98        $this->setPreference('api_key', $api_key);
99
100        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
101
102        return redirect($this->getConfigLink());
103    }
104
105    /**
106     * Parameters to create a TileLayer in LeafletJs.
107     *
108     * @return array<object>
109     */
110    public function leafletJsTileLayers(): array
111    {
112        $api_key = $this->getPreference('api_key');
113
114        if ($api_key === '') {
115            $message = I18N::translate('This service requires an API key.');
116
117            if (Auth::isAdmin()) {
118                $message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
119            }
120
121            throw new HttpServerErrorException($message);
122        }
123
124        return [
125            (object) [
126                'GM_API_KEY'  => $api_key,
127                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
128                'default'     => true,
129                'lyrs'        => 'm',
130                'maxZoom'     => 20,
131                'minZoom'     => 2,
132                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
133                'label'       => 'Streets',
134                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
135            ],
136            (object) [
137                'GM_API_KEY'  => $api_key,
138                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
139                'default'     => false,
140                'lyrs'        => 'y',
141                'maxZoom'     => 20,
142                'minZoom'     => 2,
143                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
144                'label'       => 'Hybrid',
145                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
146            ],
147            (object) [
148                'GM_API_KEY'  => $api_key,
149                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
150                'default'     => false,
151                'lyrs'        => 's',
152                'maxZoom'     => 20,
153                'minZoom'     => 2,
154                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
155                'label'       => 'Satellite',
156                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
157            ],
158            (object) [
159                'GM_API_KEY'  => $api_key,
160                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
161                'default'     => false,
162                'lyrs'        => 'p',
163                'maxZoom'     => 20,
164                'minZoom'     => 2,
165                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
166                'label'       => 'Terrain',
167                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
168            ],
169        ];
170    }
171}
172