6
6
7
7
use Icinga \Module \Notifications \Common \Auth ;
8
8
use Icinga \Module \Notifications \Common \Database ;
9
+ use Icinga \Module \Notifications \Common \Links ;
9
10
use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
10
11
use Icinga \Module \Notifications \Forms \EventRuleForm ;
12
+ use Icinga \Module \Notifications \Model \Incident ;
11
13
use Icinga \Module \Notifications \Model \Rule ;
12
14
use Icinga \Module \Notifications \Web \Control \SearchBar \ExtraTagSuggestions ;
13
15
use Icinga \Module \Notifications \Web \Form \EventRuleDecorator ;
16
+ use Icinga \Web \Notification ;
14
17
use Icinga \Web \Session ;
15
18
use ipl \Html \Attributes ;
16
19
use ipl \Html \Form ;
@@ -52,90 +55,92 @@ public function indexAction(): void
52
55
53
56
$ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
54
57
$ config = $ this ->sessionNamespace ->get ($ ruleId );
55
-
58
+ $ discardChangesButton = null ;
56
59
if ($ config === null ) {
57
60
$ config = $ this ->fromDb ($ ruleId );
58
- $ this ->sessionNamespace ->set ($ ruleId , $ config );
59
61
}
60
62
61
- // $config = $this->fromDb($ruleId);
62
- //
63
- // if ($filter === '' && isset($config['object_filter'])) {
64
- // $filter = $config['object_filter'];
65
- // }
63
+ $ eventRuleConfig = (new EventRuleConfigForm (
64
+ $ config ,
65
+ Url::fromPath (
66
+ 'notifications/event-rule/search-editor ' ,
67
+ ['id ' => $ config ['id ' ]]
68
+ )
69
+ ))->populate ($ config );
70
+ $ eventRuleConfig
71
+ ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ config ) {
72
+ $ form ->insertOrAddRule ($ config ['id ' ], $ config );
73
+ $ this ->sessionNamespace ->delete ($ config ['id ' ]);
74
+ Notification::success ((sprintf (t ('Successfully saved event rule %s ' ), $ config ['name ' ])));
75
+
76
+ $ this ->sendExtraUpdates (['#col1 ' ]);
77
+ $ this ->redirectNow (Links::eventRule ($ config ['id ' ]));
78
+ })
79
+ ->on (EventRuleConfigForm::ON_DELETE , function (EventRuleConfigForm $ form ) use ($ config ) {
80
+ $ ruleId = $ config ['id ' ];
81
+ $ form ->removeRule ($ ruleId );
82
+ $ this ->sessionNamespace ->delete ($ ruleId );
83
+ Notification::success (sprintf (t ('Successfully deleted event rule %s ' ), $ config ['name ' ]));
84
+ $ this ->redirectNow ('__CLOSE__ ' );
85
+ })
86
+ ->on (EventRuleConfigForm::ON_DISCARD , function () use ($ config ) {
87
+ $ ruleId = $ config ['id ' ];
88
+ $ this ->sessionNamespace ->delete ($ ruleId );
89
+ $ this ->redirectNow (Links::eventRule ($ ruleId ));
90
+ })
91
+ ->on (EventRuleConfigForm::ON_CHANGE , function (EventRuleConfigForm $ form ) use ($ config ) {
92
+ $ formValues = $ form ->getValues ();
93
+ $ config = array_merge ($ config , $ form ->getValues ());
94
+ $ this ->sessionNamespace ->set ($ config ['id ' ], $ config );
95
+ })
96
+ ->handleRequest ($ this ->getServerRequest ());
97
+
98
+ $ cache = $ this ->sessionNamespace ->get ($ ruleId );
99
+ if ($ cache !== null ) {
100
+ $ this ->addContent (Html::tag ('div ' , ['class ' => 'cache-notice ' ], t ('There are unsaved changes. ' )));
101
+ $ discardChangesButton = (new SubmitButtonElement (
102
+ 'discard_changes ' ,
103
+ [
104
+ 'label ' => t ('Discard Changes ' ),
105
+ 'form ' => 'event-rule-config-form ' ,
106
+ 'class ' => 'btn-discard-changes ' ,
107
+ 'formnovalidate ' => true ,
108
+ ]
109
+ ));
110
+ }
111
+
66
112
113
+ $ buttonsWrapper = new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]]));
67
114
$ eventRuleConfigSubmitButton = (new SubmitButtonElement (
68
115
'save ' ,
69
116
[
70
117
'label ' => t ('Save ' ),
71
118
'form ' => 'event-rule-config-form '
72
119
]
73
- ))->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
74
-
75
- $ discardChangesButton = (new SubmitButtonElement (
76
- 'discard_changes ' ,
77
- [
78
- 'label ' => t ('Discard Changes ' ),
79
- 'form ' => 'event-rule-config-form ' ,
80
- 'formnovalidate ' => true ,
81
- ]
82
- ))
83
- ->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
84
-
120
+ ));
85
121
$ deleteButton = (new SubmitButtonElement (
86
122
'delete ' ,
87
123
[
88
- 'label ' => t ('Delete ' ),
89
- 'form ' => 'event-rule-config-form ' ,
90
- 'class ' => 'btn-remove ' ,
124
+ 'label ' => t ('Delete ' ),
125
+ 'form ' => 'event-rule-config-form ' ,
126
+ 'class ' => 'btn-remove ' ,
91
127
'formnovalidate ' => true
92
128
]
93
- ))
94
- ->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
129
+ ));
95
130
96
- // $eventRuleDecorator = new EventRuleDecorator();
97
- // $eventRuleDecorator->decorate($ eventRuleConfigSubmitButton);
98
- // $eventRuleDecorator->decorate($discardChangesButton );
131
+ $ buttonsWrapper -> add (
132
+ [ $ eventRuleConfigSubmitButton, $ discardChangesButton , $ deleteButton ]
133
+ );
99
134
100
- $ eventRuleConfig = (new EventRuleConfigForm (
101
- $ config ['object_filter ' ] ?? '' ,
102
- Url::fromPath ('notifications/event-rule/search-editor ' , ['id ' => $ ruleId ])
103
- ))
104
- ->registerElement ($ eventRuleConfigSubmitButton )
105
- ->registerElement ($ discardChangesButton )
106
- ->registerElement ($ deleteButton );
107
-
108
- $ eventRuleConfig ->populate ($ config );
109
- $ eventRuleConfig
110
- ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ deleteButton , $ eventRuleConfigSubmitButton , $ config ) {
111
- $ pressedButton = $ form ->getPressedSubmitElement ();
112
- if ($ pressedButton ) {
113
- var_dump ($ pressedButton ->getName ());die;
114
- if ($ pressedButton ->getName () === 'delete ' ) {
115
- $ this ->sessionNamespace ->delete ($ config ['id ' ]);
116
- } else {
117
- $ db = Database::get ();
118
- $ ruleId = (int ) $ config ['id ' ];
119
- if ($ ruleId !== -1 ) {
120
- // var_dump($config);die;
121
- $ db ->update ('rule ' , [
122
- 'name ' => $ config ['name ' ],
123
- 'timeperiod_id ' => $ config ['timeperiod_id ' ] ?? null ,
124
- 'object_filter ' => $ config ['object_filter ' ] ?? null ,
125
- 'is_active ' => $ config ['is_active ' ] ?? 'n '
126
- ], ['id = ? ' => $ ruleId ]);
127
-
128
- $ form ->insertOrAddRule ($ ruleId , $ config );
129
- }
130
- }
131
- }
132
- })
133
- ->on (Form::ON_SENT , function (Form $ form ) use ($ config , $ ruleId ) {
134
- $ config = array_merge ($ config , $ form ->getValues ());
135
- $ this ->sessionNamespace ->set ($ ruleId , $ config );
136
- });
135
+ if ($ ruleId > 0 ) {
136
+ $ incidents = Incident::on (Database::get ())
137
+ ->with ('rule ' )
138
+ ->filter (Filter::equal ('rule.id ' , $ ruleId ));
137
139
138
- $ eventRuleConfig ->handleRequest ($ this ->getServerRequest ());
140
+ if ($ incidents ->count () > 0 ) {
141
+ $ deleteButton ->addAttributes (['disabled ' => true ]);
142
+ }
143
+ }
139
144
140
145
$ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
141
146
Html::tag ('h2 ' , $ config ['name ' ] ?? '' ),
@@ -148,10 +153,8 @@ public function indexAction(): void
148
153
))->openInModal ()
149
154
]);
150
155
$ this ->addControl ($ eventRuleForm );
151
- $ this ->addControl ($ eventRuleConfigSubmitButton );
152
- $ this ->addControl ($ discardChangesButton );
153
- $ this ->addControl ($ deleteButton );
154
156
157
+ $ this ->addControl ($ buttonsWrapper );
155
158
$ this ->addContent ($ eventRuleConfig );
156
159
}
157
160
@@ -284,19 +287,10 @@ public function editAction(): void
284
287
->on (Form::ON_SUCCESS , function ($ form ) use ($ ruleId , $ config ) {
285
288
$ config ['name ' ] = $ form ->getValue ('name ' );
286
289
$ config ['is_active ' ] = $ form ->getValue ('is_active ' );
287
-
288
- $ db = Database::get ();
289
290
$ params = [];
290
291
if ($ ruleId === '-1 ' ) {
291
292
$ params = $ config ;
292
293
} else {
293
- $ db ->update ('rule ' , [
294
- 'name ' => $ config ['name ' ],
295
- 'timeperiod_id ' => $ config ['timeperiod_id ' ] ?? null ,
296
- 'object_filter ' => $ config ['object_filter ' ] ?? null ,
297
- 'is_active ' => $ config ['is_active ' ] ?? 'n '
298
- ], ['id = ? ' => $ ruleId ]);
299
-
300
294
$ params ['id ' ] = $ ruleId ;
301
295
}
302
296
@@ -307,6 +301,7 @@ public function editAction(): void
307
301
$ this ->sendExtraUpdates (['#col1 ' ]);
308
302
}
309
303
304
+ $ this ->sessionNamespace ->set ($ ruleId , $ config );
310
305
$ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
311
306
$ this ->redirectNow ($ redirectUrl );
312
307
})->handleRequest ($ this ->getServerRequest ());
0 commit comments