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 MapBox - use maps within webtrees 32 */ 33class MapBox 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.mapbox.com" dir="ltr">www.mapbox.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/map-box/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: mapbox.com */ I18N::translate('Mapbox'); 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 'accessToken' => $api_key, 114 '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>', 115 'default' => false, 116 'id' => 'dark-v10', 117 'label' => 'Dark', 118 'maxZoom' => 20, 119 'minZoom' => 2, 120 'subdomains' => ['a', 'b', 'c', 'd'], 121 'tileSize' => 512, 122 'url' => 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', 123 'zoomOffset' => -1, 124 ], 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' => true, 129 'id' => 'light-v10', 130 'label' => 'Light', 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' => false, 142 'id' => 'outdoors-v11', 143 'label' => 'Outdoors', 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' => 'satellite-v9', 156 'label' => 'Satellite', 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' => 'streets-v11', 169 'label' => 'Streets', 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 ]; 178 } 179} 180