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\FlashMessages; 23use Fisharebest\Webtrees\Gedcom; 24use Fisharebest\Webtrees\Html; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Validator; 27use GuzzleHttp\Psr7\Request; 28use Psr\Http\Message\RequestInterface; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31 32use function array_filter; 33use function implode; 34use function json_decode; 35use function redirect; 36use function usort; 37 38use const JSON_THROW_ON_ERROR; 39 40/** 41 * Class OpenRouteServiceAutocomplete - use openrouteservice.org to search for place names 42 */ 43class OpenRouteServiceAutocomplete extends AbstractModule implements ModuleConfigInterface, ModuleMapAutocompleteInterface 44{ 45 use ModuleConfigTrait; 46 use ModuleMapAutocompleteTrait; 47 48 /** 49 * Name of the map provider. 50 * 51 * @return string 52 */ 53 public function description(): string 54 { 55 $link = '<a href="https://openrouteservice.org">openrouteservice.org</a>'; 56 57 return I18N::translate('Search for place names using %s.', $link); 58 } 59 60 /** 61 * @return ResponseInterface 62 */ 63 public function getAdminAction(): ResponseInterface 64 { 65 $this->layout = 'layouts/administration'; 66 67 $api_key = $this->getPreference('api_key'); 68 69 return $this->viewResponse('modules/openrouteservice/config', [ 70 'api_key' => $api_key, 71 'title' => $this->title(), 72 ]); 73 } 74 75 /** 76 * Name of the map provider. 77 * 78 * @return string 79 */ 80 public function title(): string 81 { 82 return I18N::translate('OpenRouteService'); 83 } 84 85 /** 86 * Should this module be enabled when it is first installed? 87 * 88 * @return bool 89 */ 90 public function isEnabledByDefault(): bool 91 { 92 return false; 93 } 94 95 /** 96 * @param ServerRequestInterface $request 97 * 98 * @return ResponseInterface 99 */ 100 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 101 { 102 $api_key = Validator::parsedBody($request)->string('api_key'); 103 104 $this->setPreference('api_key', $api_key); 105 106 FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 107 108 return redirect($this->getConfigLink()); 109 } 110 111 /** 112 * @param string $place 113 * 114 * @return RequestInterface 115 */ 116 protected function createPlaceNameSearchRequest(string $place): RequestInterface 117 { 118 $api_key = $this->getPreference('api_key'); 119 120 $uri = Html::url('https://api.openrouteservice.org/geocode/autocomplete', [ 121 'api_key' => $api_key, 122 'text' => $place, 123 'layers' => 'coarse', 124 ]); 125 126 return new Request('GET', $uri); 127 } 128 129 /** 130 * @param ResponseInterface $response 131 * 132 * @return array<string> 133 */ 134 protected function parsePlaceNameSearchResponse(ResponseInterface $response): array 135 { 136 $body = $response->getBody()->getContents(); 137 $places = []; 138 $results = json_decode($body, false, 512, JSON_THROW_ON_ERROR); 139 140 foreach ($results->features as $result) { 141 $result->properties->name ??= null; 142 $result->properties->county ??= null; 143 $result->properties->region ??= null; 144 $result->properties->macroregion ??= null; 145 $result->properties->country ??= null; 146 147 if ($result->properties->country === 'United Kingdom') { 148 // macroregion will contain England, Scotland, etc. 149 $result->properties->country = null; 150 // region will contain the county. 151 $result->properties->region = null; 152 } 153 154 $parts = [ 155 $result->properties->name, 156 $result->properties->county, 157 $result->properties->region, 158 $result->properties->macroregion, 159 $result->properties->country, 160 ]; 161 162 $places[] = implode(Gedcom::PLACE_SEPARATOR, array_filter($parts)) ?: $result->properties->label; 163 } 164 165 usort($places, I18N::comparator()); 166 167 return $places; 168 } 169} 170