99use Icinga \Module \Businessprocess \MonitoredNode ;
1010use Icinga \Module \Businessprocess \Node ;
1111use Icinga \Module \Businessprocess \Web \Url ;
12+ use Icinga \Module \Businessprocess \Widget \StateBadge ;
1213use ipl \Html \BaseHtmlElement ;
1314use ipl \Html \Html ;
1415use ipl \Html \HtmlDocument ;
16+ use Tests \Icinga \Module \Setup \TrueRequirement ;
1517
1618abstract class Renderer extends HtmlDocument
1719{
@@ -135,34 +137,34 @@ public function countChildNodes()
135137 * @param $summary
136138 * @return BaseHtmlElement
137139 */
138- public function renderStateBadges ($ summary )
140+ public function renderStateBadges ($ summary, $ totalChildren )
139141 {
140142 $ elements = [];
141143
142- foreach ($ summary as $ state => $ cnt ) {
143- if ($ cnt === 0
144- || $ state === 'OK '
145- || $ state === 'UP '
146- ) {
147- continue ;
148- }
149-
150- $ elements [] = Html::tag (
151- 'span ' ,
152- [
153- 'class ' => [
154- 'badge ' ,
155- 'badge- ' . strtolower ($ state )
156- ],
157- // TODO: We should translate this in this module
158- 'title ' => mt ('monitoring ' , $ state )
144+ $ itemCount = Html::tag (
145+ 'span ' ,
146+ [
147+ 'class ' => [
148+ 'item-count ' ,
159149 ],
160- $ cnt
161- );
162- }
150+ // TODO: We should translate this in this module
151+ 'title ' => mt ('monitoring ' , 'widget ' )
152+ ],
153+ $ totalChildren
154+ );
155+
156+ $ elements [] = array_filter ([
157+ $ this ->createBadgeGroup ($ summary , 'CRITICAL ' ),
158+ $ this ->createBadgeGroup ($ summary , 'UNKNOWN ' ),
159+ $ this ->createBadgeGroup ($ summary , 'WARNING ' ),
160+ $ this ->createBadge ($ summary , 'OK ' ),
161+ $ this ->createBadge ($ summary , 'MISSING ' ),
162+ $ this ->createBadge ($ summary , 'PENDING ' )
163+ ]);
163164
164165 if (!empty ($ elements )) {
165- $ container = Html::tag ('div ' , ['class ' => 'badges ' ]);
166+ $ container = Html::tag ('ul ' , ['class ' => 'state-badges ' ]);
167+ $ container ->add ($ itemCount );
166168 foreach ($ elements as $ element ) {
167169 $ container ->add ($ element );
168170 }
@@ -172,12 +174,39 @@ public function renderStateBadges($summary)
172174 return null ;
173175 }
174176
177+ protected function createBadge ($ summary , $ state )
178+ {
179+ if ($ summary [$ state ] !== 0 ) {
180+ return Html::tag ('li ' , new StateBadge ($ summary [$ state ], $ state ));
181+ }
182+
183+ return null ;
184+ }
185+
186+ protected function createBadgeGroup ($ summary , $ state )
187+ {
188+ $ content = [];
189+ if ($ summary [$ state ] !== 0 ) {
190+ $ content [] = Html::tag ('li ' , new StateBadge ($ summary [$ state ], $ state ));
191+ }
192+
193+ if ($ summary [$ state . '-HANDLED ' ] !== 0 ) {
194+ $ content [] = Html::tag ('li ' , new StateBadge ($ summary [$ state . '-HANDLED ' ], $ state , true ));
195+ }
196+
197+ if (empty ($ content )) {
198+ return null ;
199+ }
200+
201+ return Html::tag ('li ' , Html::tag ('ul ' , $ content ));
202+ }
203+
175204 public function getNodeClasses (Node $ node )
176205 {
177206 if ($ node ->isMissing ()) {
178207 $ classes = array ('missing ' );
179208 } else {
180- if ($ node ->isEmpty () && ! $ node instanceof MonitoredNode) {
209+ if ($ node ->isEmpty () && !$ node instanceof MonitoredNode) {
181210 $ classes = array ('empty ' );
182211 } else {
183212 $ classes = [strtolower ($ node ->getStateName (
@@ -205,7 +234,7 @@ public function getNodeClasses(Node $node)
205234 /**
206235 * Return the url to the given node's source configuration
207236 *
208- * @param BpNode $node
237+ * @param BpNode $node
209238 *
210239 * @return Url
211240 */
@@ -221,12 +250,12 @@ public function getSourceUrl(BpNode $node)
221250
222251 $ url = clone $ this ->getUrl ();
223252 $ url ->setParams ([
224- 'config ' => $ node ->getBpConfig ()->getName (),
225- 'node ' => $ name
253+ 'config ' => $ node ->getBpConfig ()->getName (),
254+ 'node ' => $ name
226255 ]);
227256 // This depends on the fact that the node's root path is the last element in $paths
228257 $ url ->getParams ()->addValues ('path ' , array_slice (array_pop ($ paths ), 0 , -1 ));
229- if (! $ this ->isLocked ()) {
258+ if (!$ this ->isLocked ()) {
230259 $ url ->getParams ()->add ('unlocked ' , true );
231260 }
232261
0 commit comments