66
77use Icinga \Module \Notifications \Common \Auth ;
88use Icinga \Module \Notifications \Common \Database ;
9+ use Icinga \Module \Notifications \Common \Links ;
910use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
1011use Icinga \Module \Notifications \Forms \EventRuleForm ;
12+ use Icinga \Module \Notifications \Model \Incident ;
1113use Icinga \Module \Notifications \Model \Rule ;
1214use Icinga \Module \Notifications \Web \Control \SearchBar \ExtraTagSuggestions ;
1315use Icinga \Module \Notifications \Web \Form \EventRuleDecorator ;
16+ use Icinga \Web \Notification ;
1417use Icinga \Web \Session ;
1518use ipl \Html \Attributes ;
1619use ipl \Html \Form ;
@@ -52,90 +55,92 @@ public function indexAction(): void
5255
5356 $ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
5457 $ config = $ this ->sessionNamespace ->get ($ ruleId );
55-
58+ $ discardChangesButton = null ;
5659 if ($ config === null ) {
5760 $ config = $ this ->fromDb ($ ruleId );
58- $ this ->sessionNamespace ->set ($ ruleId , $ config );
5961 }
6062
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+
66112
113+ $ buttonsWrapper = new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]]));
67114 $ eventRuleConfigSubmitButton = (new SubmitButtonElement (
68115 'save ' ,
69116 [
70117 'label ' => t ('Save ' ),
71118 'form ' => 'event-rule-config-form '
72119 ]
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+ ));
85121 $ deleteButton = (new SubmitButtonElement (
86122 'delete ' ,
87123 [
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 ' ,
91127 'formnovalidate ' => true
92128 ]
93- ))
94- ->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
129+ ));
95130
96- // $eventRuleDecorator = new EventRuleDecorator();
97- // $eventRuleDecorator->decorate($ eventRuleConfigSubmitButton);
98- // $eventRuleDecorator->decorate($discardChangesButton );
131+ $ buttonsWrapper -> add (
132+ [ $ eventRuleConfigSubmitButton, $ discardChangesButton , $ deleteButton ]
133+ );
99134
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 ));
137139
138- $ eventRuleConfig ->handleRequest ($ this ->getServerRequest ());
140+ if ($ incidents ->count () > 0 ) {
141+ $ deleteButton ->addAttributes (['disabled ' => true ]);
142+ }
143+ }
139144
140145 $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
141146 Html::tag ('h2 ' , $ config ['name ' ] ?? '' ),
@@ -148,10 +153,8 @@ public function indexAction(): void
148153 ))->openInModal ()
149154 ]);
150155 $ this ->addControl ($ eventRuleForm );
151- $ this ->addControl ($ eventRuleConfigSubmitButton );
152- $ this ->addControl ($ discardChangesButton );
153- $ this ->addControl ($ deleteButton );
154156
157+ $ this ->addControl ($ buttonsWrapper );
155158 $ this ->addContent ($ eventRuleConfig );
156159 }
157160
@@ -284,19 +287,10 @@ public function editAction(): void
284287 ->on (Form::ON_SUCCESS , function ($ form ) use ($ ruleId , $ config ) {
285288 $ config ['name ' ] = $ form ->getValue ('name ' );
286289 $ config ['is_active ' ] = $ form ->getValue ('is_active ' );
287-
288- $ db = Database::get ();
289290 $ params = [];
290291 if ($ ruleId === '-1 ' ) {
291292 $ params = $ config ;
292293 } 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-
300294 $ params ['id ' ] = $ ruleId ;
301295 }
302296
@@ -307,6 +301,7 @@ public function editAction(): void
307301 $ this ->sendExtraUpdates (['#col1 ' ]);
308302 }
309303
304+ $ this ->sessionNamespace ->set ($ ruleId , $ config );
310305 $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
311306 $ this ->redirectNow ($ redirectUrl );
312307 })->handleRequest ($ this ->getServerRequest ());
0 commit comments