From 4bb065d9bbd53d83587c04597e417b5fb3ca6ba1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 5 Sep 2014 17:24:55 +0200 Subject: [PATCH] Web\Menu: provide and render unique id fixes #7083 --- library/Icinga/Web/Menu.php | 46 +++++++++++++++++++++++++++-- library/Icinga/Web/MenuRenderer.php | 4 +-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/library/Icinga/Web/Menu.php b/library/Icinga/Web/Menu.php index e64688d47..632266007 100644 --- a/library/Icinga/Web/Menu.php +++ b/library/Icinga/Web/Menu.php @@ -61,15 +61,25 @@ class Menu implements RecursiveIterator */ protected $subMenus = array(); + /** + * Parent menu + * + * @var Menu + */ + protected $parent; + /** * Create a new menu * * @param int $id The id of this menu * @param Zend_Config $config The configuration for this menu */ - public function __construct($id, Zend_Config $config = null) + public function __construct($id, Zend_Config $config = null, Menu $parent = null) { $this->id = $id; + if ($parent !== null) { + $this->parent = $parent; + } $this->setProperties($config); } @@ -234,6 +244,30 @@ class Menu implements RecursiveIterator return $this->id; } + /** + * Get our ID without invalid characters + * + * @return string the ID + */ + protected function getSafeHtmlId() + { + return preg_replace('/[^a-zA-Z0-9]/', '_', $this->getId()); + } + + /** + * Get a unique menu item id + * + * @return string the ID + */ + public function getUniqueId() + { + if ($this->parent === null) { + return 'menuitem-' . $this->getSafeHtmlId(); + } else { + return $this->parent->getUniqueId() . '-' . $this->getSafeHtmlId(); + } + } + /** * Set the title of this menu * @@ -351,7 +385,7 @@ class Menu implements RecursiveIterator public function addSubMenu($id, Zend_Config $menuConfig = null) { if (false === ($pos = strpos($id, '.'))) { - $subMenu = new self($id, $menuConfig); + $subMenu = new self($id, $menuConfig, $this); $this->subMenus[$id] = $subMenu; } else { list($parentId, $id) = explode('.', $id, 2); @@ -618,4 +652,12 @@ class Menu implements RecursiveIterator { next($this->subMenus); } + + /** + * PHP 5.3 GC should not leak, but just to be on the safe side... + */ + public function __destruct() + { + $this->parent = null; + } } diff --git a/library/Icinga/Web/MenuRenderer.php b/library/Icinga/Web/MenuRenderer.php index 1927b1bc6..c3de9dccb 100644 --- a/library/Icinga/Web/MenuRenderer.php +++ b/library/Icinga/Web/MenuRenderer.php @@ -115,9 +115,9 @@ class MenuRenderer extends RecursiveIteratorIterator if ($childIsActive || ($passedActiveChild && $this->getDepth() === 0)) { $passedActiveChild &= $this->getDepth() !== 0; - $openTag = '
  • '; + $openTag = '
  • '; } else { - $openTag = '
  • '; + $openTag = '
  • '; } $content = $this->renderChild($child); $closingTag = '
  • ';