From 1fb5c96ef18e6f75688607607e19a2a1dff71e6f Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 3 Sep 2015 08:52:02 +0200 Subject: [PATCH] Navigation: Add method order() refs #5600 --- library/Icinga/Web/Navigation/Navigation.php | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/library/Icinga/Web/Navigation/Navigation.php b/library/Icinga/Web/Navigation/Navigation.php index e41c947ec..6260dbc95 100644 --- a/library/Icinga/Web/Navigation/Navigation.php +++ b/library/Icinga/Web/Navigation/Navigation.php @@ -109,6 +109,7 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate */ public function getIterator() { + $this->order(); return new ArrayIterator($this->items); } @@ -269,6 +270,40 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate return new RecursiveNavigationRenderer($this); } + /** + * Order this navigation's items + * + * @return $this + */ + public function order() + { + uasort($this->items, array($this, 'compareItems')); + foreach ($this->items as $item) { + if ($item->hasChildren()) { + $item->getChildren()->order(); + } + } + + return $this; + } + + /** + * Return whether the first item is less than, more than or equal to the second one + * + * @param NavigationItem $a + * @param NavigationItem $b + * + * @return int + */ + protected function compareItems(NavigationItem $a, NavigationItem $b) + { + if ($a->getPriority() === $b->getPriority()) { + return strcasecmp($a->getLabel(), $b->getLabel()); + } + + return $a->getPriority() > $b->getPriority() ? 1 : -1; + } + /** * Merge this navigation with the given one *