xref: /webtrees/app/Module/OpenRouteServiceAutocomplete.php (revision bc251ebe44f96e2ace3e34d8e38fd186f992618e)
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 JsonException;
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        // I18N: openrouteservice.org
83        return I18N::translate('OpenRouteService');
84    }
85
86    /**
87     * Should this module be enabled when it is first installed?
88     *
89     * @return bool
90     */
91    public function isEnabledByDefault(): bool
92    {
93        return false;
94    }
95
96    /**
97     * @param ServerRequestInterface $request
98     *
99     * @return ResponseInterface
100     */
101    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
102    {
103        $params = (array) $request->getParsedBody();
104
105        $this->setPreference('api_key', $params['api_key'] ?? '');
106
107        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
108
109        return redirect($this->getConfigLink());
110    }
111
112    /**
113     * @param string $place
114     *
115     * @return RequestInterface
116     */
117    protected function createPlaceNameSearchRequest(string $place): RequestInterface
118    {
119        $api_key = $this->getPreference('api_key');
120
121        $uri = Html::url('https://api.openrouteservice.org/geocode/autocomplete', [
122            'api_key' => $api_key,
123            'text'    => $place,
124            'layers'  => 'coarse',
125        ]);
126
127        return new Request('GET', $uri);
128    }
129
130    /**
131     * @param ResponseInterface $response
132     *
133     * @return array<string>
134     */
135    protected function parsePlaceNameSearchResponse(ResponseInterface $response): array
136    {
137        $body   = $response->getBody()->getContents();
138        $places = [];
139
140        try {
141            $results = json_decode($body, false, 512, JSON_THROW_ON_ERROR);
142            foreach ($results->features as $result) {
143                if ($result->properties->country === 'United Kingdom') {
144                    // macroregion will contain England, Scotland, etc.
145                    $result->properties->country = null;
146                }
147
148                $parts = [
149                    $result->properties->name ?? null,
150                    $result->properties->county ?? null,
151                    $result->properties->region ?? null,
152                    $result->properties->macroregion ?? null,
153                    $result->properties->country ?? null,
154                ];
155
156                $places[] = implode(Gedcom::PLACE_SEPARATOR, array_filter($parts)) ?: $result->properties->label;
157            }
158        } catch (JsonException $ex) {
159        }
160
161        usort($places, I18N::comparator());
162
163        return $places;
164    }
165}
166