xref: /webtrees/app/Module/GoogleMaps.php (revision d99c9da5728c2ac7aa94467fca8c8a77af5cf9a4)
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 GoogleMaps - use maps within webtrees
32 */
33class GoogleMaps 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.google.com/maps" dir="ltr">www.google.com/maps</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/google-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::translate('Google™ 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                'GM_API_KEY'  => $api_key,
114                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
115                'default'     => true,
116                'lyrs'        => 'm',
117                'maxZoom'     => 20,
118                'minZoom'     => 2,
119                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
120                'label'       => 'Streets',
121                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
122            ],
123            (object) [
124                'GM_API_KEY'  => $api_key,
125                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
126                'default'     => false,
127                'lyrs'        => 'y',
128                'maxZoom'     => 20,
129                'minZoom'     => 2,
130                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
131                'label'       => 'Hybrid',
132                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
133            ],
134            (object) [
135                'GM_API_KEY'  => $api_key,
136                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
137                'default'     => false,
138                'lyrs'        => 's',
139                'maxZoom'     => 20,
140                'minZoom'     => 2,
141                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
142                'label'       => 'Satellite',
143                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
144            ],
145            (object) [
146                'GM_API_KEY'  => $api_key,
147                'attribution' => 'Map data &copy2021 Google LLC <a href="https://www.google.com/intl/en-GB_US/help/terms_maps">Terms of use</a>',
148                'default'     => false,
149                'lyrs'        => 'p',
150                'maxZoom'     => 20,
151                'minZoom'     => 2,
152                'subdomains'  => ['mt0', 'mt1', 'mt2', 'mt3'],
153                'label'       => 'Terrain',
154                'url'         => 'https://{s}.google.com/vt/lyrs={lyrs}&x={x}&y={y}&z={z}',
155            ],
156        ];
157    }
158}
159