|
2 | 2 |
|
3 | 3 | namespace ipl\Web\Control; |
4 | 4 |
|
| 5 | +use Icinga\Exception\ConfigurationError; |
5 | 6 | use ipl\Html\Attributes; |
6 | 7 | use ipl\Html\Form; |
7 | 8 | use ipl\Html\FormDecorator\CallbackDecorator; |
@@ -55,6 +56,9 @@ class SearchEditor extends Form |
55 | 56 | /** @var bool */ |
56 | 57 | protected $cleared = false; |
57 | 58 |
|
| 59 | + /** @var string[] Additional `metadata` fields for the condition */ |
| 60 | + protected array $metadataFields = []; |
| 61 | + |
58 | 62 | /** |
59 | 63 | * Set the filter query string to populate the form with |
60 | 64 | * |
@@ -95,6 +99,26 @@ public function setSuggestionUrl(Url $url) |
95 | 99 | return $this; |
96 | 100 | } |
97 | 101 |
|
| 102 | + /** |
| 103 | + * Set additional `metadata` fields for the condition |
| 104 | + * |
| 105 | + * The value of these fields is populated to condition's `metadata` |
| 106 | + * |
| 107 | + * @param array $fields |
| 108 | + * |
| 109 | + * @return $this |
| 110 | + */ |
| 111 | + public function setMetadataFields(array $fields): static |
| 112 | + { |
| 113 | + if (in_array('search', $fields, true)) { |
| 114 | + throw new ConfigurationError("'search' is a reserved keyword and cannot be used as a metadata field"); |
| 115 | + } |
| 116 | + |
| 117 | + $this->metadataFields = $fields; |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
98 | 122 | /** |
99 | 123 | * Get the query string parser being used |
100 | 124 | * |
@@ -195,6 +219,10 @@ protected function applyChanges(Filter\Rule $rule, array &$values, array $path = |
195 | 219 | } else { |
196 | 220 | // Make sure we don't forget to present the column labels again |
197 | 221 | $rule->metaData()->set('columnLabel', $this->popKey($values, $identifier . '-column')); |
| 222 | + |
| 223 | + foreach ($this->metadataFields as $fieldName) { |
| 224 | + $rule->metaData()->set($fieldName, $this->popKey($values, $identifier . '-column-' . $fieldName)); |
| 225 | + } |
198 | 226 | } |
199 | 227 |
|
200 | 228 | if ($newColumn !== null && $rule->getColumn() !== $newColumn) { |
@@ -551,6 +579,16 @@ protected function createCondition(Filter\Condition $condition, $identifier) |
551 | 579 | }] |
552 | 580 | ]); |
553 | 581 |
|
| 582 | + $metadataFields = new HtmlDocument(); |
| 583 | + foreach ($this->metadataFields as $fieldNameSuffix) { |
| 584 | + $columnMetaInput = $this->createElement('hidden', $identifier . '-column-' . $fieldNameSuffix, [ |
| 585 | + 'value' => $condition->metaData()->get($fieldNameSuffix) |
| 586 | + ]); |
| 587 | + $this->registerElement($columnMetaInput); |
| 588 | + |
| 589 | + $metadataFields->addHtml($columnMetaInput); |
| 590 | + } |
| 591 | + |
554 | 592 | $operatorInput = $this->createElement('select', $identifier . '-operator', [ |
555 | 593 | 'options' => [ |
556 | 594 | '~' => '~', |
@@ -588,6 +626,7 @@ protected function createCondition(Filter\Condition $condition, $identifier) |
588 | 626 | $columnInput, |
589 | 627 | $columnFakeInput, |
590 | 628 | $columnSearchInput, |
| 629 | + $metadataFields, |
591 | 630 | $operatorInput, |
592 | 631 | $valueInput |
593 | 632 | ); |
|
0 commit comments