|
6 | 6 | namespace Icinga\Module\Notifications\Web\Control\SearchEditor; |
7 | 7 |
|
8 | 8 | use Icinga\Module\Notifications\Hook\V2\SourceHook; |
9 | | -use ipl\Html\HtmlElement; |
10 | 9 | use ipl\Stdlib\Filter; |
11 | | -use ipl\Web\Control\SearchBar\Suggestions; |
12 | | -use Traversable; |
| 10 | +use ipl\Web\Control\SearchEditor; |
| 11 | +use ipl\Web\Filter\QueryString; |
| 12 | +use ipl\Web\FormElement\SearchSuggestions; |
| 13 | +use Psr\Http\Message\ServerRequestInterface; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * Suggestions for the SearchEditor used to modify rule-filters |
16 | 17 | */ |
17 | | -class RuleFilterSuggestions extends Suggestions |
| 18 | +class RuleFilterSuggestions extends SearchSuggestions |
18 | 19 | { |
19 | 20 | protected SourceHook $hook; |
20 | 21 |
|
| 22 | + protected ?string $type = null; |
| 23 | + |
| 24 | + protected ?string $column = null; |
| 25 | + |
| 26 | + protected Filter\Chain $searchFilter; |
| 27 | + |
21 | 28 | public function __construct(SourceHook $hook) |
22 | 29 | { |
23 | 30 | $this->hook = $hook; |
24 | | - } |
| 31 | + $this->searchFilter = Filter::all(); |
25 | 32 |
|
26 | | - /** |
27 | | - * These suggestions aren't used by any searchbar, so this function is never called |
28 | | - */ |
29 | | - protected function createQuickSearchFilter($searchTerm) |
30 | | - { |
31 | | - } |
| 33 | + parent::__construct( |
| 34 | + (function () { |
| 35 | + if ($this->type === 'column') { |
| 36 | + $provider = $this->hook->getColumnSuggestions($this->getSearchTerm() ?? ''); |
| 37 | + $this->setGroupingCallback(fn($x) => $x['group']); |
| 38 | + } else { |
| 39 | + $provider = $this->hook->getValueSuggestions( |
| 40 | + $this->column, |
| 41 | + $this->getSearchTerm() ?? '', |
| 42 | + $this->searchFilter |
| 43 | + ); |
| 44 | + } |
32 | 45 |
|
33 | | - protected function fetchValueSuggestions($column, $searchTerm, Filter\Chain $searchFilter): Traversable |
34 | | - { |
35 | | - yield from $this->hook->getValueSuggestions($column, $searchTerm, $searchFilter); |
| 46 | + yield from $provider; |
| 47 | + })() |
| 48 | + ); |
36 | 49 | } |
37 | 50 |
|
38 | | - protected function shouldShowRelationFor(string $column): bool |
| 51 | + public function forRequest(ServerRequestInterface $request): static |
39 | 52 | { |
40 | | - return $this->hook->shouldShowRelationFor($column); |
41 | | - } |
| 53 | + if ($request->getMethod() !== 'POST') { |
| 54 | + return $this; |
| 55 | + } |
42 | 56 |
|
43 | | - protected function fetchColumnSuggestions($searchTerm): Traversable |
44 | | - { |
45 | | - $currentGroup = null; |
46 | | - foreach ($this->hook->getColumnSuggestions($searchTerm) as $item) { |
47 | | - if (isset($item['group']) && $item['group'] !== $currentGroup) { |
48 | | - $currentGroup = $item['group']; |
49 | | - $this->addHtml(HtmlElement::create( |
50 | | - 'li', |
51 | | - ['class' => static::SUGGESTION_TITLE_CLASS], |
52 | | - $currentGroup |
53 | | - )); |
54 | | - } |
55 | | - |
56 | | - yield $item['search'] => $item['label']; |
| 57 | + $requestData = json_decode($request->getBody()->read(8192), true); |
| 58 | + if (empty($requestData)) { |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + $this->setSearchTerm($requestData['term']['label']); |
| 63 | + $this->setOriginalSearchValue($requestData['term']['search']); |
| 64 | + $this->setExcludeTerms($requestData['exclude'] ?? []); |
| 65 | + |
| 66 | + $this->type = $requestData['term']['type'] ?? null; |
| 67 | + |
| 68 | + $column = $requestData['column'] ?? null; |
| 69 | + if ($column !== null && $column !== SearchEditor::FAKE_COLUMN) { |
| 70 | + $this->column = $column; |
57 | 71 | } |
| 72 | + |
| 73 | + $searchFilter = QueryString::parse($requestData['searchFilter'] ?? ''); |
| 74 | + $this->searchFilter = $searchFilter instanceof Filter\Chain |
| 75 | + ? $searchFilter |
| 76 | + : Filter::all($searchFilter); |
| 77 | + |
| 78 | + return $this; |
58 | 79 | } |
59 | 80 | } |
0 commit comments