xref: /webtrees/app/Http/RequestHandlers/TomSelectIndividual.php (revision c8d78f19d0bdf4c0ec4728253bd37250d2e6cec4)
1*c8d78f19SGreg Roach<?php
2*c8d78f19SGreg Roach
3*c8d78f19SGreg Roach/**
4*c8d78f19SGreg Roach * webtrees: online genealogy
5*c8d78f19SGreg Roach * Copyright (C) 2021 webtrees development team
6*c8d78f19SGreg Roach * This program is free software: you can redistribute it and/or modify
7*c8d78f19SGreg Roach * it under the terms of the GNU General Public License as published by
8*c8d78f19SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*c8d78f19SGreg Roach * (at your option) any later version.
10*c8d78f19SGreg Roach * This program is distributed in the hope that it will be useful,
11*c8d78f19SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*c8d78f19SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*c8d78f19SGreg Roach * GNU General Public License for more details.
14*c8d78f19SGreg Roach * You should have received a copy of the GNU General Public License
15*c8d78f19SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*c8d78f19SGreg Roach */
17*c8d78f19SGreg Roach
18*c8d78f19SGreg Roachdeclare(strict_types=1);
19*c8d78f19SGreg Roach
20*c8d78f19SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*c8d78f19SGreg Roach
22*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Individual;
23*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Registry;
24*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
25*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Tree;
26*c8d78f19SGreg Roachuse Illuminate\Support\Collection;
27*c8d78f19SGreg Roach
28*c8d78f19SGreg Roachuse function explode;
29*c8d78f19SGreg Roachuse function view;
30*c8d78f19SGreg Roach
31*c8d78f19SGreg Roach/**
32*c8d78f19SGreg Roach * Autocomplete for individuals.
33*c8d78f19SGreg Roach */
34*c8d78f19SGreg Roachclass TomSelectIndividual extends AbstractTomSelectHandler
35*c8d78f19SGreg Roach{
36*c8d78f19SGreg Roach    protected SearchService $search_service;
37*c8d78f19SGreg Roach
38*c8d78f19SGreg Roach    /**
39*c8d78f19SGreg Roach     * TomSelectIndividual constructor.
40*c8d78f19SGreg Roach     *
41*c8d78f19SGreg Roach     * @param SearchService $search_service
42*c8d78f19SGreg Roach     */
43*c8d78f19SGreg Roach    public function __construct(SearchService $search_service)
44*c8d78f19SGreg Roach    {
45*c8d78f19SGreg Roach        $this->search_service = $search_service;
46*c8d78f19SGreg Roach    }
47*c8d78f19SGreg Roach
48*c8d78f19SGreg Roach    /**
49*c8d78f19SGreg Roach     * Perform the search
50*c8d78f19SGreg Roach     *
51*c8d78f19SGreg Roach     * @param Tree   $tree
52*c8d78f19SGreg Roach     * @param string $query
53*c8d78f19SGreg Roach     * @param int    $offset
54*c8d78f19SGreg Roach     * @param int    $limit
55*c8d78f19SGreg Roach     * @param string $at
56*c8d78f19SGreg Roach     *
57*c8d78f19SGreg Roach     * @return Collection<array<string,string>>
58*c8d78f19SGreg Roach     */
59*c8d78f19SGreg Roach    protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection
60*c8d78f19SGreg Roach    {
61*c8d78f19SGreg Roach        // Search by XREF
62*c8d78f19SGreg Roach        $individual = Registry::individualFactory()->make($query, $tree);
63*c8d78f19SGreg Roach
64*c8d78f19SGreg Roach        if ($individual instanceof Individual) {
65*c8d78f19SGreg Roach            $results = new Collection([$individual]);
66*c8d78f19SGreg Roach        } else {
67*c8d78f19SGreg Roach            $results = $this->search_service->searchIndividualNames([$tree], explode(' ', $query), $offset, $limit);
68*c8d78f19SGreg Roach        }
69*c8d78f19SGreg Roach
70*c8d78f19SGreg Roach        return $results->map(static function (Individual $individual) use ($at): array {
71*c8d78f19SGreg Roach            return [
72*c8d78f19SGreg Roach                'text'  => view('selects/individual', ['individual' => $individual]),
73*c8d78f19SGreg Roach                'value' => $at . $individual->xref() . $at,
74*c8d78f19SGreg Roach            ];
75*c8d78f19SGreg Roach        });
76*c8d78f19SGreg Roach    }
77*c8d78f19SGreg Roach}
78