1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Tree; 23use Fisharebest\Webtrees\Validator; 24use Illuminate\Support\Collection; 25use Psr\Http\Message\ResponseInterface; 26use Psr\Http\Message\ServerRequestInterface; 27use Psr\Http\Server\RequestHandlerInterface; 28 29use function response; 30 31/** 32 * Autocomplete for TomSelect based controls. 33 */ 34abstract class AbstractTomSelectHandler implements RequestHandlerInterface 35{ 36 // For clients that request one page of data at a time. 37 private const RESULTS_PER_PAGE = 50; 38 39 /** 40 * @param ServerRequestInterface $request 41 * 42 * @return ResponseInterface 43 */ 44 public function handle(ServerRequestInterface $request): ResponseInterface 45 { 46 $tree = Validator::attributes($request)->tree(); 47 48 $at = Validator::queryParams($request)->string('at'); 49 $page = Validator::queryParams($request)->optionalInteger('page') ?? 1; 50 $query = Validator::queryParams($request)->string('query'); 51 52 // Fetch one more row than we need, so we can know if more rows exist. 53 $offset = ($page - 1) * self::RESULTS_PER_PAGE; 54 $limit = self::RESULTS_PER_PAGE + 1; 55 56 // Perform the search. 57 if ($query !== '') { 58 $results = $this->search($tree, $query, $offset, $limit, $at ? '@' : ''); 59 } else { 60 $results = new Collection(); 61 } 62 63 if ($results->count() > self::RESULTS_PER_PAGE) { 64 $next_url = route(static::class, ['tree' => $tree->name(), 'at' => $at ? '@' : '', 'page' => $page + 1]); 65 } else { 66 $next_url = null; 67 } 68 69 return response([ 70 'data' => $results->slice(0, self::RESULTS_PER_PAGE)->all(), 71 'nextUrl' => $next_url, 72 ]); 73 } 74 75 /** 76 * Perform the search 77 * 78 * @param Tree $tree 79 * @param string $query 80 * @param int $offset 81 * @param int $limit 82 * @param string $at "@" or "" 83 * 84 * @return Collection<int,array<string,string>> 85 */ 86 abstract protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection; 87} 88