2323use ipl \Web \Control \SearchEditor ;
2424use ipl \Web \Filter \QueryString ;
2525use ipl \Web \Url ;
26+ use ipl \Web \Widget \EmptyStateBar ;
2627use ipl \Web \Widget \Icon ;
2728use ipl \Web \Widget \Link ;
2829
@@ -31,7 +32,7 @@ class EventRuleController extends CompatController
3132 use Auth;
3233
3334 /** @var Session\SessionNamespace */
34- private $ sessionNamespace ;
35+ private Session \ SessionNamespace $ sessionNamespace ;
3536
3637 public function init ()
3738 {
@@ -41,47 +42,50 @@ public function init()
4142
4243 public function indexAction (): void
4344 {
44- $ this ->addTitleTab (t ('Event Rule ' ));
45- $ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
45+ $ this ->addTitleTab ($ this -> translate ('Event Rule ' ));
46+ $ this ->content ->addAttributes (['class ' => 'event-rule-detail ' ]);
4647
4748 $ ruleId = (int ) $ this ->params ->getRequired ('id ' );
48- $ configValues = $ this ->sessionNamespace ->get ($ ruleId );
49- $ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
49+ $ config = $ this ->sessionNamespace ->get ($ ruleId );
5050
51- $ disableSave = false ;
52- if ($ configValues === null ) {
53- $ configValues = $ this ->fromDb ($ ruleId );
54- $ disableSave = true ;
51+ $ fromCache = true ;
52+ if ($ config === null ) {
53+ $ config = $ this ->fromDb ($ ruleId );
54+ $ fromCache = false ;
5555 }
5656
5757 $ eventRuleConfig = new EventRuleConfigForm (
58- $ configValues ,
58+ $ config ,
5959 Url::fromPath ('notifications/event-rule/search-editor ' , ['id ' => $ ruleId ])
6060 );
6161
6262 $ eventRuleConfig
63- ->populate ($ configValues )
64- ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
65- $ insertId = $ form ->addOrUpdateRule ($ ruleId , $ configValues );
63+ ->populate ($ config )
64+ ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ ruleId , $ config ) {
65+ $ insertId = $ form ->addOrUpdateRule ($ ruleId , $ config );
6666 $ this ->sessionNamespace ->delete ($ ruleId );
67- Notification::success ((sprintf (t ('Successfully saved event rule %s ' ), $ configValues ['name ' ])));
67+ Notification::success (sprintf (
68+ $ this ->translate ('Successfully saved event rule %s ' ),
69+ $ config ['name ' ]
70+ ));
6871 $ this ->sendExtraUpdates (['#col1 ' ]);
6972 $ this ->redirectNow (Links::eventRule ($ insertId ));
7073 })
71- ->on (EventRuleConfigForm::ON_SENT , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
74+ ->on (EventRuleConfigForm::ON_SENT , function (EventRuleConfigForm $ form ) use ($ ruleId , $ config ) {
7275 if ($ form ->hasBeenRemoved ()) {
7376 $ form ->removeRule ($ ruleId );
7477 $ this ->sessionNamespace ->delete ($ ruleId );
75- Notification::success (sprintf (t ('Successfully deleted event rule %s ' ), $ configValues ['name ' ]));
78+ Notification::success (sprintf (
79+ $ this ->translate ('Successfully deleted event rule %s ' ),
80+ $ config ['name ' ]
81+ ));
7682 $ this ->redirectNow (Links::eventRules ());
7783 } elseif ($ form ->hasBeenDiscarded ()) {
7884 $ this ->sessionNamespace ->delete ($ ruleId );
79- Notification::success (
80- sprintf (
81- t ('Successfully discarded changes to event rule %s ' ),
82- $ configValues ['name ' ]
83- )
84- );
85+ Notification::success (sprintf (
86+ $ this ->translate ('Successfully discarded changes to event rule %s ' ),
87+ $ config ['name ' ]
88+ ));
8589
8690 if ($ ruleId === -1 ) {
8791 $ this ->switchToSingleColumnLayout ();
@@ -90,38 +94,35 @@ public function indexAction(): void
9094 }
9195 }
9296 })
93- ->on (EventRuleConfigForm::ON_CHANGE , function (EventRuleConfigForm $ form ) use ($ ruleId , $ configValues ) {
97+ ->on (EventRuleConfigForm::ON_CHANGE , function (EventRuleConfigForm $ form ) use ($ ruleId , $ config ) {
9498 $ formValues = $ form ->getValues ();
95- $ configValues = array_merge ($ configValues , $ formValues );
96- $ configValues ['rule_escalation ' ] = $ formValues ['rule_escalation ' ];
97- $ this ->sessionNamespace ->set ($ ruleId , $ configValues );
99+ $ config = array_merge ($ config , $ formValues );
100+ $ config ['rule_escalation ' ] = $ formValues ['rule_escalation ' ];
101+ $ this ->sessionNamespace ->set ($ ruleId , $ config );
98102 })
99103 ->handleRequest ($ this ->getServerRequest ());
100104
101- $ cache = $ this ->sessionNamespace ->get ($ ruleId );
102105 $ discardChangesButton = null ;
103- if ($ cache !== null ) {
104- $ this ->addContent (Html:: tag ( ' div ' , [ ' class ' => ' cache-notice ' ], t ('There are unsaved changes. ' )));
106+ if ($ fromCache ) {
107+ $ this ->addContent (new EmptyStateBar ( $ this -> translate ('There are unsaved changes. ' )));
105108 $ discardChangesButton = new SubmitButtonElement (
106109 'discard_changes ' ,
107110 [
108- 'label ' => t ('Discard Changes ' ),
111+ 'label ' => $ this -> translate ('Discard Changes ' ),
109112 'form ' => 'event-rule-config-form ' ,
110113 'class ' => 'btn-discard-changes ' ,
111- 'formnovalidate ' => true ,
114+ 'formnovalidate ' => true
112115 ]
113116 );
114-
115- $ disableSave = false ;
116117 }
117118
118119 $ buttonsWrapper = new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]]));
119120 $ eventRuleConfigSubmitButton = new SubmitButtonElement (
120121 'save ' ,
121122 [
122- 'label ' => t ('Save ' ),
123+ 'label ' => $ this -> translate ('Save ' ),
123124 'form ' => 'event-rule-config-form ' ,
124- 'disabled ' => $ disableSave
125+ 'disabled ' => $ fromCache
125126 ]
126127 );
127128
@@ -130,7 +131,7 @@ public function indexAction(): void
130131 $ deleteButton = new SubmitButtonElement (
131132 'delete ' ,
132133 [
133- 'label ' => t ('Delete ' ),
134+ 'label ' => $ this -> translate ('Delete ' ),
134135 'form ' => 'event-rule-config-form ' ,
135136 'class ' => 'btn-remove ' ,
136137 'formnovalidate ' => true
@@ -141,7 +142,7 @@ public function indexAction(): void
141142 $ buttonsWrapper ->add ([$ eventRuleConfigSubmitButton , $ discardChangesButton , $ deleteButton ]);
142143
143144 $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
144- Html::tag ('h2 ' , $ configValues ['name ' ]),
145+ Html::tag ('h2 ' , $ config ['name ' ]),
145146 (new Link (
146147 new Icon ('edit ' ),
147148 Url::fromPath ('notifications/event-rule/edit ' , [
@@ -171,38 +172,38 @@ public function fromDb(int $ruleId): array
171172
172173 $ rule = $ query ->first ();
173174 if ($ rule === null ) {
174- $ this ->httpNotFound (t ('Rule not found ' ));
175+ $ this ->httpNotFound ($ this -> translate ('Rule not found ' ));
175176 }
176177
177178 $ config = iterator_to_array ($ rule );
178179
179180 $ ruleEscalations = $ rule
180181 ->rule_escalation
181- ->withoutColumns (['changed_at ' , 'deleted ' ]);
182+ ->columns (['id ' , 'position ' , ' condition ' ]);
182183
183184 foreach ($ ruleEscalations as $ re ) {
184185 foreach ($ re as $ k => $ v ) {
185- if (in_array ( $ k , [ ' id ' , ' condition ' ]) ) {
186+ if ($ k !== ' position ' ) {
186187 $ config [$ re ->getTableName ()][$ re ->position ][$ k ] = (string ) $ v ;
187188 }
188189 }
189190
190191 $ escalationRecipients = $ re
191192 ->rule_escalation_recipient
192- ->withoutColumns (['changed_at ' , 'deleted ' ]);
193+ ->columns (['id ' , 'position ' , ' contact_id ' , ' contactgroup_id ' , ' schedule_id ' , ' channel_id ' ]);
193194
194195 foreach ($ escalationRecipients as $ recipient ) {
195- $ requiredValues = [];
196+ $ recipientData = [];
196197
197198 foreach ($ recipient as $ k => $ v ) {
198199 if ($ v !== null && in_array ($ k , ['contact_id ' , 'contactgroup_id ' , 'schedule_id ' ])) {
199- $ requiredValues [$ k ] = (string ) $ v ;
200+ $ recipientData [$ k ] = (string ) $ v ;
200201 } elseif (in_array ($ k , ['id ' , 'channel_id ' ])) {
201- $ requiredValues [$ k ] = $ v ? (string ) $ v : null ;
202+ $ recipientData [$ k ] = $ v ? (string ) $ v : null ;
202203 }
203204 }
204205
205- $ config [$ re ->getTableName ()][$ re ->position ]['recipients ' ][] = $ requiredValues ;
206+ $ config [$ re ->getTableName ()][$ re ->position ]['recipients ' ][] = $ recipientData ;
206207 }
207208 }
208209
@@ -230,19 +231,12 @@ public function completeAction(): void
230231 */
231232 public function searchEditorAction (): void
232233 {
233- /** @var string $ruleId */
234- $ ruleId = $ this ->params ->shiftRequired ('id ' );
235-
236- $ eventRule = $ this ->sessionNamespace ->get ($ ruleId );
237-
238- if ($ eventRule === null ) {
239- $ eventRule = $ this ->fromDb ((int ) $ ruleId );
240- }
234+ $ ruleId = (int ) $ this ->params ->shiftRequired ('id ' );
235+ $ config = $ this ->sessionNamespace ->get ($ ruleId ) ?? $ this ->fromDb ($ ruleId );
241236
242237 $ editor = new SearchEditor ();
243238
244- $ objectFilter = $ eventRule ['object_filter ' ] ?? '' ;
245- $ editor ->setQueryString ($ objectFilter )
239+ $ editor ->setQueryString ($ config ['object_filter ' ] ?? '' )
246240 ->setAction (Url::fromRequest ()->getAbsoluteUrl ())
247241 ->setSuggestionUrl (
248242 Url::fromPath ('notifications/event-rule/complete ' , [
@@ -252,17 +246,13 @@ public function searchEditorAction(): void
252246 ])
253247 );
254248
255- $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) use ($ ruleId , $ eventRule ) {
256- $ eventRule ['object_filter ' ] = self ::createFilterString ($ form ->getFilter ());
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 ]
264- )
265- );
249+ $ editor ->on (Form::ON_SUCCESS , function (SearchEditor $ form ) use ($ ruleId , $ config ) {
250+ $ config ['object_filter ' ] = $ this ->createFilterString ($ form ->getFilter ());
251+ $ this ->sessionNamespace ->set ($ ruleId , $ config );
252+ $ this ->closeModalAndRefreshRelatedView (Url::fromPath (
253+ 'notifications/event-rule ' ,
254+ ['id ' => $ ruleId ]
255+ ));
266256 });
267257
268258 $ editor ->handleRequest ($ this ->getServerRequest ());
@@ -278,7 +268,7 @@ public function searchEditorAction(): void
278268 *
279269 * @return ?string
280270 */
281- public static function createFilterString (Filter \Rule $ filters ): ?string
271+ private function createFilterString (Filter \Rule $ filters ): ?string
282272 {
283273 if ($ filters instanceof Filter \Chain) {
284274 foreach ($ filters as $ filter ) {
0 commit comments