xref: /webtrees/app/Module/MapBox.php (revision d11be7027e34e3121be11cc025421873364403f9)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6c9c6f2ecSGreg Roach * This program is free software: you can redistribute it and/or modify
7c9c6f2ecSGreg Roach * it under the terms of the GNU General Public License as published by
8c9c6f2ecSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c9c6f2ecSGreg Roach * (at your option) any later version.
10c9c6f2ecSGreg Roach * This program is distributed in the hope that it will be useful,
11c9c6f2ecSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c9c6f2ecSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c9c6f2ecSGreg Roach * GNU General Public License for more details.
14c9c6f2ecSGreg Roach * You should have received a copy of the GNU General Public License
15c9c6f2ecSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c9c6f2ecSGreg Roach */
17c9c6f2ecSGreg Roach
18c9c6f2ecSGreg Roachdeclare(strict_types=1);
19c9c6f2ecSGreg Roach
20c9c6f2ecSGreg Roachnamespace Fisharebest\Webtrees\Module;
21c9c6f2ecSGreg Roach
22a94016a5SGreg Roachuse Fisharebest\Webtrees\Auth;
23c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24a94016a5SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
25c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\I18N;
26748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
27c9c6f2ecSGreg Roachuse Psr\Http\Message\ResponseInterface;
28c9c6f2ecSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29c9c6f2ecSGreg Roach
30a94016a5SGreg Roachuse function e;
31c9c6f2ecSGreg Roachuse function redirect;
32c9c6f2ecSGreg Roach
33c9c6f2ecSGreg Roach/**
34c9c6f2ecSGreg Roach * Class MapBox - use maps within webtrees
35c9c6f2ecSGreg Roach */
36c9c6f2ecSGreg Roachclass MapBox extends AbstractModule implements ModuleConfigInterface, ModuleMapProviderInterface
37c9c6f2ecSGreg Roach{
38c9c6f2ecSGreg Roach    use ModuleConfigTrait;
39c9c6f2ecSGreg Roach    use ModuleMapProviderTrait;
40c9c6f2ecSGreg Roach
41c9c6f2ecSGreg Roach    /**
42c9c6f2ecSGreg Roach     * Name of the map provider.
43c9c6f2ecSGreg Roach     *
44c9c6f2ecSGreg Roach     * @return string
45c9c6f2ecSGreg Roach     */
46c9c6f2ecSGreg Roach    public function description(): string
47c9c6f2ecSGreg Roach    {
48c9c6f2ecSGreg Roach        $link = '<a href="https://www.mapbox.com" dir="ltr">www.mapbox.com</a>';
49c9c6f2ecSGreg Roach
50c9c6f2ecSGreg Roach        // I18N: %s is a link/URL
51c9c6f2ecSGreg Roach        return I18N::translate('Create maps using %s.', $link);
52c9c6f2ecSGreg Roach    }
53c9c6f2ecSGreg Roach
54c9c6f2ecSGreg Roach    /**
55c9c6f2ecSGreg Roach     * Should this module be enabled when it is first installed?
56c9c6f2ecSGreg Roach     *
57c9c6f2ecSGreg Roach     * @return bool
58c9c6f2ecSGreg Roach     */
59c9c6f2ecSGreg Roach    public function isEnabledByDefault(): bool
60c9c6f2ecSGreg Roach    {
61c9c6f2ecSGreg Roach        return false;
62c9c6f2ecSGreg Roach    }
63c9c6f2ecSGreg Roach
64c9c6f2ecSGreg Roach    /**
65c9c6f2ecSGreg Roach     * @return ResponseInterface
66c9c6f2ecSGreg Roach     */
67c9c6f2ecSGreg Roach    public function getAdminAction(): ResponseInterface
68c9c6f2ecSGreg Roach    {
69c9c6f2ecSGreg Roach        $this->layout = 'layouts/administration';
70c9c6f2ecSGreg Roach
71c9c6f2ecSGreg Roach        $api_key = $this->getPreference('api_key');
72c9c6f2ecSGreg Roach
73a94016a5SGreg Roach        if ($api_key === '') {
74a94016a5SGreg Roach            $message = I18N::translate('This service requires an API key.');
75a94016a5SGreg Roach
76a94016a5SGreg Roach            if (Auth::isAdmin()) {
77a94016a5SGreg Roach                $message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
78a94016a5SGreg Roach            }
79a94016a5SGreg Roach
80a94016a5SGreg Roach            throw new HttpServerErrorException($message);
81a94016a5SGreg Roach        }
82a94016a5SGreg Roach
83cdd821aeSGreg Roach        return $this->viewResponse('modules/map-box/config', [
84c9c6f2ecSGreg Roach            'api_key' => $api_key,
85c9c6f2ecSGreg Roach            'title'   => $this->title(),
86c9c6f2ecSGreg Roach        ]);
87c9c6f2ecSGreg Roach    }
88c9c6f2ecSGreg Roach
89c9c6f2ecSGreg Roach    /**
90c9c6f2ecSGreg Roach     * Name of the map provider.
91c9c6f2ecSGreg Roach     *
92c9c6f2ecSGreg Roach     * @return string
93c9c6f2ecSGreg Roach     */
94c9c6f2ecSGreg Roach    public function title(): string
95c9c6f2ecSGreg Roach    {
969fb58c9eSGreg Roach        return /* I18N: mapbox.com */ I18N::translate('Mapbox');
97c9c6f2ecSGreg Roach    }
98c9c6f2ecSGreg Roach
99c9c6f2ecSGreg Roach    /**
100c9c6f2ecSGreg Roach     * @param ServerRequestInterface $request
101c9c6f2ecSGreg Roach     *
102c9c6f2ecSGreg Roach     * @return ResponseInterface
103c9c6f2ecSGreg Roach     */
104c9c6f2ecSGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
105c9c6f2ecSGreg Roach    {
106748dbe15SGreg Roach        $api_key = Validator::parsedBody($request)->string('api_key');
107c9c6f2ecSGreg Roach
108748dbe15SGreg Roach        $this->setPreference('api_key', $api_key);
109c9c6f2ecSGreg Roach
110c9c6f2ecSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
111c9c6f2ecSGreg Roach
112c9c6f2ecSGreg Roach        return redirect($this->getConfigLink());
113c9c6f2ecSGreg Roach    }
114c9c6f2ecSGreg Roach
115c9c6f2ecSGreg Roach    /**
116c9c6f2ecSGreg Roach     * Parameters to create a TileLayer in LeafletJs.
117c9c6f2ecSGreg Roach     *
118c9c6f2ecSGreg Roach     * @return array<object>
119c9c6f2ecSGreg Roach     */
120c9c6f2ecSGreg Roach    public function leafletJsTileLayers(): array
121c9c6f2ecSGreg Roach    {
122c9c6f2ecSGreg Roach        $api_key = $this->getPreference('api_key');
123c9c6f2ecSGreg Roach
124c9c6f2ecSGreg Roach        return [
125c9c6f2ecSGreg Roach            (object) [
126c9c6f2ecSGreg Roach                'accessToken' => $api_key,
127ad3143ccSGreg Roach                'attribution' => '©<a href="https://www.mapbox.com/about/maps">Mapbox</a> ©<a href="https://www.openstreetmap.org/copyrightt">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback">Improve this map</a></strong>',
128c9c6f2ecSGreg Roach                'default'     => false,
129c9c6f2ecSGreg Roach                'id'          => 'dark-v10',
130c9c6f2ecSGreg Roach                'label'       => 'Dark',
131c9c6f2ecSGreg Roach                'maxZoom'     => 20,
132c9c6f2ecSGreg Roach                'minZoom'     => 2,
133c9c6f2ecSGreg Roach                'subdomains'  => ['a', 'b', 'c', 'd'],
134c9c6f2ecSGreg Roach                'tileSize'    => 512,
135c9c6f2ecSGreg Roach                'url'         => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
136c9c6f2ecSGreg Roach                'zoomOffset'  => -1,
137c9c6f2ecSGreg Roach            ],
138c9c6f2ecSGreg Roach            (object) [
139c9c6f2ecSGreg Roach                'accessToken' => $api_key,
140ad3143ccSGreg Roach                'attribution' => '©<a href="https://www.mapbox.com/about/maps">Mapbox</a> ©<a href="https://www.openstreetmap.org/copyrightt">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback">Improve this map</a></strong>',
141c9c6f2ecSGreg Roach                'default'     => true,
142c9c6f2ecSGreg Roach                'id'          => 'light-v10',
143c9c6f2ecSGreg Roach                'label'       => 'Light',
144c9c6f2ecSGreg Roach                'maxZoom'     => 20,
145c9c6f2ecSGreg Roach                'minZoom'     => 2,
146c9c6f2ecSGreg Roach                'subdomains'  => ['a', 'b', 'c', 'd'],
147c9c6f2ecSGreg Roach                'tileSize'    => 512,
148c9c6f2ecSGreg Roach                'url'         => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
149c9c6f2ecSGreg Roach                'zoomOffset'  => -1,
150c9c6f2ecSGreg Roach            ],
151c9c6f2ecSGreg Roach            (object) [
152c9c6f2ecSGreg Roach                'accessToken' => $api_key,
153ad3143ccSGreg Roach                'attribution' => '©<a href="https://www.mapbox.com/about/maps">Mapbox</a> ©<a href="https://www.openstreetmap.org/copyrightt">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback">Improve this map</a></strong>',
154c9c6f2ecSGreg Roach                'default'     => false,
155c9c6f2ecSGreg Roach                'id'          => 'outdoors-v11',
156c9c6f2ecSGreg Roach                'label'       => 'Outdoors',
157c9c6f2ecSGreg Roach                'maxZoom'     => 20,
158c9c6f2ecSGreg Roach                'minZoom'     => 2,
159c9c6f2ecSGreg Roach                'subdomains'  => ['a', 'b', 'c', 'd'],
160c9c6f2ecSGreg Roach                'tileSize'    => 512,
161c9c6f2ecSGreg Roach                'url'         => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
162c9c6f2ecSGreg Roach                'zoomOffset'  => -1,
163c9c6f2ecSGreg Roach            ],
164c9c6f2ecSGreg Roach            (object) [
165c9c6f2ecSGreg Roach                'accessToken' => $api_key,
166ad3143ccSGreg Roach                'attribution' => '©<a href="https://www.mapbox.com/about/maps">Mapbox</a> ©<a href="https://www.openstreetmap.org/copyrightt">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback">Improve this map</a></strong>',
167c9c6f2ecSGreg Roach                'default'     => false,
168c9c6f2ecSGreg Roach                'id'          => 'satellite-v9',
169c9c6f2ecSGreg Roach                'label'       => 'Satellite',
170c9c6f2ecSGreg Roach                'maxZoom'     => 20,
171c9c6f2ecSGreg Roach                'minZoom'     => 2,
172c9c6f2ecSGreg Roach                'subdomains'  => ['a', 'b', 'c', 'd'],
173c9c6f2ecSGreg Roach                'tileSize'    => 512,
174c9c6f2ecSGreg Roach                'url'         => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
175c9c6f2ecSGreg Roach                'zoomOffset'  => -1,
176c9c6f2ecSGreg Roach            ],
177c9c6f2ecSGreg Roach            (object) [
178c9c6f2ecSGreg Roach                'accessToken' => $api_key,
179ad3143ccSGreg Roach                'attribution' => '©<a href="https://www.mapbox.com/about/maps">Mapbox</a> ©<a href="https://www.openstreetmap.org/copyrightt">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback">Improve this map</a></strong>',
180c9c6f2ecSGreg Roach                'default'     => false,
181c9c6f2ecSGreg Roach                'id'          => 'streets-v11',
182c9c6f2ecSGreg Roach                'label'       => 'Streets',
183c9c6f2ecSGreg Roach                'maxZoom'     => 20,
184c9c6f2ecSGreg Roach                'minZoom'     => 2,
185c9c6f2ecSGreg Roach                'subdomains'  => ['a', 'b', 'c', 'd'],
186c9c6f2ecSGreg Roach                'tileSize'    => 512,
187c9c6f2ecSGreg Roach                'url'         => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
188c9c6f2ecSGreg Roach                'zoomOffset'  => -1,
189c9c6f2ecSGreg Roach            ],
190c9c6f2ecSGreg Roach        ];
191c9c6f2ecSGreg Roach    }
192c9c6f2ecSGreg Roach}
193