1c8d78f19SGreg Roach<?php 2c8d78f19SGreg Roach 3c8d78f19SGreg Roach/** 4c8d78f19SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6c8d78f19SGreg Roach * This program is free software: you can redistribute it and/or modify 7c8d78f19SGreg Roach * it under the terms of the GNU General Public License as published by 8c8d78f19SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9c8d78f19SGreg Roach * (at your option) any later version. 10c8d78f19SGreg Roach * This program is distributed in the hope that it will be useful, 11c8d78f19SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12c8d78f19SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13c8d78f19SGreg Roach * GNU General Public License for more details. 14c8d78f19SGreg Roach * You should have received a copy of the GNU General Public License 15c8d78f19SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16c8d78f19SGreg Roach */ 17c8d78f19SGreg Roach 18c8d78f19SGreg Roachdeclare(strict_types=1); 19c8d78f19SGreg Roach 20c8d78f19SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21c8d78f19SGreg Roach 22c8d78f19SGreg Roachuse Fisharebest\Webtrees\Individual; 23c8d78f19SGreg Roachuse Fisharebest\Webtrees\Registry; 24c8d78f19SGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 25c8d78f19SGreg Roachuse Fisharebest\Webtrees\Tree; 26c8d78f19SGreg Roachuse Illuminate\Support\Collection; 27c8d78f19SGreg Roach 28110d87e5SGreg Roachuse function array_filter; 29c8d78f19SGreg Roachuse function explode; 30c8d78f19SGreg Roachuse function view; 31c8d78f19SGreg Roach 32c8d78f19SGreg Roach/** 33c8d78f19SGreg Roach * Autocomplete for individuals. 34c8d78f19SGreg Roach */ 35c8d78f19SGreg Roachclass TomSelectIndividual extends AbstractTomSelectHandler 36c8d78f19SGreg Roach{ 37c8d78f19SGreg Roach protected SearchService $search_service; 38c8d78f19SGreg Roach 39c8d78f19SGreg Roach /** 40c8d78f19SGreg Roach * @param SearchService $search_service 41c8d78f19SGreg Roach */ 42c8d78f19SGreg Roach public function __construct(SearchService $search_service) 43c8d78f19SGreg Roach { 44c8d78f19SGreg Roach $this->search_service = $search_service; 45c8d78f19SGreg Roach } 46c8d78f19SGreg Roach 47c8d78f19SGreg Roach /** 48c8d78f19SGreg Roach * Perform the search 49c8d78f19SGreg Roach * 50c8d78f19SGreg Roach * @param Tree $tree 51c8d78f19SGreg Roach * @param string $query 52c8d78f19SGreg Roach * @param int $offset 53c8d78f19SGreg Roach * @param int $limit 54c8d78f19SGreg Roach * @param string $at 55c8d78f19SGreg Roach * 56110d87e5SGreg Roach * @return Collection<int,array{text:string,value:string}> 57c8d78f19SGreg Roach */ 58c8d78f19SGreg Roach protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection 59c8d78f19SGreg Roach { 60c8d78f19SGreg Roach // Search by XREF 61c8d78f19SGreg Roach $individual = Registry::individualFactory()->make($query, $tree); 62c8d78f19SGreg Roach 63c8d78f19SGreg Roach if ($individual instanceof Individual) { 64c8d78f19SGreg Roach $results = new Collection([$individual]); 65c8d78f19SGreg Roach } else { 66110d87e5SGreg Roach $search = array_filter(explode(' ', $query)); 67110d87e5SGreg Roach $results = $this->search_service->searchIndividualNames([$tree], $search, $offset, $limit); 68c8d78f19SGreg Roach } 69c8d78f19SGreg Roach 70*f25fc0f9SGreg Roach return $results->map(static fn (Individual $individual): array => [ 71c8d78f19SGreg Roach 'text' => view('selects/individual', ['individual' => $individual]), 72c8d78f19SGreg Roach 'value' => $at . $individual->xref() . $at, 73*f25fc0f9SGreg Roach ]); 74c8d78f19SGreg Roach } 75c8d78f19SGreg Roach} 76