-
Notifications
You must be signed in to change notification settings - Fork 3
/
TomSelectSourceWithSuggestions.php
55 lines (46 loc) · 1.5 KB
/
TomSelectSourceWithSuggestions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Cissee\Webtrees\Module\ResearchSuggestions;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Http\RequestHandlers\AbstractTomSelectHandler;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;
use function app;
use function view;
/**
* Autocomplete for sources.
*/
class TomSelectSourceWithSuggestions extends AbstractTomSelectHandler
{
/**
* Perform the search
*
* @param Tree $tree
* @param string $query
* @param int $offset
* @param int $limit
* @param string $at
*
* @return Collection<int,array{text:string,value:string}>
*/
protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection
{
//return suggested sources, if there are any
// Create a dummy record
$dummy = Registry::individualFactory()->new(
'xref',
"0 @xref@ INDI\n1 DEAT Y\n",
null,
$tree);
$fact = new Fact($query, $dummy, '');
$sourceEvents = app(ResearchSuggestionsService::class)->getSourceSuggestions($fact, $tree);
$results = $sourceEvents->map(static function (SourceEvent $source) use ($at): array {
return [
'text' => view('selects/source', ['source' => $source->getSource()]),
'value' => $at . $source->getSourceXref() . $at,
];
});
return $results;
}
}