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\Registry; 23c8d78f19SGreg Roachuse Fisharebest\Webtrees\Repository; 24c8d78f19SGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 25c8d78f19SGreg Roachuse Fisharebest\Webtrees\Tree; 26c8d78f19SGreg Roachuse Illuminate\Support\Collection; 27c8d78f19SGreg Roach 28110d87e5SGreg Roachuse function array_filter; 29110d87e5SGreg Roachuse function explode; 30c8d78f19SGreg Roachuse function view; 31c8d78f19SGreg Roach 32c8d78f19SGreg Roach/** 33c8d78f19SGreg Roach * Autocomplete for repositories. 34c8d78f19SGreg Roach */ 35c8d78f19SGreg Roachclass TomSelectRepository 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( 43c8d78f19SGreg Roach SearchService $search_service 44c8d78f19SGreg Roach ) { 45c8d78f19SGreg Roach $this->search_service = $search_service; 46c8d78f19SGreg Roach } 47c8d78f19SGreg Roach 48c8d78f19SGreg Roach /** 49c8d78f19SGreg Roach * Perform the search 50c8d78f19SGreg Roach * 51c8d78f19SGreg Roach * @param Tree $tree 52c8d78f19SGreg Roach * @param string $query 53c8d78f19SGreg Roach * @param int $offset 54c8d78f19SGreg Roach * @param int $limit 55c8d78f19SGreg Roach * @param string $at 56c8d78f19SGreg Roach * 57110d87e5SGreg Roach * @return Collection<int,array{text:string,value:string}> 58c8d78f19SGreg Roach */ 59c8d78f19SGreg Roach protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection 60c8d78f19SGreg Roach { 61c8d78f19SGreg Roach // Search by XREF 62c8d78f19SGreg Roach $repository = Registry::repositoryFactory()->make($query, $tree); 63c8d78f19SGreg Roach 64c8d78f19SGreg Roach if ($repository instanceof Repository) { 65c8d78f19SGreg Roach $results = new Collection([$repository]); 66c8d78f19SGreg Roach } else { 67110d87e5SGreg Roach $search = array_filter(explode(' ', $query)); 68110d87e5SGreg Roach $results = $this->search_service->searchRepositories([$tree], $search, $offset, $limit); 69c8d78f19SGreg Roach } 70c8d78f19SGreg Roach 71*f25fc0f9SGreg Roach return $results->map(static fn (Repository $repository): array => [ 72c8d78f19SGreg Roach 'text' => view('selects/repository', ['repository' => $repository]), 73c8d78f19SGreg Roach 'value' => $at . $repository->xref() . $at, 74*f25fc0f9SGreg Roach ]); 75c8d78f19SGreg Roach } 76c8d78f19SGreg Roach} 77