Skip to content

Commit 2e49f45

Browse files
Use SearchSuggestions when editing event rules
1 parent e2881f8 commit 2e49f45

3 files changed

Lines changed: 54 additions & 43 deletions

File tree

application/controllers/EventRuleController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
use Icinga\Module\Notifications\Hook\V2\SourceHook;
1919
use Icinga\Module\Notifications\Model\Rule;
2020
use Icinga\Module\Notifications\Model\Source;
21-
use Icinga\Module\Notifications\Web\Control\SearchEditor\RuleFilterSuggestions;
2221
use Icinga\Module\Notifications\Util\RuleSerializer;
2322
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
23+
use Icinga\Module\Notifications\Web\Control\SearchEditor\RuleFilterSuggestions;
2424
use Icinga\Web\Notification;
2525
use Icinga\Web\Session;
2626
use ipl\Html\Contract\Form;
@@ -254,8 +254,7 @@ function (Condition $condition) use ($hook) {
254254
public function suggestAction(): void
255255
{
256256
$hook = $this->resolveSourceHook((int) $this->params->getRequired('id'));
257-
$suggestions = new RuleFilterSuggestions($hook);
258-
$suggestions->forRequest($this->getServerRequest());
257+
$suggestions = (new RuleFilterSuggestions($hook))->forRequest($this->getServerRequest());
259258
$this->getDocument()->addHtml($suggestions);
260259
}
261260

library/Notifications/Hook/V2/SourceHook.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,4 @@ public function getValueSuggestions(string $column, string $searchTerm, Chain $s
7979
* @return Traversable Columns to be suggested as `search` => `label`
8080
*/
8181
public function getColumnSuggestions(string $searchTerm): Traversable;
82-
83-
/**
84-
* Get whether a relation label should be added to the suggestion
85-
*
86-
* @param string $column
87-
*
88-
* @return bool
89-
*/
90-
public function shouldShowRelationFor(string $column): bool;
9182
}

library/Notifications/Web/Control/SearchEditor/RuleFilterSuggestions.php

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,75 @@
66
namespace Icinga\Module\Notifications\Web\Control\SearchEditor;
77

88
use Icinga\Module\Notifications\Hook\V2\SourceHook;
9-
use ipl\Html\HtmlElement;
109
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;
1314

1415
/**
1516
* Suggestions for the SearchEditor used to modify rule-filters
1617
*/
17-
class RuleFilterSuggestions extends Suggestions
18+
class RuleFilterSuggestions extends SearchSuggestions
1819
{
1920
protected SourceHook $hook;
2021

22+
protected ?string $type = null;
23+
24+
protected ?string $column = null;
25+
26+
protected Filter\Chain $searchFilter;
27+
2128
public function __construct(SourceHook $hook)
2229
{
2330
$this->hook = $hook;
24-
}
31+
$this->searchFilter = Filter::all();
2532

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+
}
3245

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+
);
3649
}
3750

38-
protected function shouldShowRelationFor(string $column): bool
51+
public function forRequest(ServerRequestInterface $request): static
3952
{
40-
return $this->hook->shouldShowRelationFor($column);
41-
}
53+
if ($request->getMethod() !== 'POST') {
54+
return $this;
55+
}
4256

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;
5771
}
72+
73+
$searchFilter = QueryString::parse($requestData['searchFilter'] ?? '');
74+
$this->searchFilter = $searchFilter instanceof Filter\Chain
75+
? $searchFilter
76+
: Filter::all($searchFilter);
77+
78+
return $this;
5879
}
5980
}

0 commit comments

Comments
 (0)