1c8d78f19SGreg Roach<?php 2c8d78f19SGreg Roach 3c8d78f19SGreg Roach/** 4c8d78f19SGreg Roach * webtrees: online genealogy 5*d11be702SGreg 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 * TomSelectIndividual constructor. 41c8d78f19SGreg Roach * 42c8d78f19SGreg Roach * @param SearchService $search_service 43c8d78f19SGreg Roach */ 44c8d78f19SGreg Roach public function __construct(SearchService $search_service) 45c8d78f19SGreg Roach { 46c8d78f19SGreg Roach $this->search_service = $search_service; 47c8d78f19SGreg Roach } 48c8d78f19SGreg Roach 49c8d78f19SGreg Roach /** 50c8d78f19SGreg Roach * Perform the search 51c8d78f19SGreg Roach * 52c8d78f19SGreg Roach * @param Tree $tree 53c8d78f19SGreg Roach * @param string $query 54c8d78f19SGreg Roach * @param int $offset 55c8d78f19SGreg Roach * @param int $limit 56c8d78f19SGreg Roach * @param string $at 57c8d78f19SGreg Roach * 58110d87e5SGreg Roach * @return Collection<int,array{text:string,value:string}> 59c8d78f19SGreg Roach */ 60c8d78f19SGreg Roach protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection 61c8d78f19SGreg Roach { 62c8d78f19SGreg Roach // Search by XREF 63c8d78f19SGreg Roach $individual = Registry::individualFactory()->make($query, $tree); 64c8d78f19SGreg Roach 65c8d78f19SGreg Roach if ($individual instanceof Individual) { 66c8d78f19SGreg Roach $results = new Collection([$individual]); 67c8d78f19SGreg Roach } else { 68110d87e5SGreg Roach $search = array_filter(explode(' ', $query)); 69110d87e5SGreg Roach $results = $this->search_service->searchIndividualNames([$tree], $search, $offset, $limit); 70c8d78f19SGreg Roach } 71c8d78f19SGreg Roach 72c8d78f19SGreg Roach return $results->map(static function (Individual $individual) use ($at): array { 73c8d78f19SGreg Roach return [ 74c8d78f19SGreg Roach 'text' => view('selects/individual', ['individual' => $individual]), 75c8d78f19SGreg Roach 'value' => $at . $individual->xref() . $at, 76c8d78f19SGreg Roach ]; 77c8d78f19SGreg Roach }); 78c8d78f19SGreg Roach } 79c8d78f19SGreg Roach} 80