66
77use Icinga \Module \Notifications \Common \Database ;
88use Icinga \Module \Notifications \Common \Links ;
9- use Icinga \Module \Notifications \Forms \SaveEventRuleForm ;
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 ;
13- use Icinga \Module \Notifications \Widget \EventRuleConfig ;
1413use Icinga \Module \Notifications \Widget \ItemList \ObjectList ;
15- use Icinga \Web \Notification ;
1614use Icinga \Web \Session ;
17- use ipl \Html \Html ;
18- use ipl \Stdlib \Filter ;
15+ use ipl \Html \Form ;
1916use ipl \Web \Compat \CompatController ;
2017use ipl \Web \Compat \SearchControls ;
2118use ipl \Web \Control \LimitControl ;
22- use ipl \Web \Control \SearchEditor ;
2319use ipl \Web \Control \SortControl ;
2420use ipl \Web \Filter \QueryString ;
2521use ipl \Web \Layout \DetailedItemLayout ;
2622use ipl \Web \Url ;
2723use ipl \Web \Widget \ButtonLink ;
28- use ipl \Web \Widget \Icon ;
29- use ipl \Web \Widget \Link ;
3024
3125class EventRulesController extends CompatController
3226{
3327 use SearchControls;
3428
35- /** @var Filter\Rule Filter from query string parameters */
36- private $ filter ;
37-
38- /** @var Session\SessionNamespace */
39- private $ sessionNamespace ;
40-
4129 public function init ()
4230 {
4331 $ this ->assertPermission ('notifications/config/event-rules ' );
44- $ this ->sessionNamespace = Session::getSession ()->getNamespace ('notifications ' );
4532 }
4633
4734 public function indexAction (): void
@@ -53,8 +40,8 @@ public function indexAction(): void
5340 $ sortControl = $ this ->createSortControl (
5441 $ eventRules ,
5542 [
56- 'name ' => t ('Name ' ),
57- 'changed_at ' => t ('Changed At ' )
43+ 'name ' => $ this -> translate ('Name ' ),
44+ 'changed_at ' => $ this -> translate ('Changed At ' )
5845 ]
5946 );
6047
@@ -68,7 +55,7 @@ public function indexAction(): void
6855
6956 if ($ searchBar ->hasBeenSent () && ! $ searchBar ->isValid ()) {
7057 if ($ searchBar ->hasBeenSubmitted ()) {
71- $ filter = $ this ->getFilter ( );
58+ $ filter = QueryString:: parse (( string ) $ this ->params );
7259 } else {
7360 $ this ->addControl ($ searchBar );
7461 $ this ->sendMultipartUpdate ();
@@ -84,13 +71,14 @@ public function indexAction(): void
8471 $ this ->addControl ($ sortControl );
8572 $ this ->addControl ($ limitControl );
8673 $ this ->addControl ($ searchBar );
74+
8775 $ this ->addContent (
8876 (new ButtonLink (
89- t ('New Event Rule ' ),
90- Url::fromPath ('notifications/event-rule/edit ' , ['id ' => -1 , 'clearCache ' => true ]),
91- 'plus '
77+ $ this ->translate ('Add Event Rule ' ),
78+ Url::fromPath ('notifications/event-rules/add ' ),
79+ 'plus ' ,
80+ ['class ' => 'add-new-component ' ]
9281 ))->openInModal ()
93- ->addAttributes (['class ' => 'add-new-component ' ])
9482 );
9583
9684 $ this ->addContent (
@@ -108,69 +96,19 @@ public function indexAction(): void
10896
10997 public function addAction (): void
11098 {
111- $ this ->addTitleTab (t ('Add Event Rule ' ));
112- $ this ->getTabs ()->setRefreshUrl (Url::fromPath ('notifications/event-rules/add ' ));
113-
114- $ this ->controls ->addAttributes (['class ' => 'event-rule-detail ' ]);
115-
116- if ($ this ->params ->has ('use_cache ' ) || $ this ->getServerRequest ()->getMethod () !== 'GET ' ) {
117- $ cache = $ this ->sessionNamespace ->get (-1 , []);
118- } else {
119- $ this ->sessionNamespace ->delete (-1 );
120-
121- $ cache = [];
122- }
123-
124- $ eventRuleConfig = new EventRuleConfig (Url::fromPath ('notifications/event-rules/add-search-editor ' ), $ cache );
99+ $ this ->setTitle ($ this ->translate ('Add Event Rule ' ));
125100
126- $ eventRuleForm = Html::tag ('div ' , ['class ' => 'event-rule-form ' ], [
127- Html::tag ('h2 ' , $ eventRuleConfig ->getConfig ()['name ' ] ?? '' ),
128- (new Link (
129- new Icon ('edit ' ),
130- Url::fromPath ('notifications/event-rule/edit ' , [
131- 'id ' => -1
132- ]),
133- ['class ' => 'control-button ' ]
134- ))->openInModal ()
135- ]);
136-
137- $ saveForm = (new SaveEventRuleForm ())
138- ->on (SaveEventRuleForm::ON_SUCCESS , function ($ saveForm ) use ($ eventRuleConfig ) {
139- if (! $ eventRuleConfig ->isValid ()) {
140- $ eventRuleConfig ->addAttributes (['class ' => 'invalid ' ]);
141- return ;
142- }
143-
144- $ id = $ saveForm ->addRule ($ this ->sessionNamespace ->get (-1 ));
145-
146- Notification::success ($ this ->translate ('Successfully added rule. ' ));
101+ $ eventRuleForm = (new EventRuleForm ())
102+ ->populate (['id ' => -1 ])
103+ ->setCsrfCounterMeasureId (Session::getSession ()->getId ())
104+ ->setAction (Url::fromRequest ()->getAbsoluteUrl ())
105+ ->on (Form::ON_SUCCESS , function ($ form ) {
147106 $ this ->sendExtraUpdates (['#col1 ' ]);
148- $ this ->redirectNow (Links::eventRule ($ id ));
107+ $ this ->getResponse ()->setHeader ('X-Icinga-Container ' , 'col2 ' );
108+ $ this ->redirectNow (Links::eventRule (-1 )->addParams (['name ' => $ form ->getValue ('name ' )]));
149109 })->handleRequest ($ this ->getServerRequest ());
150110
151- $ eventRuleConfig ->on (EventRuleConfig::ON_CHANGE , function ($ eventRuleConfig ) {
152- $ this ->sessionNamespace ->set (-1 , $ eventRuleConfig ->getConfig ());
153-
154- $ this ->redirectNow (Url::fromPath ('notifications/event-rules/add ' , ['use_cache ' => true ]));
155- });
156-
157- foreach ($ eventRuleConfig ->getForms () as $ f ) {
158- $ f ->handleRequest ($ this ->getServerRequest ());
159-
160- if (! $ f ->hasBeenSent ()) {
161- // Force validation of populated values in case we display an unsaved rule
162- $ f ->validatePartial ();
163- }
164- }
165-
166- $ eventRuleFormAndSave = Html::tag ('div ' , ['class ' => 'event-rule-and-save-forms ' ]);
167- $ eventRuleFormAndSave ->add ([
168- $ eventRuleForm ,
169- $ saveForm
170- ]);
171-
172- $ this ->addControl ($ eventRuleFormAndSave );
173- $ this ->addContent ($ eventRuleConfig );
111+ $ this ->addContent ($ eventRuleForm );
174112 }
175113
176114 public function completeAction (): void
@@ -195,49 +133,6 @@ public function searchEditorAction(): void
195133 $ this ->setTitle ($ this ->translate ('Adjust Filter ' ));
196134 }
197135
198- public function addSearchEditorAction (): void
199- {
200- $ cache = $ this ->sessionNamespace ->get (-1 );
201-
202- $ editor = EventRuleConfig::createSearchEditor ()
203- ->setQueryString ($ cache ['object_filter ' ] ?? '' );
204-
205- $ editor ->on (SearchEditor::ON_SUCCESS , function (SearchEditor $ form ) {
206- $ cache = $ this ->sessionNamespace ->get (-1 );
207- $ cache ['object_filter ' ] = EventRuleConfig::createFilterString ($ form ->getFilter ());
208-
209- $ this ->sessionNamespace ->set (-1 , $ cache );
210-
211- $ this ->getResponse ()
212- ->setHeader ('X-Icinga-Container ' , '_self ' )
213- ->redirectAndExit (
214- Url::fromPath (
215- 'notifications/event-rules/add ' ,
216- ['use_cache ' => true ]
217- )
218- );
219- });
220-
221- $ editor ->handleRequest ($ this ->getServerRequest ());
222-
223- $ this ->getDocument ()->addHtml ($ editor );
224- $ this ->setTitle ($ this ->translate ('Adjust Filter ' ));
225- }
226-
227- /**
228- * Get the filter created from query string parameters
229- *
230- * @return Filter\Rule
231- */
232- protected function getFilter (): Filter \Rule
233- {
234- if ($ this ->filter === null ) {
235- $ this ->filter = QueryString::parse ((string ) $ this ->params );
236- }
237-
238- return $ this ->filter ;
239- }
240-
241136 public function getTabs ()
242137 {
243138 if ($ this ->getRequest ()->getActionName () === 'index ' ) {
0 commit comments