fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Http\RequestHandlers; |
||
| 21 | |||
| 22 | use Fisharebest\Webtrees\Tree; |
||
| 23 | use Fisharebest\Webtrees\Validator; |
||
| 24 | use Illuminate\Support\Collection; |
||
| 25 | use Psr\Http\Message\ResponseInterface; |
||
| 26 | use Psr\Http\Message\ServerRequestInterface; |
||
| 27 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 28 | |||
| 29 | use function response; |
||
| 30 | |||
| 31 | abstract class AbstractTomSelectHandler implements RequestHandlerInterface |
||
| 32 | { |
||
| 33 | // For clients that request one page of data at a time. |
||
| 34 | private const int RESULTS_PER_PAGE = 50; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | |||
| 36 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 37 | { |
||
| 38 | $tree = Validator::attributes($request)->tree(); |
||
| 39 | $at = Validator::queryParams($request)->isInArray(['', '@'])->string('at'); |
||
| 40 | $page = Validator::queryParams($request)->integer('page', 1); |
||
| 41 | $query = Validator::queryParams($request)->string('query'); |
||
| 42 | |||
| 43 | // Fetch one more row than we need, so we can know if more rows exist. |
||
| 44 | $offset = ($page - 1) * self::RESULTS_PER_PAGE; |
||
| 45 | $limit = self::RESULTS_PER_PAGE + 1; |
||
| 46 | |||
| 47 | // Perform the search. |
||
| 48 | if ($query !== '') { |
||
| 49 | $results = $this->search($tree, $query, $offset, $limit, $at); |
||
| 50 | } else { |
||
| 51 | $results = new Collection(); |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($results->count() > self::RESULTS_PER_PAGE) { |
||
| 55 | $next_url = route(static::class, ['tree' => $tree->name(), 'at' => $at, 'page' => $page + 1]); |
||
| 56 | } else { |
||
| 57 | $next_url = null; |
||
| 58 | } |
||
| 59 | |||
| 60 | return response([ |
||
| 61 | 'data' => $results->slice(0, self::RESULTS_PER_PAGE)->all(), |
||
| 62 | 'nextUrl' => $next_url, |
||
| 63 | ]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Perform the search |
||
| 68 | * |
||
| 69 | * @param Tree $tree |
||
| 70 | * @param string $query |
||
| 71 | * @param int $offset |
||
| 72 | * @param int $limit |
||
| 73 | * @param string $at "@" or "" |
||
| 74 | * |
||
| 75 | * @return Collection<int,array{text:string,value:string}> |
||
| 76 | */ |
||
| 77 | abstract protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection; |
||
| 78 | } |
||
| 79 |