xref: /webtrees/app/Http/RequestHandlers/TomSelectSharedNote.php (revision d11be7027e34e3121be11cc025421873364403f9)
1701f5d18SGreg Roach<?php
2701f5d18SGreg Roach
3701f5d18SGreg Roach/**
4701f5d18SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6701f5d18SGreg Roach * This program is free software: you can redistribute it and/or modify
7701f5d18SGreg Roach * it under the terms of the GNU General Public License as published by
8701f5d18SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9701f5d18SGreg Roach * (at your option) any later version.
10701f5d18SGreg Roach * This program is distributed in the hope that it will be useful,
11701f5d18SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12701f5d18SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13701f5d18SGreg Roach * GNU General Public License for more details.
14701f5d18SGreg Roach * You should have received a copy of the GNU General Public License
15701f5d18SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16701f5d18SGreg Roach */
17701f5d18SGreg Roach
18701f5d18SGreg Roachdeclare(strict_types=1);
19701f5d18SGreg Roach
20701f5d18SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21701f5d18SGreg Roach
22701f5d18SGreg Roachuse Fisharebest\Webtrees\Registry;
23701f5d18SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
24701f5d18SGreg Roachuse Fisharebest\Webtrees\SharedNote;
25701f5d18SGreg Roachuse Fisharebest\Webtrees\Tree;
26701f5d18SGreg Roachuse Illuminate\Support\Collection;
27701f5d18SGreg Roach
28701f5d18SGreg Roachuse function array_filter;
29701f5d18SGreg Roachuse function explode;
30701f5d18SGreg Roachuse function view;
31701f5d18SGreg Roach
32701f5d18SGreg Roach/**
33701f5d18SGreg Roach * Autocomplete for notes.
34701f5d18SGreg Roach */
35701f5d18SGreg Roachclass TomSelectSharedNote extends AbstractTomSelectHandler
36701f5d18SGreg Roach{
37701f5d18SGreg Roach    protected SearchService $search_service;
38701f5d18SGreg Roach
39701f5d18SGreg Roach    /**
40701f5d18SGreg Roach     * TomSelectSharedNote constructor.
41701f5d18SGreg Roach     *
42701f5d18SGreg Roach     * @param SearchService $search_service
43701f5d18SGreg Roach     */
44701f5d18SGreg Roach    public function __construct(
45701f5d18SGreg Roach        SearchService $search_service
46701f5d18SGreg Roach    ) {
47701f5d18SGreg Roach        $this->search_service = $search_service;
48701f5d18SGreg Roach    }
49701f5d18SGreg Roach
50701f5d18SGreg Roach    /**
51701f5d18SGreg Roach     * Perform the search
52701f5d18SGreg Roach     *
53701f5d18SGreg Roach     * @param Tree   $tree
54701f5d18SGreg Roach     * @param string $query
55701f5d18SGreg Roach     * @param int    $offset
56701f5d18SGreg Roach     * @param int    $limit
57701f5d18SGreg Roach     * @param string $at
58701f5d18SGreg Roach     *
59701f5d18SGreg Roach     * @return Collection<int,array{text:string,value:string}>
60701f5d18SGreg Roach     */
61701f5d18SGreg Roach    protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection
62701f5d18SGreg Roach    {
63701f5d18SGreg Roach        // Search by XREF
64701f5d18SGreg Roach        $note = Registry::sharedNoteFactory()->make($query, $tree);
65701f5d18SGreg Roach
66701f5d18SGreg Roach        if ($note instanceof SharedNote) {
67701f5d18SGreg Roach            $results = new Collection([$note]);
68701f5d18SGreg Roach        } else {
69701f5d18SGreg Roach            $search  = array_filter(explode(' ', $query));
70701f5d18SGreg Roach            $results = $this->search_service->searchSharedNotes([$tree], $search, $offset, $limit);
71701f5d18SGreg Roach        }
72701f5d18SGreg Roach
73701f5d18SGreg Roach        return $results->map(static function (SharedNote $note) use ($at): array {
74701f5d18SGreg Roach            return [
75701f5d18SGreg Roach                'text'  => view('selects/shared-note', ['note' => $note]),
76701f5d18SGreg Roach                'value' => $at . $note->xref() . $at,
77701f5d18SGreg Roach            ];
78701f5d18SGreg Roach        });
79701f5d18SGreg Roach    }
80701f5d18SGreg Roach}
81