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