66
77use Icinga \Module \Notifications \Common \Database ;
88use Icinga \Module \Notifications \Common \Links ;
9- use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
9+ use Icinga \Module \Notifications \Forms \EventRuleForm ;
1010use Icinga \Module \Notifications \Model \Rule ;
1111use Icinga \Module \Notifications \View \EventRuleRenderer ;
1212use Icinga \Module \Notifications \Web \Control \SearchBar \ObjectSuggestions ;
1313use Icinga \Module \Notifications \Widget \ItemList \ObjectList ;
14- use Icinga \Web \Notification ;
1514use Icinga \Web \Session ;
16- use ipl \Html \Attributes ;
1715use ipl \Html \Form ;
18- use ipl \Html \FormElement \SubmitButtonElement ;
1916use ipl \Html \Html ;
20- use ipl \Html \HtmlElement ;
2117use ipl \Stdlib \Filter ;
2218use ipl \Web \Compat \CompatController ;
2319use ipl \Web \Compat \SearchControls ;
2723use ipl \Web \Layout \DetailedItemLayout ;
2824use ipl \Web \Url ;
2925use ipl \Web \Widget \ButtonLink ;
30- use ipl \Web \Widget \Icon ;
3126use ipl \Web \Widget \Link ;
3227
3328class EventRulesController extends CompatController
@@ -49,7 +44,6 @@ public function init()
4944 public function indexAction (): void
5045 {
5146 $ eventRules = Rule::on (Database::get ());
52- $ this ->sessionNamespace ->delete ('-1 ' );
5347
5448 $ limitControl = $ this ->createLimitControl ();
5549 $ paginationControl = $ this ->createPaginationControl ($ eventRules );
@@ -87,14 +81,24 @@ public function indexAction(): void
8781 $ this ->addControl ($ sortControl );
8882 $ this ->addControl ($ limitControl );
8983 $ this ->addControl ($ searchBar );
90- $ this ->addContent (
84+
85+ $ addButton =
9186 (new ButtonLink (
92- t ('New Event Rule ' ),
93- Url::fromPath ('notifications/event-rule/edit ' , [ ' id ' => - 1 ] ),
87+ t ('Add Event Rule ' ),
88+ Url::fromPath ('notifications/event-rules/add ' ),
9489 'plus '
95- ))->openInModal ()
96- ->addAttributes (['class ' => 'add-new-component ' ])
97- );
90+ ))->openInModal ();
91+ if (isset ($ this ->sessionNamespace ->{-1 })) {
92+ $ this ->addContent (Html::tag ('div ' , ['class ' => 'add-new-component ' ], [
93+ $ addButton ->disable ($ this ->translate (
94+ 'You have unsaved changes. Please save or discard them first. '
95+ )),
96+ (new Link ($ this ->translate ('Continue Editing ' ), Links::eventRule (-1 )))
97+ ->setBaseTarget ('_next ' )
98+ ]));
99+ } else {
100+ $ this ->addContent ($ addButton ->addAttributes (['class ' => 'add-new-component ' ]));
101+ }
98102
99103 $ this ->addContent (
100104 (new ObjectList ($ eventRules , new EventRuleRenderer ()))
@@ -111,62 +115,19 @@ public function indexAction(): void
111115
112116 public function addAction (): void
113117 {
114- $ this ->addTitleTab (t ('Add Event Rule ' ));
115- $ this ->getTabs ()->setRefreshUrl (Url::fromPath ('notifications/event-rules/add ' , ['id ' => '-1 ' ]));
116-
117- $ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
118- $ ruleId = $ this ->params ->get ('id ' );
119- $ config = $ this ->sessionNamespace ->get ($ ruleId );
120- $ config ['object_filter ' ] = $ config ['object_filter ' ] ?? null ;
121-
122- $ eventRuleConfigSubmitButton = (new SubmitButtonElement (
123- 'save ' ,
124- [
125- 'label ' => t ('Add Event Rule ' ),
126- 'form ' => 'event-rule-config-form '
127- ]
128- ))->setWrapper (new HtmlElement ('div ' , Attributes::create (['class ' => ['icinga-controls ' , 'save-config ' ]])));
129-
130- $ eventRuleConfig = new EventRuleConfigForm (
131- $ config ,
132- Url::fromPath (
133- 'notifications/event-rules/search-editor ' ,
134- ['id ' => $ ruleId ]
135- )
136- );
137-
138- $ eventRuleConfig
139- ->populate ($ config )
140- ->on (Form::ON_SUCCESS , function (EventRuleConfigForm $ form ) use ($ config ) {
141- $ ruleId = (int ) $ config ['id ' ];
142- $ ruleName = $ config ['name ' ];
143- $ insertId = $ form ->addOrUpdateRule ($ ruleId , $ config );
144- $ this ->sessionNamespace ->delete ($ ruleId );
145- Notification::success (sprintf (t ('Successfully add event rule %s ' ), $ ruleName ));
146- $ this ->redirectNow (Links::eventRule ($ insertId ));
147- })
148- ->on (EventRuleConfigForm::ON_CHANGE , function (EventRuleConfigForm $ form ) use ($ config ) {
149- $ formValues = $ form ->getValues ();
150- $ config = array_merge ($ config , $ formValues );
151- $ config ['rule_escalation ' ] = $ formValues ['rule_escalation ' ];
152- $ this ->sessionNamespace ->set ('-1 ' , $ config );
153- })
154- ->handleRequest ($ this ->getServerRequest ());
155-
156- $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
157- Html::tag ('h2 ' , $ config ['name ' ] ?? '' ),
158- (new Link (
159- new Icon ('edit ' ),
160- Url::fromPath ('notifications/event-rule/edit ' , [
161- 'id ' => -1
162- ]),
163- ['class ' => 'control-button ' ]
164- ))->openInModal ()
165- ]);
166-
167- $ this ->addControl ($ eventRuleForm );
168- $ this ->addControl ($ eventRuleConfigSubmitButton );
169- $ this ->addContent ($ eventRuleConfig );
118+ $ this ->setTitle ($ this ->translate ('Add Event Rule ' ));
119+
120+ $ eventRuleForm = (new EventRuleForm ())
121+ ->populate (['id ' => -1 ])
122+ ->setAction (Url::fromRequest ()->getAbsoluteUrl ())
123+ ->on (Form::ON_SUCCESS , function ($ form ) {
124+ $ this ->sessionNamespace ->set (-1 , ['id ' => -1 , 'name ' => $ form ->getValue ('name ' )]);
125+ $ this ->sendExtraUpdates (['#col1 ' ]);
126+ $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
127+ $ this ->redirectNow (Links::eventRule (-1 ));
128+ })->handleRequest ($ this ->getServerRequest ());
129+
130+ $ this ->addContent ($ eventRuleForm );
170131 }
171132
172133 public function completeAction (): void
0 commit comments