6
6
7
7
use Icinga \Module \Notifications \Common \Database ;
8
8
use Icinga \Module \Notifications \Common \Links ;
9
- use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
9
+ use Icinga \Module \Notifications \Forms \EventRuleForm ;
10
10
use Icinga \Module \Notifications \Model \Rule ;
11
11
use Icinga \Module \Notifications \View \EventRuleRenderer ;
12
12
use Icinga \Module \Notifications \Web \Control \SearchBar \ObjectSuggestions ;
13
13
use Icinga \Module \Notifications \Widget \ItemList \ObjectList ;
14
- use Icinga \Web \Notification ;
15
14
use Icinga \Web \Session ;
16
- use ipl \Html \Attributes ;
17
15
use ipl \Html \Form ;
18
- use ipl \Html \FormElement \SubmitButtonElement ;
19
16
use ipl \Html \Html ;
20
- use ipl \Html \HtmlElement ;
21
17
use ipl \Stdlib \Filter ;
22
18
use ipl \Web \Compat \CompatController ;
23
19
use ipl \Web \Compat \SearchControls ;
27
23
use ipl \Web \Layout \DetailedItemLayout ;
28
24
use ipl \Web \Url ;
29
25
use ipl \Web \Widget \ButtonLink ;
30
- use ipl \Web \Widget \Icon ;
31
26
use ipl \Web \Widget \Link ;
32
27
33
28
class EventRulesController extends CompatController
@@ -49,7 +44,6 @@ public function init()
49
44
public function indexAction (): void
50
45
{
51
46
$ eventRules = Rule::on (Database::get ());
52
- $ this ->sessionNamespace ->delete ('-1 ' );
53
47
54
48
$ limitControl = $ this ->createLimitControl ();
55
49
$ paginationControl = $ this ->createPaginationControl ($ eventRules );
@@ -87,14 +81,24 @@ public function indexAction(): void
87
81
$ this ->addControl ($ sortControl );
88
82
$ this ->addControl ($ limitControl );
89
83
$ this ->addControl ($ searchBar );
90
- $ this ->addContent (
84
+
85
+ $ addButton =
91
86
(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 ' ),
94
89
'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
+ }
98
102
99
103
$ this ->addContent (
100
104
(new ObjectList ($ eventRules , new EventRuleRenderer ()))
@@ -111,62 +115,19 @@ public function indexAction(): void
111
115
112
116
public function addAction (): void
113
117
{
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 );
170
131
}
171
132
172
133
public function completeAction (): void
0 commit comments