xref: /webtrees/app/Http/RequestHandlers/AutoCompletePlace.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
163763244SGreg Roach<?php
263763244SGreg Roach
363763244SGreg Roach/**
463763244SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
663763244SGreg Roach * This program is free software: you can redistribute it and/or modify
763763244SGreg Roach * it under the terms of the GNU General Public License as published by
863763244SGreg Roach * the Free Software Foundation, either version 3 of the License, or
963763244SGreg Roach * (at your option) any later version.
1063763244SGreg Roach * This program is distributed in the hope that it will be useful,
1163763244SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1263763244SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1363763244SGreg Roach * GNU General Public License for more details.
1463763244SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1663763244SGreg Roach */
1763763244SGreg Roach
1863763244SGreg Roachdeclare(strict_types=1);
1963763244SGreg Roach
2063763244SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2163763244SGreg Roach
22c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface;
2363763244SGreg Roachuse Fisharebest\Webtrees\Place;
24c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
25c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2763763244SGreg Roachuse Illuminate\Support\Collection;
2863763244SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2963763244SGreg Roach
3063763244SGreg Roach/**
3163763244SGreg Roach * Autocomplete handler for places
3263763244SGreg Roach */
3363763244SGreg Roachclass AutoCompletePlace extends AbstractAutocompleteHandler
3463763244SGreg Roach{
35c9c6f2ecSGreg Roach    private ModuleService $module_service;
36c9c6f2ecSGreg Roach
37c9c6f2ecSGreg Roach    /**
3873d58381SGreg Roach     * @param ModuleService $module_service
39110d87e5SGreg Roach     * @param SearchService $search_service
40c9c6f2ecSGreg Roach     */
41110d87e5SGreg Roach    public function __construct(ModuleService $module_service, SearchService $search_service)
42c9c6f2ecSGreg Roach    {
43c9c6f2ecSGreg Roach        parent::__construct($search_service);
44c9c6f2ecSGreg Roach
45c9c6f2ecSGreg Roach        $this->module_service = $module_service;
46c9c6f2ecSGreg Roach    }
47c9c6f2ecSGreg Roach
48928b8592SGreg Roach    /**
49928b8592SGreg Roach     * @param ServerRequestInterface $request
50928b8592SGreg Roach     *
5136779af1SGreg Roach     * @return Collection<int,string>
52928b8592SGreg Roach     */
5363763244SGreg Roach    protected function search(ServerRequestInterface $request): Collection
5463763244SGreg Roach    {
55b55cbc6bSGreg Roach        $tree  = Validator::attributes($request)->tree();
56bb5c0006SGreg Roach        $query = Validator::queryParams($request)->string('query');
5763763244SGreg Roach
5863763244SGreg Roach        $data = $this->search_service
5963763244SGreg Roach            ->searchPlaces($tree, $query, 0, static::LIMIT)
60*f25fc0f9SGreg Roach            ->map(static fn (Place $place): string => $place->gedcomName());
6163763244SGreg Roach
62c9c6f2ecSGreg Roach        // No place found? Use external gazetteers.
63c9c6f2ecSGreg Roach        foreach ($this->module_service->findByInterface(ModuleMapAutocompleteInterface::class) as $module) {
64c9c6f2ecSGreg Roach            if ($data->isEmpty()) {
65c9c6f2ecSGreg Roach                $data = $data->concat($module->searchPlaceNames($query))->sort();
6663763244SGreg Roach            }
6763763244SGreg Roach        }
6863763244SGreg Roach
69c9c6f2ecSGreg Roach        return $data;
7063763244SGreg Roach    }
7163763244SGreg Roach}
72