xref: /webtrees/app/Http/RequestHandlers/AbstractTomSelectHandler.php (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
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 assert;
30use function response;
31use function strlen;
32
33/**
34 * Autocomplete for TomSelect based controls.
35 */
36abstract class AbstractTomSelectHandler implements RequestHandlerInterface
37{
38    // For clients that request one page of data at a time.
39    private const RESULTS_PER_PAGE = 50;
40
41    /**
42     * @param ServerRequestInterface $request
43     *
44     * @return ResponseInterface
45     */
46    public function handle(ServerRequestInterface $request): ResponseInterface
47    {
48        $tree = $request->getAttribute('tree');
49        assert($tree instanceof Tree);
50
51        $at    = Validator::queryParams($request)->requiredString('at');
52        $page  = Validator::queryParams($request)->integer('page') ?? 1;
53        $query = Validator::queryParams($request)->requiredString('query');
54
55        // Fetch one more row than we need, so we can know if more rows exist.
56        $offset = ($page - 1) * self::RESULTS_PER_PAGE;
57        $limit  = self::RESULTS_PER_PAGE + 1;
58
59        // Perform the search.
60        if ($query !== '') {
61            $results = $this->search($tree, $query, $offset, $limit, $at ? '@' : '');
62        } else {
63            $results = new Collection();
64        }
65
66        if ($results->count() > self::RESULTS_PER_PAGE) {
67            $next_url = route(static::class, ['tree' => $tree->name(), 'at' => $at ? '@' : '', 'page' => $page + 1]);
68        } else {
69            $next_url = null;
70        }
71
72        return response([
73            'data'    => $results->slice(0, self::RESULTS_PER_PAGE)->all(),
74            'nextUrl' => $next_url,
75        ]);
76    }
77
78    /**
79     * Perform the search
80     *
81     * @param Tree   $tree
82     * @param string $query
83     * @param int    $offset
84     * @param int    $limit
85     * @param string $at    "@" or ""
86     *
87     * @return Collection<array<string,string>>
88     */
89    abstract protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection;
90}
91