xref: /webtrees/app/Module/OpenRouteServiceAutocomplete.php (revision f52ec17fcd96ea572eea1bce149d1a403414e1cc)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
5c9c6f2ecSGreg Roach * Copyright (C) 2021 webtrees development team
6c9c6f2ecSGreg Roach * This program is free software: you can redistribute it and/or modify
7c9c6f2ecSGreg Roach * it under the terms of the GNU General Public License as published by
8c9c6f2ecSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c9c6f2ecSGreg Roach * (at your option) any later version.
10c9c6f2ecSGreg Roach * This program is distributed in the hope that it will be useful,
11c9c6f2ecSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c9c6f2ecSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c9c6f2ecSGreg Roach * GNU General Public License for more details.
14c9c6f2ecSGreg Roach * You should have received a copy of the GNU General Public License
15c9c6f2ecSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c9c6f2ecSGreg Roach */
17c9c6f2ecSGreg Roach
18c9c6f2ecSGreg Roachdeclare(strict_types=1);
19c9c6f2ecSGreg Roach
20c9c6f2ecSGreg Roachnamespace Fisharebest\Webtrees\Module;
21c9c6f2ecSGreg Roach
22c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
23c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Gedcom;
24c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Html;
25c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\I18N;
26c9c6f2ecSGreg Roachuse GuzzleHttp\Psr7\Request;
27c9c6f2ecSGreg Roachuse Psr\Http\Message\RequestInterface;
28c9c6f2ecSGreg Roachuse Psr\Http\Message\ResponseInterface;
29c9c6f2ecSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30c9c6f2ecSGreg Roach
31c9c6f2ecSGreg Roachuse function array_filter;
32c9c6f2ecSGreg Roachuse function implode;
33c9c6f2ecSGreg Roachuse function json_decode;
34c9c6f2ecSGreg Roachuse function redirect;
35c9c6f2ecSGreg Roachuse function usort;
36c9c6f2ecSGreg Roach
37c9c6f2ecSGreg Roachuse const JSON_THROW_ON_ERROR;
38c9c6f2ecSGreg Roach
39c9c6f2ecSGreg Roach/**
40c9c6f2ecSGreg Roach * Class OpenRouteServiceAutocomplete - use openrouteservice.org to search for place names
41c9c6f2ecSGreg Roach */
42c9c6f2ecSGreg Roachclass OpenRouteServiceAutocomplete extends AbstractModule implements ModuleConfigInterface, ModuleMapAutocompleteInterface
43c9c6f2ecSGreg Roach{
44c9c6f2ecSGreg Roach    use ModuleConfigTrait;
45c9c6f2ecSGreg Roach    use ModuleMapAutocompleteTrait;
46c9c6f2ecSGreg Roach
47c9c6f2ecSGreg Roach    /**
48c9c6f2ecSGreg Roach     * Name of the map provider.
49c9c6f2ecSGreg Roach     *
50c9c6f2ecSGreg Roach     * @return string
51c9c6f2ecSGreg Roach     */
52c9c6f2ecSGreg Roach    public function description(): string
53c9c6f2ecSGreg Roach    {
54c9c6f2ecSGreg Roach        $link = '<a href="https://openrouteservice.org">openrouteservice.org</a>';
55c9c6f2ecSGreg Roach
56c9c6f2ecSGreg Roach        return I18N::translate('Search for place names using %s.', $link);
57c9c6f2ecSGreg Roach    }
58c9c6f2ecSGreg Roach
59c9c6f2ecSGreg Roach    /**
60c9c6f2ecSGreg Roach     * @return ResponseInterface
61c9c6f2ecSGreg Roach     */
62c9c6f2ecSGreg Roach    public function getAdminAction(): ResponseInterface
63c9c6f2ecSGreg Roach    {
64c9c6f2ecSGreg Roach        $this->layout = 'layouts/administration';
65c9c6f2ecSGreg Roach
66c9c6f2ecSGreg Roach        $api_key = $this->getPreference('api_key');
67c9c6f2ecSGreg Roach
68c9c6f2ecSGreg Roach        return $this->viewResponse('modules/openrouteservice/config', [
69c9c6f2ecSGreg Roach            'api_key' => $api_key,
70c9c6f2ecSGreg Roach            'title'   => $this->title(),
71c9c6f2ecSGreg Roach        ]);
72c9c6f2ecSGreg Roach    }
73c9c6f2ecSGreg Roach
74c9c6f2ecSGreg Roach    /**
75c9c6f2ecSGreg Roach     * Name of the map provider.
76c9c6f2ecSGreg Roach     *
77c9c6f2ecSGreg Roach     * @return string
78c9c6f2ecSGreg Roach     */
79c9c6f2ecSGreg Roach    public function title(): string
80c9c6f2ecSGreg Roach    {
81c9c6f2ecSGreg Roach        return I18N::translate('OpenRouteService');
82c9c6f2ecSGreg Roach    }
83c9c6f2ecSGreg Roach
84c9c6f2ecSGreg Roach    /**
85c9c6f2ecSGreg Roach     * Should this module be enabled when it is first installed?
86c9c6f2ecSGreg Roach     *
87c9c6f2ecSGreg Roach     * @return bool
88c9c6f2ecSGreg Roach     */
89c9c6f2ecSGreg Roach    public function isEnabledByDefault(): bool
90c9c6f2ecSGreg Roach    {
91c9c6f2ecSGreg Roach        return false;
92c9c6f2ecSGreg Roach    }
93c9c6f2ecSGreg Roach
94c9c6f2ecSGreg Roach    /**
95c9c6f2ecSGreg Roach     * @param ServerRequestInterface $request
96c9c6f2ecSGreg Roach     *
97c9c6f2ecSGreg Roach     * @return ResponseInterface
98c9c6f2ecSGreg Roach     */
99c9c6f2ecSGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
100c9c6f2ecSGreg Roach    {
101c9c6f2ecSGreg Roach        $params = (array) $request->getParsedBody();
102c9c6f2ecSGreg Roach
103c9c6f2ecSGreg Roach        $this->setPreference('api_key', $params['api_key'] ?? '');
104c9c6f2ecSGreg Roach
105c9c6f2ecSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
106c9c6f2ecSGreg Roach
107c9c6f2ecSGreg Roach        return redirect($this->getConfigLink());
108c9c6f2ecSGreg Roach    }
109c9c6f2ecSGreg Roach
110c9c6f2ecSGreg Roach    /**
111c9c6f2ecSGreg Roach     * @param string $place
112c9c6f2ecSGreg Roach     *
113c9c6f2ecSGreg Roach     * @return RequestInterface
114c9c6f2ecSGreg Roach     */
115c9c6f2ecSGreg Roach    protected function createPlaceNameSearchRequest(string $place): RequestInterface
116c9c6f2ecSGreg Roach    {
117c9c6f2ecSGreg Roach        $api_key = $this->getPreference('api_key');
118c9c6f2ecSGreg Roach
119c9c6f2ecSGreg Roach        $uri = Html::url('https://api.openrouteservice.org/geocode/autocomplete', [
120c9c6f2ecSGreg Roach            'api_key' => $api_key,
121c9c6f2ecSGreg Roach            'text'    => $place,
122c9c6f2ecSGreg Roach            'layers'  => 'coarse',
123c9c6f2ecSGreg Roach        ]);
124c9c6f2ecSGreg Roach
125c9c6f2ecSGreg Roach        return new Request('GET', $uri);
126c9c6f2ecSGreg Roach    }
127c9c6f2ecSGreg Roach
128c9c6f2ecSGreg Roach    /**
129c9c6f2ecSGreg Roach     * @param ResponseInterface $response
130c9c6f2ecSGreg Roach     *
131c9c6f2ecSGreg Roach     * @return array<string>
132c9c6f2ecSGreg Roach     */
133c9c6f2ecSGreg Roach    protected function parsePlaceNameSearchResponse(ResponseInterface $response): array
134c9c6f2ecSGreg Roach    {
135c9c6f2ecSGreg Roach        $body    = $response->getBody()->getContents();
136c9c6f2ecSGreg Roach        $places  = [];
137c9c6f2ecSGreg Roach        $results = json_decode($body, false, 512, JSON_THROW_ON_ERROR);
1382aeda30fSGreg Roach
139c9c6f2ecSGreg Roach        foreach ($results->features as $result) {
140cada4c7cSGreg Roach            $result->properties->name ??= null;
141cada4c7cSGreg Roach            $result->properties->county ??= null;
142cada4c7cSGreg Roach            $result->properties->region ??= null;
143cada4c7cSGreg Roach            $result->properties->macroregion ??= null;
144cada4c7cSGreg Roach            $result->properties->country ??= null;
145cada4c7cSGreg Roach
146c9c6f2ecSGreg Roach            if ($result->properties->country === 'United Kingdom') {
147c9c6f2ecSGreg Roach                // macroregion will contain England, Scotland, etc.
148c9c6f2ecSGreg Roach                $result->properties->country = null;
149*f52ec17fSGreg Roach                // region will contain the county.
150*f52ec17fSGreg Roach                $result->properties->region = null;
151c9c6f2ecSGreg Roach            }
152c9c6f2ecSGreg Roach
153c9c6f2ecSGreg Roach            $parts = [
154cada4c7cSGreg Roach                $result->properties->name,
155cada4c7cSGreg Roach                $result->properties->county,
156cada4c7cSGreg Roach                $result->properties->region,
157cada4c7cSGreg Roach                $result->properties->macroregion,
158cada4c7cSGreg Roach                $result->properties->country,
159c9c6f2ecSGreg Roach            ];
160c9c6f2ecSGreg Roach
161c9c6f2ecSGreg Roach            $places[] = implode(Gedcom::PLACE_SEPARATOR, array_filter($parts)) ?: $result->properties->label;
162c9c6f2ecSGreg Roach        }
163c9c6f2ecSGreg Roach
164c9c6f2ecSGreg Roach        usort($places, I18N::comparator());
165c9c6f2ecSGreg Roach
166c9c6f2ecSGreg Roach        return $places;
167c9c6f2ecSGreg Roach    }
168c9c6f2ecSGreg Roach}
169