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