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 MapBox - use maps within webtrees 35 */ 36class MapBox 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.mapbox.com" dir="ltr">www.mapbox.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/map-box/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: mapbox.com */ I18N::translate('Mapbox'); 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 'accessToken' => $api_key, 127 '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>', 128 'default' => false, 129 'id' => 'dark-v10', 130 'label' => 'Dark', 131 'maxZoom' => 20, 132 'minZoom' => 2, 133 'subdomains' => ['a', 'b', 'c', 'd'], 134 'tileSize' => 512, 135 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 136 'zoomOffset' => -1, 137 ], 138 (object) [ 139 'accessToken' => $api_key, 140 '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>', 141 'default' => true, 142 'id' => 'light-v10', 143 'label' => 'Light', 144 'maxZoom' => 20, 145 'minZoom' => 2, 146 'subdomains' => ['a', 'b', 'c', 'd'], 147 'tileSize' => 512, 148 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 149 'zoomOffset' => -1, 150 ], 151 (object) [ 152 'accessToken' => $api_key, 153 '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>', 154 'default' => false, 155 'id' => 'outdoors-v11', 156 'label' => 'Outdoors', 157 'maxZoom' => 20, 158 'minZoom' => 2, 159 'subdomains' => ['a', 'b', 'c', 'd'], 160 'tileSize' => 512, 161 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 162 'zoomOffset' => -1, 163 ], 164 (object) [ 165 'accessToken' => $api_key, 166 '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>', 167 'default' => false, 168 'id' => 'satellite-v9', 169 'label' => 'Satellite', 170 'maxZoom' => 20, 171 'minZoom' => 2, 172 'subdomains' => ['a', 'b', 'c', 'd'], 173 'tileSize' => 512, 174 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 175 'zoomOffset' => -1, 176 ], 177 (object) [ 178 'accessToken' => $api_key, 179 '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>', 180 'default' => false, 181 'id' => 'streets-v11', 182 'label' => 'Streets', 183 'maxZoom' => 20, 184 'minZoom' => 2, 185 'subdomains' => ['a', 'b', 'c', 'd'], 186 'tileSize' => 512, 187 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 188 'zoomOffset' => -1, 189 ], 190 ]; 191 } 192} 193