Example #1
0
 /**
  * render menu markup
  *
  * @return string
  */
 public function render()
 {
     // check authentication
     if (!$this->authenticateMenu($this->menu)) {
         return '';
     }
     // if menu has not been prepared yet, do it now (caching avoids re-parsing for submenus)
     if (!in_array($this->menu, self::$primedMenus, TRUE)) {
         // clear selected menu entries (which remain in the session)
         $this->clearSelectedMenuEntries($this->menu);
         // walk entire menu and add dynamic entries where necessary
         $this->completeMenu($this->menu);
         // prepare path segments to identify active menu entries
         $this->pathSegments = explode('/', trim($this->request->getPathInfo(), '/'));
         // skip script name
         if ($this->useNiceUris && basename($this->request->getScriptName()) != 'index.php') {
             array_shift($this->pathSegments);
         }
         // skip locale if one found
         if (count($this->pathSegments) && Application::getInstance()->hasLocale($this->pathSegments[0])) {
             array_shift($this->pathSegments);
         }
         // walk tree until an active entry is reached
         $this->walkMenuTree($this->menu, $this->pathSegments[0] === '' ? explode('/', $this->route->getPath()) : $this->pathSegments);
         // cache menu for multiple renderings
         self::$primedMenus[] = $this->menu;
     }
     $htmlId = $this->id . 'menu';
     // drill down to required submenu (if only submenu needs to be rendered)
     $m = $this->menu;
     if ($this->level !== FALSE) {
         $htmlId .= '_level_' . $this->level;
         if ($this->level > 0) {
             while ($this->level-- > 0) {
                 $e = $this->menu->getSelectedEntry();
                 if (!$e || !$e->getSubMenu()) {
                     break;
                 }
                 $m = $e->getSubMenu();
             }
             if ($this->level >= 0) {
                 return '';
             }
         }
     }
     // output
     // instantiate renderer class, defaults to SimpleListRenderer
     if (!empty($this->decorator)) {
         $rendererName = $this->decorator;
     } else {
         $rendererName = 'SimpleList';
     }
     $className = __NAMESPACE__ . '\\Menu\\Renderer\\' . $rendererName . 'Renderer';
     $renderer = new $className($m);
     $renderer->setParameters($this->renderArgs);
     // enable or disable display of submenus
     $m->setShowSubmenus($this->level === FALSE);
     // enable or disable always active menu
     $m->setForceActive(self::$forceActiveMenu);
     // if no container tag was specified, use a DIV element
     if (!isset($this->renderArgs['containerTag'])) {
         $this->renderArgs['containerTag'] = 'div';
     }
     // omit wrapper, if a falsy container tag was specified
     if ($this->renderArgs['containerTag']) {
         return sprintf('<%1$s%2$s>%3$s</%1$s>', $this->renderArgs['containerTag'], isset($this->renderArgs['omitId']) && $this->renderArgs['omitId'] ? '' : ' id="' . $htmlId . '"', $renderer->render());
     }
     return $renderer->render();
 }