/** * insert or append menu entry * * @param MenuEntry $entry * @param int $ndx * @return Menu */ protected function insertEntry(MenuEntry $entry, $ndx = NULL) { if ($entry instanceof DynamicMenuEntry) { $this->dynamicEntries[] = $entry; } if (!isset($ndx) || $ndx >= count($this->entries)) { $this->entries[] = $entry; } else { array_splice($this->entries, $ndx, 0, [$entry]); } $entry->setMenu($this); return $this; }
/** * fallback method for authenticating single menu entry access on observe_table/observe_row level * positive authentication if auth_parameter contains a table name found in the admins table access setting * * @param MenuEntry $e * @return boolean */ protected function authenticateMenuEntry(MenuEntry $e) { $p = $e->getAuthParameters(); if (empty($p)) { return FALSE; } $admin = User::getSessionUser(); if (!$admin) { return FALSE; } $tables = preg_split('/\\s*,\\s*/', trim($p)); return !array_intersect($tables, $admin->getTableAccess()); }
public function __destruct() { parent::__destruct(); }
/** * Parse XML menu entries and creates menu instance * * @param simpleXmlElement $menu * @return Menu */ private function parseMenu(\SimpleXMLElement $menu) { $a = $menu->attributes(); $root = isset($a->script) ? (string) $a->script : $this->site->root_document; $type = isset($a->type) && (string) $a->type === 'dynamic' ? 'dynamic' : 'static'; $service = !empty($a->service) ? (string) $a->service : NULL; $id = !empty($a->id) ? (string) $a->id : NULL; if ($type === 'dynamic' && !$service) { throw new ConfigException("A dynamic menu requires a configured service."); } $m = new Menu($root, $id, $type, $service); if (isset($a->auth)) { // set optional authentication level; if level is not defined, menu is locked for everyone // if auth level is defined, additional authentication parameters can be set $menuAuth = strtoupper(trim((string) $a->auth)); if (defined("vxPHP\\User\\User::AUTH_{$menuAuth}")) { $m->setAuth(constant("vxPHP\\User\\User::AUTH_{$menuAuth}")); if (isset($a->auth_parameters)) { $m->setAuthParameters((string) $a->auth_parameters); } } else { $m->setAuth(-1); } } else { $menuAuth = NULL; } foreach ($menu->children() as $entry) { if ($entry->getName() == 'menuentry') { $a = $entry->attributes(); if (isset($a->page) && isset($a->path)) { throw new ConfigException(sprintf("Menu entry with both page ('%s') and path ('%s') attribute found.", (string) $a->page, (string) $a->path)); } // menu entry comes with a path attribute (which can also link an external resource) if (isset($a->path)) { $path = (string) $a->path; $local = strpos($path, '/') !== 0 && !preg_match('~^[a-z]+://~', $path); $e = new MenuEntry($path, $a, $local); } else { if (isset($a->page)) { $page = (string) $a->page; if (!isset($this->routes[$m->getScript()][$page])) { throw new ConfigException(sprintf("No route for menu entry ('%s') found. Available routes for script '%s' are '%s'.", $page, $m->getScript(), empty($this->routes[$m->getScript()]) ? 'none' : implode("', '", array_keys($this->routes[$m->getScript()])))); } $e = new MenuEntry((string) $this->routes[$m->getScript()][$page]->getPath(NULL, TRUE), $a, TRUE); } } // handle authentication settings of menu entry if ($menuAuth || isset($a->auth)) { // fallback to menu settings, when auth attribute is not set if (!isset($a->auth)) { $e->setAuth($m->getAuth()); $e->setAuthParameters($m->getAuthParameters()); } else { // set optional authentication level; if level is not defined, entry is locked for everyone // if auth level is defined, additional authentication parameters can be set $auth = strtoupper(trim((string) $a->auth)); if (defined("UserAbstract::AUTH_{$auth}")) { $e->setAuth(constant("UserAbstract::AUTH_{$auth}")); if (isset($a->auth_parameters)) { $e->setAuthParameters((string) $a->auth_parameters); } } else { $e->setAuth(-1); } } } $m->appendEntry($e); if (isset($entry->menu)) { $e->appendMenu($this->parseMenu($entry->menu)); } } else { if ($entry->getName() == 'menuentry_placeholder') { $a = $entry->attributes(); $e = new DynamicMenuEntry(NULL, $a); $m->appendEntry($e); } } } return $m; }
/** * @see \vxPHP\Webpage\Menu\Renderer\MenuRenderer::renderEntry() */ protected function renderEntry(MenuEntry $entry) { $attributes = $entry->getAttributes(); // check display attribute if (!isset($attributes->display) || $attributes->display !== 'none') { if ($this->hasNiceUris) { if (($script = basename($this->menu->getScript(), '.php')) == 'index') { $script = '/'; } else { $script = '/' . $script . '/'; } } else { $script = '/' . $this->menu->getScript() . '/'; } $sel = $this->menu->getSelectedEntry(); if (isset($attributes->text)) { // render a not selected menu entry if (!isset($sel) || $sel !== $entry) { $markup = sprintf('<li class="%s">%s<a href="%s">%s</a>%s', preg_replace('~[^\\w]~', '_', $entry->getPath()), $this->openingTags, $entry->getHref(), empty($this->parameters['rawText']) ? htmlspecialchars($attributes->text) : $attributes->text, $this->closingTags); // ensure rendering of submenus, when a parameter "unfoldAll" is set if (!empty($this->parameters['unfoldAll']) && ($subMenu = $entry->getSubMenu())) { $markup .= static::create($subMenu)->setParameters($this->parameters)->render(); } } else { // render a selected menu entry if ((!$entry->getSubMenu() || is_null($entry->getSubMenu()->getSelectedEntry())) && !$this->menu->getForceActive()) { $markup = sprintf('<li class="active %s">%s<span>%s</span>%s', preg_replace('~[^\\w]~', '_', $entry->getPath()), $this->openingTags, empty($this->parameters['rawText']) ? htmlspecialchars($attributes->text) : $attributes->text, $this->closingTags); } else { $markup = sprintf('<li class="active %s">%s<a href="%s">%s</a>%s', preg_replace('~[^\\w]~', '_', $entry->getPath()), $this->openingTags, $entry->getHref(), empty($this->parameters['rawText']) ? htmlspecialchars($attributes->text) : $attributes->text, $this->closingTags); } if ($this->menu->getShowSubmenus() && ($subMenu = $entry->getSubMenu())) { $markup .= static::create($subMenu)->setParameters($this->parameters)->render(); } } return $markup . '</li>'; } } }