7
7
use Icinga \Module \Notifications \Common \Auth ;
8
8
use Icinga \Module \Notifications \Common \Database ;
9
9
use Icinga \Module \Notifications \Common \Links ;
10
+ use Icinga \Module \Notifications \Forms \EventRuleConfigElements \EventRuleConfigFilter ;
10
11
use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
11
12
use Icinga \Module \Notifications \Forms \EventRuleForm ;
12
13
use Icinga \Module \Notifications \Model \Incident ;
13
14
use Icinga \Module \Notifications \Model \Rule ;
14
15
use Icinga \Module \Notifications \Web \Control \SearchBar \ExtraTagSuggestions ;
15
16
use Icinga \Web \Notification ;
16
- use Icinga \Web \Session ;
17
17
use ipl \Html \Attributes ;
18
18
use ipl \Html \Form ;
19
19
use ipl \Html \FormElement \SubmitButtonElement ;
@@ -31,95 +31,62 @@ class EventRuleController extends CompatController
31
31
{
32
32
use Auth;
33
33
34
- /** @var Session\SessionNamespace */
35
- private $ sessionNamespace ;
36
-
37
34
/** @var ?string Event rule config filter */
38
35
protected $ filter ;
39
36
40
- public function init ()
41
- {
42
- $ this ->sessionNamespace = Session::getSession ()->getNamespace ('notifications ' );
43
- }
44
-
45
37
public function indexAction (): void
46
38
{
47
39
$ this ->assertPermission ('notifications/config/event-rule ' );
48
- $ this ->sessionNamespace -> delete ( ' -1 ' );
40
+ $ this ->addContent (Html:: tag ( ' div ' , [ ' class ' => ' container ' , ' id ' => ' dummy ' ]) );
49
41
50
42
$ this ->addTitleTab (t ('Event Rule ' ));
51
43
$ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
52
44
53
45
$ ruleId = $ this ->params ->getRequired ('id ' );
54
- $ configValues = $ this ->sessionNamespace ->get ($ ruleId );
55
46
$ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
56
47
57
- $ disableSave = false ;
58
- if ($ configValues === null ) {
59
- $ configValues = $ this ->fromDb ((int ) $ ruleId );
60
- $ disableSave = true ;
48
+ $ eventRuleConfigValues = $ this ->fromDb ((int ) $ ruleId );
49
+
50
+ if ($ this ->getRequest ()->isPost ()) {
51
+ if ($ this ->getRequest ()->has ('searchbar ' )) {
52
+ $ eventRuleConfigValues ['object_filter ' ] = $ this ->getRequest ()->get ('searchbar ' );
53
+ } else {
54
+ $ eventRuleConfigValues ['object_filter ' ] = '' ;
55
+ }
61
56
}
62
57
63
58
$ eventRuleConfig = (new EventRuleConfigForm (
64
- $ configValues ,
65
59
Url::fromPath (
66
60
'notifications/event-rule/search-editor ' ,
67
- ['id ' => $ ruleId ]
61
+ ['id ' => $ ruleId, ' object_filter ' => $ eventRuleConfigValues [ ' object_filter ' ] ]
68
62
)
69
- ))->populate ($ configValues );
70
- $ eventRuleConfig
71
- ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
72
- $ form ->addOrUpdateRule ($ ruleId , $ configValues );
73
- $ this ->sessionNamespace ->delete ($ ruleId );
74
- Notification::success ((sprintf (t ('Successfully saved event rule %s ' ), $ configValues ['name ' ])));
63
+ ))
64
+ ->populate ($ eventRuleConfigValues )
65
+ ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ ruleId , $ eventRuleConfigValues ) {
66
+ $ config = $ form ->getValues ();
67
+ $ config ['object_filter ' ] = $ eventRuleConfigValues ['object_filter ' ];
68
+ $ form ->addOrUpdateRule ($ ruleId , $ config );
69
+ Notification::success ((sprintf (t ('Successfully saved event rule %s ' ), $ eventRuleConfigValues ['name ' ])));
75
70
$ this ->redirectNow (Links::eventRule ((int ) $ ruleId ));
76
71
})
77
- ->on (EventRuleConfigForm::ON_DELETE , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
78
- $ form ->removeRule ((int ) $ ruleId );
79
- $ this ->sessionNamespace ->delete ($ ruleId );
80
- Notification::success (sprintf (t ('Successfully deleted event rule %s ' ), $ configValues ['name ' ]));
81
- $ this ->redirectNow ('__CLOSE__ ' );
82
- })
83
- ->on (EventRuleConfigForm::ON_DISCARD , function () use ($ ruleId , $ configValues ) {
84
- $ this ->sessionNamespace ->delete ($ ruleId );
85
- Notification::success (
86
- sprintf (
87
- t ('Successfully discarded changes to event rule %s ' ),
88
- $ configValues ['name ' ]
89
- )
90
- );
91
- $ this ->redirectNow (Links::eventRule ((int ) $ ruleId ));
92
- })
93
- ->on (EventRuleConfigForm::ON_CHANGE , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
94
- $ configValues = array_merge ($ configValues , $ form ->getValues ());
95
- $ configValues ['rule_escalation ' ] = $ form ->getValues ()['rule_escalation ' ];
96
- $ this ->sessionNamespace ->set ($ ruleId , $ configValues );
97
- })
72
+ ->on (
73
+ EventRuleConfigForm::ON_DELETE ,
74
+ function (EventRuleConfigForm $ form ) use ($ ruleId , $ eventRuleConfigValues ) {
75
+ $ form ->removeRule ((int ) $ ruleId );
76
+ Notification::success (
77
+ sprintf (t ('Successfully deleted event rule %s ' ), $ eventRuleConfigValues ['name ' ])
78
+ );
79
+ $ this ->redirectNow ('__CLOSE__ ' );
80
+ }
81
+ )
98
82
->handleRequest ($ this ->getServerRequest ());
99
83
100
- $ cache = $ this ->sessionNamespace ->get ($ ruleId );
101
- $ discardChangesButton = null ;
102
- if ($ cache !== null ) {
103
- $ this ->addContent (Html::tag ('div ' , ['class ' => 'cache-notice ' ], t ('There are unsaved changes. ' )));
104
- $ discardChangesButton = (new SubmitButtonElement (
105
- 'discard_changes ' ,
106
- [
107
- 'label ' => t ('Discard Changes ' ),
108
- 'form ' => 'event-rule-config-form ' ,
109
- 'class ' => 'btn-discard-changes ' ,
110
- 'formnovalidate ' => true ,
111
- ]
112
- ));
113
- $ disableSave = false ;
114
- }
115
-
116
84
$ buttonsWrapper = new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]]));
117
85
$ eventRuleConfigSubmitButton = (new SubmitButtonElement (
118
86
'save ' ,
119
87
[
120
88
'label ' => t ('Save ' ),
121
89
'form ' => 'event-rule-config-form ' ,
122
- 'disabled ' => $ disableSave
123
90
]
124
91
));
125
92
$ deleteButton = (new SubmitButtonElement (
@@ -133,7 +100,7 @@ public function indexAction(): void
133
100
));
134
101
135
102
$ buttonsWrapper ->add (
136
- [$ eventRuleConfigSubmitButton , $ discardChangesButton , $ deleteButton ]
103
+ [$ eventRuleConfigSubmitButton , $ deleteButton ]
137
104
);
138
105
139
106
if ($ ruleId > 0 ) {
@@ -146,8 +113,8 @@ public function indexAction(): void
146
113
}
147
114
}
148
115
149
- $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
150
- Html::tag ('h2 ' , $ configValues ['name ' ] ?? '' ),
116
+ $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' , ' id ' => ' event-rule-form ' ], [
117
+ Html::tag ('h2 ' , $ eventRuleConfigValues ['name ' ] ?? '' ),
151
118
(new Link (
152
119
new Icon ('edit ' ),
153
120
Url::fromPath ('notifications/event-rule/edit ' , [
@@ -162,6 +129,49 @@ public function indexAction(): void
162
129
$ this ->addContent ($ eventRuleConfig );
163
130
}
164
131
132
+ public function ruleAction (): void
133
+ {
134
+ /** @var int $ruleId */
135
+ $ ruleId = $ this ->params ->getRequired ('id ' );
136
+ $ query = Rule::on (Database::get ())
137
+ ->withoutColumns ('timeperiod_id ' )
138
+ ->filter (Filter::equal ('id ' , $ ruleId ));
139
+
140
+ $ rule = $ query ->first ();
141
+
142
+ $ this ->getDocument ()->add ([
143
+ Html::tag ('h2 ' , $ rule ->name ?? '' ),
144
+ (new Link (
145
+ new Icon ('edit ' ),
146
+ Url::fromPath ('notifications/event-rule/edit ' , [
147
+ 'id ' => $ ruleId
148
+ ]),
149
+ ['class ' => 'control-button ' ]
150
+ ))->openInModal ()
151
+ ]);
152
+ }
153
+
154
+ public function configFilterAction (): void
155
+ {
156
+ $ ruleId = $ this ->params ->getRequired ('id ' );
157
+ /** @var string $objectFilter */
158
+ $ objectFilter = $ this ->params ->get ('object_filter ' , '' );
159
+ $ eventRuleFilterFieldset = (new EventRuleConfigFilter ('config-filter ' ))
160
+ ->setObjectFilter ($ objectFilter )
161
+ ->setSearchEditorUrl (
162
+ Url::fromPath (
163
+ 'notifications/event-rule/search-editor ' ,
164
+ ['id ' => $ ruleId , 'object_filter ' => $ objectFilter ]
165
+ )
166
+ );
167
+
168
+ if ($ objectFilter === '' ) {
169
+ $ eventRuleFilterFieldset ->getAttributes ()->add ('class ' , 'empty-filter ' );
170
+ }
171
+
172
+ $ this ->getDocument ()->add ($ eventRuleFilterFieldset );
173
+ }
174
+
165
175
/**
166
176
* Create config from db
167
177
*
@@ -232,37 +242,35 @@ public function searchEditorAction(): void
232
242
{
233
243
/** @var string $ruleId */
234
244
$ ruleId = $ this ->params ->shiftRequired ('id ' );
235
-
236
- $ eventRule = $ this ->sessionNamespace ->get ($ ruleId );
237
-
238
- if ($ eventRule === null ) {
239
- $ eventRule = $ this ->fromDb ((int ) $ ruleId );
240
- }
241
-
242
245
$ editor = new SearchEditor ();
243
246
244
- $ objectFilter = $ eventRule ['object_filter ' ] ?? '' ;
247
+ /** @var string $objectFilter */
248
+ $ objectFilter = $ this ->params ->shift ('object_filter ' , '' );
245
249
$ editor ->setQueryString ($ objectFilter );
246
250
$ editor ->setAction (Url::fromRequest ()->getAbsoluteUrl ());
247
251
$ editor ->setSuggestionUrl (
248
252
Url::fromPath (
249
253
"notifications/event-rule/complete " ,
250
- ['_disableLayout ' => true , 'showCompact ' => true , 'id ' => Url:: fromRequest ()-> getParams ()-> get ( ' id ' ) ]
254
+ ['_disableLayout ' => true , 'showCompact ' => true , 'id ' => $ ruleId ]
251
255
)
252
256
);
253
257
254
- $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) use ($ ruleId, $ eventRule ) {
258
+ $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) use ($ ruleId ) {
255
259
$ filter = self ::createFilterString ($ form ->getFilter ());
256
- $ eventRule [ ' object_filter ' ] = $ filter ;
257
- $ this -> sessionNamespace -> set ( $ ruleId , $ eventRule );
258
- $ this -> getResponse ()
259
- -> setHeader ( ' X-Icinga-Container ' , ' _self ' )
260
- -> redirectAndExit (
261
- Url:: fromPath (
262
- ' notifications/event-rule ' ,
263
- [ ' id ' => $ ruleId ]
260
+ $ this -> sendExtraUpdates (
261
+ [
262
+ ' #config-filter ' => Url:: fromPath (
263
+ ' notifications/event-rule/config-filter ' ,
264
+ [
265
+ ' id ' => $ ruleId ,
266
+ ' object_filter ' => $ filter
267
+ ]
264
268
)
265
- );
269
+ ]
270
+ );
271
+ $ this ->getResponse ()
272
+ ->setHeader ('X-Icinga-Container ' , 'dummy ' )
273
+ ->redirectAndExit ('__CLOSE__ ' );
266
274
});
267
275
268
276
$ editor ->handleRequest ($ this ->getServerRequest ());
@@ -297,13 +305,11 @@ public function editAction(): void
297
305
{
298
306
/** @var string $ruleId */
299
307
$ ruleId = $ this ->params ->getRequired ('id ' );
300
- $ config = $ this ->sessionNamespace ->get ($ ruleId );
301
- if ($ config === null ) {
302
- if ($ ruleId === '-1 ' ) {
303
- $ config = ['id ' => $ ruleId ];
304
- } else {
305
- $ config = $ this ->fromDb ((int ) $ ruleId );
306
- }
308
+ if ($ ruleId === '-1 ' ) {
309
+ $ config = ['id ' => $ ruleId ];
310
+ } else {
311
+ /** @var array<string, mixed> $config */
312
+ $ config = $ this ->fromDb ((int ) $ ruleId );
307
313
}
308
314
309
315
$ eventRuleForm = (new EventRuleForm ())
@@ -325,14 +331,23 @@ public function editAction(): void
325
331
326
332
if ($ ruleId === '-1 ' ) {
327
333
$ redirectUrl = Url::fromPath ('notifications/event-rules/add ' , $ params );
334
+ $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
335
+ $ this ->redirectNow ($ redirectUrl );
328
336
} else {
329
- $ redirectUrl = Url::fromPath ('notifications/event-rule ' , $ params );
330
- $ this ->sendExtraUpdates (['#col1 ' ]);
337
+ Database::get ()->update ('rule ' , [
338
+ 'name ' => $ config ['name ' ],
339
+ 'is_active ' => $ config ['is_active ' ] ?? 'n '
340
+ ], ['id = ? ' => $ ruleId ]);
341
+ $ this ->sendExtraUpdates ([
342
+ '#event-rule-form ' => Url::fromPath (
343
+ 'notifications/event-rule/rule ' ,
344
+ ['id ' => $ ruleId ]
345
+ )->getAbsoluteUrl ()
346
+ ]);
347
+
348
+ $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'dummy ' )
349
+ ->redirectAndExit ('__CLOSE__ ' );
331
350
}
332
-
333
- $ this ->sessionNamespace ->set ($ ruleId , $ config );
334
- $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
335
- $ this ->redirectNow ($ redirectUrl );
336
351
})->handleRequest ($ this ->getServerRequest ());
337
352
338
353
if ($ ruleId === '-1 ' ) {
0 commit comments