1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Registry; 27use Fisharebest\Webtrees\Validator; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30 31use function redirect; 32 33/** 34 * Class BingMaps - use maps within webtrees 35 */ 36class BingMaps 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.bing.com/maps" dir="ltr">www.bing.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/bing-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('Bing™ 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 'bingMapsKey' => $api_key, 127 'default' => false, 128 'imagerySet' => 'Aerial', 129 'label' => 'Aerial', 130 'maxZoom' => 15, 131 'minZoom' => 2, 132 ], 133 (object) [ 134 'bingMapsKey' => $api_key, 135 'default' => false, 136 'imagerySet' => 'CanvasDark', 137 'label' => 'Dark', 138 'maxZoom' => 15, 139 'minZoom' => 2, 140 ], 141 (object) [ 142 'bingMapsKey' => $api_key, 143 'default' => false, 144 'imagerySet' => 'CanvasGray', 145 'label' => 'Grey', 146 'maxZoom' => 15, 147 'minZoom' => 2, 148 ], 149 (object) [ 150 'bingMapsKey' => $api_key, 151 'default' => true, 152 'imagerySet' => 'CanvasLight', 153 'label' => 'Light', 154 'maxZoom' => 15, 155 'minZoom' => 2, 156 ], 157 (object) [ 158 'bingMapsKey' => $api_key, 159 'default' => false, 160 'imagerySet' => 'Road', 161 'label' => 'Road', 162 'maxZoom' => 15, 163 'minZoom' => 2, 164 ], 165 ]; 166 } 167} 168