xref: /webtrees/app/Http/RequestHandlers/TomSelectSubmission.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\Registry;
23*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
24*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Submission;
25*c8d78f19SGreg Roachuse Fisharebest\Webtrees\Tree;
26*c8d78f19SGreg Roachuse Illuminate\Support\Collection;
27*c8d78f19SGreg Roach
28*c8d78f19SGreg Roachuse function view;
29*c8d78f19SGreg Roach
30*c8d78f19SGreg Roach/**
31*c8d78f19SGreg Roach * Autocomplete for submissions.
32*c8d78f19SGreg Roach */
33*c8d78f19SGreg Roachclass TomSelectSubmission extends AbstractTomSelectHandler
34*c8d78f19SGreg Roach{
35*c8d78f19SGreg Roach    protected SearchService $search_service;
36*c8d78f19SGreg Roach
37*c8d78f19SGreg Roach    /**
38*c8d78f19SGreg Roach     * TomSelectSubmission constructor.
39*c8d78f19SGreg Roach     *
40*c8d78f19SGreg Roach     * @param SearchService $search_service
41*c8d78f19SGreg Roach     */
42*c8d78f19SGreg Roach    public function __construct(
43*c8d78f19SGreg Roach        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        $submission = Registry::submissionFactory()->make($query, $tree);
63*c8d78f19SGreg Roach
64*c8d78f19SGreg Roach        if ($submission instanceof Submission) {
65*c8d78f19SGreg Roach            $results = new Collection([$submission]);
66*c8d78f19SGreg Roach        } else {
67*c8d78f19SGreg Roach            $results = $this->search_service->searchSubmissions([$tree], [$query], $offset, $limit);
68*c8d78f19SGreg Roach        }
69*c8d78f19SGreg Roach
70*c8d78f19SGreg Roach        return $results->map(static function (Submission $submission) use ($at): array {
71*c8d78f19SGreg Roach            return [
72*c8d78f19SGreg Roach                'text'  => view('selects/submission', ['submission' => $submission]),
73*c8d78f19SGreg Roach                'value' => $at . $submission->xref() . $at,
74*c8d78f19SGreg Roach            ];
75*c8d78f19SGreg Roach        });
76*c8d78f19SGreg Roach    }
77*c8d78f19SGreg Roach}
78