4
4
5
5
namespace Icinga \Module \Notifications \Controllers ;
6
6
7
- use Icinga \Exception \ProgrammingError ;
8
7
use Icinga \Module \Notifications \Common \Database ;
9
- use Icinga \Module \Notifications \Common \Links ;
10
- use Icinga \Module \Notifications \Forms \EventRuleForm ;
11
- use Icinga \Module \Notifications \Forms \SaveEventRuleForm ;
12
- use Icinga \Module \Notifications \Model \ObjectExtraTag ;
8
+ use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
13
9
use Icinga \Module \Notifications \Model \Rule ;
14
10
use Icinga \Module \Notifications \Web \Control \SearchBar \ObjectSuggestions ;
15
11
use Icinga \Module \Notifications \Widget \EventRuleConfig ;
16
12
use Icinga \Module \Notifications \Widget \ItemList \EventRuleList ;
17
13
use Icinga \Web \Notification ;
18
14
use Icinga \Web \Session ;
15
+ use ipl \Html \Attributes ;
19
16
use ipl \Html \Form ;
17
+ use ipl \Html \FormElement \SubmitButtonElement ;
20
18
use ipl \Html \Html ;
19
+ use ipl \Html \HtmlElement ;
21
20
use ipl \Stdlib \Filter ;
22
21
use ipl \Web \Compat \CompatController ;
23
22
use ipl \Web \Compat \SearchControls ;
24
- use ipl \Web \Control \LimitControl ;
25
23
use ipl \Web \Control \SearchEditor ;
26
- use ipl \Web \Control \SortControl ;
27
24
use ipl \Web \Filter \QueryString ;
28
25
use ipl \Web \Url ;
29
26
use ipl \Web \Widget \ButtonLink ;
@@ -88,7 +85,7 @@ public function indexAction(): void
88
85
$ this ->addContent (
89
86
(new ButtonLink (
90
87
t ('New Event Rule ' ),
91
- Url::fromPath ('notifications/event-rule/edit ' , ['id ' => -1 , ' clearCache ' => true ]),
88
+ Url::fromPath ('notifications/event-rule/edit ' , ['id ' => -1 ]),
92
89
'plus '
93
90
))->openInModal ()
94
91
->addAttributes (['class ' => 'new-event-rule ' ])
@@ -110,19 +107,56 @@ public function addAction(): void
110
107
$ this ->getTabs ()->setRefreshUrl (Url::fromPath ('notifications/event-rules/add ' ));
111
108
112
109
$ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
110
+ /** @var string $ruleId */
111
+ $ ruleId = $ this ->params ->getRequired ('id ' );
113
112
114
- if ($ this ->params ->has ('use_cache ' ) || $ this ->getServerRequest ()->getMethod () !== 'GET ' ) {
115
- $ cache = $ this ->sessionNamespace ->get (-1 , []);
116
- } else {
117
- $ this ->sessionNamespace ->delete (-1 );
113
+ $ params = $ this ->params ->toArray (false );
114
+ /** @var array<string, mixed>|null $config */
115
+ $ config = $ this ->sessionNamespace ->get ($ ruleId );
118
116
119
- $ cache = [];
117
+ if ($ config === null ) {
118
+ /** @var array<string, mixed> $config */
119
+ $ config = $ params ;
120
120
}
121
121
122
- $ eventRuleConfig = new EventRuleConfig (Url::fromPath ('notifications/event-rules/add-search-editor ' ), $ cache );
122
+ $ eventRuleConfigSubmitButton = (new SubmitButtonElement (
123
+ 'save ' ,
124
+ [
125
+ 'label ' => t ('Add Event Rule ' ),
126
+ 'form ' => 'event-rule-config-form ' ,
127
+ 'formnovalidate ' => true
128
+ ]
129
+ ))->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
130
+
131
+ $ eventRuleConfig = (new EventRuleConfigForm (
132
+ $ config ,
133
+ Url::fromPath (
134
+ 'notifications/event-rules/search-editor ' ,
135
+ ['id ' => $ ruleId ]
136
+ )
137
+ ))
138
+ ->registerElement ($ eventRuleConfigSubmitButton )
139
+ ->populate ($ config );
140
+
141
+ $ eventRuleConfig
142
+ ->on (Form::ON_SENT , function (Form $ form ) use ($ config ) {
143
+ $ config = array_merge ($ config , $ form ->getValues ());
144
+ $ this ->sessionNamespace ->set ('-1 ' , $ config );
145
+ })
146
+ ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ config ) {
147
+ /** @var string $ruleId */
148
+ $ ruleId = $ config ['id ' ];
149
+ /** @var string $ruleName */
150
+ $ ruleName = $ config ['name ' ];
151
+ $ form ->addOrUpdateRule ($ ruleId , $ config );
152
+ $ this ->sessionNamespace ->delete ($ ruleId );
153
+ Notification::success (sprintf (t ('Successfully add event rule %s ' ), $ ruleName ));
154
+ $ this ->redirectNow ('__CLOSE__ ' );
155
+ })
156
+ ->handleRequest ($ this ->getServerRequest ());
123
157
124
158
$ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
125
- Html::tag ('h2 ' , $ eventRuleConfig -> getConfig () ['name ' ] ?? '' ),
159
+ Html::tag ('h2 ' , $ config ['name ' ] ?? '' ),
126
160
(new Link (
127
161
new Icon ('edit ' ),
128
162
Url::fromPath ('notifications/event-rule/edit ' , [
@@ -132,42 +166,8 @@ public function addAction(): void
132
166
))->openInModal ()
133
167
]);
134
168
135
- $ saveForm = (new SaveEventRuleForm ())
136
- ->on (SaveEventRuleForm::ON_SUCCESS , function ($ saveForm ) use ($ eventRuleConfig ) {
137
- if (! $ eventRuleConfig ->isValid ()) {
138
- $ eventRuleConfig ->addAttributes (['class ' => 'invalid ' ]);
139
- return ;
140
- }
141
-
142
- $ id = $ saveForm ->addRule ($ this ->sessionNamespace ->get (-1 ));
143
-
144
- Notification::success ($ this ->translate ('Successfully added rule. ' ));
145
- $ this ->sendExtraUpdates (['#col1 ' ]);
146
- $ this ->redirectNow (Links::eventRule ($ id ));
147
- })->handleRequest ($ this ->getServerRequest ());
148
-
149
- $ eventRuleConfig ->on (EventRuleConfig::ON_CHANGE , function ($ eventRuleConfig ) {
150
- $ this ->sessionNamespace ->set (-1 , $ eventRuleConfig ->getConfig ());
151
-
152
- $ this ->redirectNow (Url::fromPath ('notifications/event-rules/add ' , ['use_cache ' => true ]));
153
- });
154
-
155
- foreach ($ eventRuleConfig ->getForms () as $ f ) {
156
- $ f ->handleRequest ($ this ->getServerRequest ());
157
-
158
- if (! $ f ->hasBeenSent ()) {
159
- // Force validation of populated values in case we display an unsaved rule
160
- $ f ->validatePartial ();
161
- }
162
- }
163
-
164
- $ eventRuleFormAndSave = Html::tag ('div ' , ['class ' => 'event-rule-and-save-forms ' ]);
165
- $ eventRuleFormAndSave ->add ([
166
- $ eventRuleForm ,
167
- $ saveForm
168
- ]);
169
-
170
- $ this ->addControl ($ eventRuleFormAndSave );
169
+ $ this ->addControl ($ eventRuleForm );
170
+ $ this ->addControl ($ eventRuleConfigSubmitButton );
171
171
$ this ->addContent ($ eventRuleConfig );
172
172
}
173
173
@@ -181,47 +181,70 @@ public function completeAction(): void
181
181
182
182
public function searchEditorAction (): void
183
183
{
184
- $ editor = $ this ->createSearchEditor (
185
- Rule::on (Database::get ()),
186
- [
187
- LimitControl::DEFAULT_LIMIT_PARAM ,
188
- SortControl::DEFAULT_SORT_PARAM ,
189
- ]
190
- );
184
+ /** @var string $ruleId */
185
+ $ ruleId = $ this ->params ->shiftRequired ('id ' );
191
186
192
- $ this ->getDocument ()->add ($ editor );
193
- $ this ->setTitle ($ this ->translate ('Adjust Filter ' ));
194
- }
187
+ /** @var array<string, mixed>|null $eventRule */
188
+ $ eventRule = $ this ->sessionNamespace ->get ($ ruleId );
195
189
196
- public function addSearchEditorAction (): void
197
- {
198
- $ cache = $ this -> sessionNamespace -> get (- 1 );
190
+ if ( $ eventRule === null ) {
191
+ $ eventRule = [ ' id ' => ' -1 ' ];
192
+ }
199
193
200
- $ editor = EventRuleConfig::createSearchEditor ()
201
- ->setQueryString ($ cache ['object_filter ' ] ?? '' );
194
+ $ editor = new SearchEditor ();
202
195
203
- $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) {
204
- $ cache = $ this ->sessionNamespace ->get (-1 );
205
- $ cache ['object_filter ' ] = EventRuleConfig::createFilterString ($ form ->getFilter ());
196
+ /** @var string $objectFilter */
197
+ $ objectFilter = $ eventRule ['object_filter ' ] ?? '' ;
198
+ $ editor ->setQueryString ($ objectFilter );
199
+ $ editor ->setAction (Url::fromRequest ()->getAbsoluteUrl ());
200
+ $ editor ->setSuggestionUrl (Url::fromPath (
201
+ "notifications/event-rule/complete " ,
202
+ ['_disableLayout ' => true , 'showCompact ' => true , 'id ' => Url::fromRequest ()->getParams ()->get ('id ' )]
203
+ ));
206
204
207
- $ this ->sessionNamespace ->set (-1 , $ cache );
205
+ $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) use ($ ruleId , $ eventRule ) {
206
+ $ filter = self ::createFilterString ($ form ->getFilter ());
207
+ $ eventRule ['object_filter ' ] = $ filter ;
208
208
209
+ $ this ->sessionNamespace ->set ($ ruleId , $ eventRule );
209
210
$ this ->getResponse ()
210
211
->setHeader ('X-Icinga-Container ' , '_self ' )
211
212
->redirectAndExit (
212
213
Url::fromPath (
213
214
'notifications/event-rules/add ' ,
214
- ['use_cache ' => true ]
215
+ ['id ' => $ ruleId ]
215
216
)
216
217
);
217
218
});
218
219
219
220
$ editor ->handleRequest ($ this ->getServerRequest ());
220
221
221
- $ this ->getDocument ()->addHtml ($ editor );
222
+ $ this ->getDocument ()->add ($ editor );
222
223
$ this ->setTitle ($ this ->translate ('Adjust Filter ' ));
223
224
}
224
225
226
+ /**
227
+ * Create filter string from the given filter rule
228
+ *
229
+ * @param Filter\Rule $filters
230
+ *
231
+ * @return string
232
+ */
233
+ public static function createFilterString (Filter \Rule $ filters ): string
234
+ {
235
+ if ($ filters instanceof Filter \Chain) {
236
+ foreach ($ filters as $ filter ) {
237
+ self ::createFilterString ($ filter );
238
+ }
239
+ } elseif ($ filters instanceof Filter \Condition && empty ($ filters ->getValue ())) {
240
+ $ filters ->setValue (true );
241
+ }
242
+
243
+ $ filterStr = QueryString::render ($ filters );
244
+
245
+ return ! empty ($ filterStr ) ? $ filterStr : '' ;
246
+ }
247
+
225
248
/**
226
249
* Get the filter created from query string parameters
227
250
*
0 commit comments