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