xref: /webtrees/app/Module/OpenRouteServiceAutocomplete.php (revision 53a83c54b8b942be5ba4e73683d0401e77cc44e4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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        // I18N: openrouteservice.org
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        $params = (array) $request->getParsedBody();
103
104        $this->setPreference('api_key', $params['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            if ($result->properties->country === 'United Kingdom') {
142                // macroregion will contain England, Scotland, etc.
143                $result->properties->country = null;
144            }
145
146            $parts = [
147                $result->properties->name ?? null,
148                $result->properties->county ?? null,
149                $result->properties->region ?? null,
150                $result->properties->macroregion ?? null,
151                $result->properties->country ?? null,
152            ];
153
154            $places[] = implode(Gedcom::PLACE_SEPARATOR, array_filter($parts)) ?: $result->properties->label;
155        }
156
157        usort($places, I18N::comparator());
158
159        return $places;
160    }
161}
162