xref: /webtrees/app/Module/GeonamesAutocomplete.php (revision d11be7027e34e3121be11cc025421873364403f9)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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 Fisharebest\Webtrees\Site;
27748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
28c9c6f2ecSGreg Roachuse GuzzleHttp\Psr7\Request;
29c9c6f2ecSGreg Roachuse Psr\Http\Message\RequestInterface;
30c9c6f2ecSGreg Roachuse Psr\Http\Message\ResponseInterface;
31c9c6f2ecSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32c9c6f2ecSGreg Roach
33c9c6f2ecSGreg Roachuse function array_filter;
34c9c6f2ecSGreg Roachuse function implode;
35c9c6f2ecSGreg Roachuse function json_decode;
36c9c6f2ecSGreg Roachuse function redirect;
37c9c6f2ecSGreg Roachuse function usort;
38c9c6f2ecSGreg Roach
39c9c6f2ecSGreg Roachuse const JSON_THROW_ON_ERROR;
40c9c6f2ecSGreg Roach
41c9c6f2ecSGreg Roach/**
42c9c6f2ecSGreg Roach * Class GeonamesAutocomplete - use geonames.org to search for place names
43c9c6f2ecSGreg Roach */
44c9c6f2ecSGreg Roachclass GeonamesAutocomplete extends AbstractModule implements ModuleConfigInterface, ModuleMapAutocompleteInterface
45c9c6f2ecSGreg Roach{
46c9c6f2ecSGreg Roach    use ModuleConfigTrait;
47c9c6f2ecSGreg Roach    use ModuleMapAutocompleteTrait;
48c9c6f2ecSGreg Roach
49c9c6f2ecSGreg Roach    /**
50c9c6f2ecSGreg Roach     * Name of the map provider.
51c9c6f2ecSGreg Roach     *
52c9c6f2ecSGreg Roach     * @return string
53c9c6f2ecSGreg Roach     */
54c9c6f2ecSGreg Roach    public function title(): string
55c9c6f2ecSGreg Roach    {
565b71d802SGreg Roach        // I18N: https://www.geonames.org
57c9c6f2ecSGreg Roach        return I18N::translate('GeoNames');
58c9c6f2ecSGreg Roach    }
59c9c6f2ecSGreg Roach
60c9c6f2ecSGreg Roach    /**
61c9c6f2ecSGreg Roach     * Name of the map provider.
62c9c6f2ecSGreg Roach     *
63c9c6f2ecSGreg Roach     * @return string
64c9c6f2ecSGreg Roach     */
65c9c6f2ecSGreg Roach    public function description(): string
66c9c6f2ecSGreg Roach    {
67c9c6f2ecSGreg Roach        $link = '<a href="https://geonames.org">geonames.org</a>';
68c9c6f2ecSGreg Roach
69c9c6f2ecSGreg Roach        return I18N::translate('Search for place names using %s.', $link);
70c9c6f2ecSGreg Roach    }
71c9c6f2ecSGreg Roach
72c9c6f2ecSGreg Roach    /**
73c9c6f2ecSGreg Roach     * Should this module be enabled when it is first installed?
74c9c6f2ecSGreg Roach     *
75c9c6f2ecSGreg Roach     * @return bool
76c9c6f2ecSGreg Roach     */
77c9c6f2ecSGreg Roach    public function isEnabledByDefault(): bool
78c9c6f2ecSGreg Roach    {
79c9c6f2ecSGreg Roach        return false;
80c9c6f2ecSGreg Roach    }
81c9c6f2ecSGreg Roach
82c9c6f2ecSGreg Roach    /**
83c9c6f2ecSGreg Roach     * @return ResponseInterface
84c9c6f2ecSGreg Roach     */
85c9c6f2ecSGreg Roach    public function getAdminAction(): ResponseInterface
86c9c6f2ecSGreg Roach    {
87c9c6f2ecSGreg Roach        $this->layout = 'layouts/administration';
88c9c6f2ecSGreg Roach
89c9c6f2ecSGreg Roach        // This was a global setting before it became a module setting...
90c9c6f2ecSGreg Roach        $default  = Site::getPreference('geonames');
91c9c6f2ecSGreg Roach        $username = $this->getPreference('username', $default);
92c9c6f2ecSGreg Roach
93c9c6f2ecSGreg Roach        return $this->viewResponse('modules/geonames/config', [
94c9c6f2ecSGreg Roach            'username' => $username,
95c9c6f2ecSGreg Roach            'title'    => $this->title(),
96c9c6f2ecSGreg Roach        ]);
97c9c6f2ecSGreg Roach    }
98c9c6f2ecSGreg Roach
99c9c6f2ecSGreg Roach    /**
100c9c6f2ecSGreg Roach     * @param ServerRequestInterface $request
101c9c6f2ecSGreg Roach     *
102c9c6f2ecSGreg Roach     * @return ResponseInterface
103c9c6f2ecSGreg Roach     */
104c9c6f2ecSGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
105c9c6f2ecSGreg Roach    {
106748dbe15SGreg Roach        $username = Validator::parsedBody($request)->string('username');
107c9c6f2ecSGreg Roach
108748dbe15SGreg Roach        $this->setPreference('username', $username);
109c9c6f2ecSGreg Roach
110c9c6f2ecSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
111c9c6f2ecSGreg Roach
112c9c6f2ecSGreg Roach        return redirect($this->getConfigLink());
113c9c6f2ecSGreg Roach    }
114c9c6f2ecSGreg Roach
115c9c6f2ecSGreg Roach    /**
116c9c6f2ecSGreg Roach     * @param string $place
117c9c6f2ecSGreg Roach     *
118c9c6f2ecSGreg Roach     * @return RequestInterface
119c9c6f2ecSGreg Roach     */
120c9c6f2ecSGreg Roach    protected function createPlaceNameSearchRequest(string $place): RequestInterface
121c9c6f2ecSGreg Roach    {
122c9c6f2ecSGreg Roach        // This was a global setting before it became a module setting...
123c9c6f2ecSGreg Roach        $default  = Site::getPreference('geonames');
124c9c6f2ecSGreg Roach        $username = $this->getPreference('username', $default);
125c9c6f2ecSGreg Roach
126c9c6f2ecSGreg Roach        $uri = Html::url('https://secure.geonames.org/searchJSON', [
127c9c6f2ecSGreg Roach            'name_startsWith' => $place,
128c9c6f2ecSGreg Roach            'featureClass'    => 'P',
129c9c6f2ecSGreg Roach            'lang'            => I18N::languageTag(),
130c9c6f2ecSGreg Roach            'username'        => $username,
131c9c6f2ecSGreg Roach        ]);
132c9c6f2ecSGreg Roach
133c9c6f2ecSGreg Roach        return new Request('GET', $uri);
134c9c6f2ecSGreg Roach    }
135c9c6f2ecSGreg Roach
136c9c6f2ecSGreg Roach    /**
137c9c6f2ecSGreg Roach     * @param ResponseInterface $response
138c9c6f2ecSGreg Roach     *
139c9c6f2ecSGreg Roach     * @return array<string>
140c9c6f2ecSGreg Roach     */
141c9c6f2ecSGreg Roach    protected function parsePlaceNameSearchResponse(ResponseInterface $response): array
142c9c6f2ecSGreg Roach    {
143c9c6f2ecSGreg Roach        $body    = $response->getBody()->getContents();
144c9c6f2ecSGreg Roach        $places  = [];
145c9c6f2ecSGreg Roach        $results = json_decode($body, false, 512, JSON_THROW_ON_ERROR);
146c9c6f2ecSGreg Roach
147c9c6f2ecSGreg Roach        foreach ($results->geonames as $result) {
148c9c6f2ecSGreg Roach            if (($result->countryName ?? null) === 'United Kingdom') {
149c9c6f2ecSGreg Roach                // adminName1 will be England, Scotland, etc.
150c9c6f2ecSGreg Roach                $result->countryName = null;
151c9c6f2ecSGreg Roach            }
152c9c6f2ecSGreg Roach
153c9c6f2ecSGreg Roach            $parts = [
154c9c6f2ecSGreg Roach                $result->name,
155c9c6f2ecSGreg Roach                $result->adminName2 ?? null,
156c9c6f2ecSGreg Roach                $result->adminName1 ?? null,
157c9c6f2ecSGreg Roach                $result->countryName ?? null,
158c9c6f2ecSGreg Roach            ];
159c9c6f2ecSGreg Roach
160c9c6f2ecSGreg Roach            $places[] = implode(Gedcom::PLACE_SEPARATOR, array_filter($parts));
161c9c6f2ecSGreg Roach        }
162c9c6f2ecSGreg Roach
163c9c6f2ecSGreg Roach        usort($places, I18N::comparator());
164c9c6f2ecSGreg Roach
165c9c6f2ecSGreg Roach        return $places;
166c9c6f2ecSGreg Roach    }
167c9c6f2ecSGreg Roach}
168