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