/** * Parse a XMLElement reading menu entries * @param $xml A SimpleXMLElement Object * @param $permission_callback check permission callback */ public function parse($xml, $permission_callback = NULL) { $i = 0; foreach ($xml as $xmlElement) { $atts = $xmlElement->attributes(); $label = (string) $atts['label']; $action = (string) $xmlElement->action; $icon = (string) $xmlElement->icon; $menu = NULL; $menuItem = new TMenuItem($label, $action, $icon); if ($xmlElement->menu) { $menu = new TMenu($xmlElement->menu->menuitem, $permission_callback, $this->menu_level + 1, $this->menu_class, $this->item_class); $menuItem->setMenu($menu); } // just child nodes have actions if ($action) { if (!empty($action) and $permission_callback) { // check permission $parts = explode('#', $action); $className = $parts[0]; if (call_user_func($permission_callback, $className)) { $this->addMenuItem($menuItem); } } else { // menus without permission check $this->addMenuItem($menuItem); } } else { if (isset($menu) and count($menu->getMenuItems()) > 0) { $this->addMenuItem($menuItem); } } $i++; } }
/** * Build a MenuBar from a XML file * @param $xml_file path for the file * @param $permission_callback check permission callback */ public static function newFromXML($xml_file, $permission_callback = NULL, $bar_class = 'nav navbar-nav', $menu_class = 'dropdown-menu', $item_class = '') { if (file_exists($xml_file)) { $menu_string = file_get_contents($xml_file); if (utf8_encode(utf8_decode($menu_string)) == $menu_string) { $xml = new SimpleXMLElement($menu_string); } else { $xml = new SimpleXMLElement(utf8_encode($menu_string)); } $menubar = new TMenuBar(); $ul = new TElement('ul'); $ul->{'class'} = $bar_class; $menubar->add($ul); foreach ($xml as $xmlElement) { $atts = $xmlElement->attributes(); $label = (string) $atts['label']; $action = (string) $xmlElement->action; $icon = (string) $xmlElement->icon; $item = new TMenuItem($label, $action, $icon); $menu = new TMenu($xmlElement->menu->menuitem, $permission_callback, 1, $menu_class, $item_class); // check children count (permissions) if (count($menu->getMenuItems()) > 0) { $item->setMenu($menu); $item->{'class'} = 'active'; $ul->add($item); } else { if ($action) { $ul->add($item); } } } return $menubar; } }