<?php
 namespace App\Menu\Structure; class Menu extends \Engine\Menu\Structure\MenuAbstract { protected $file; protected $userAccess; static protected $instance; static public function getInstance() { if (!static::$instance) { static::$instance=new self(); static::$instance->loadFromXml(APP_REAL_PATH.'_config/application/menuXML2-simple.cfg.php'); } return static::$instance; } protected function __construct() { } public function loadFromXml($menuFile) { $this->file = $menuFile; if (!is_file($menuFile)) { throw new \Exception("Menu file does not exist."); } $menuXmlString = include($menuFile); $menuXml = simplexml_load_string($menuXmlString); if (!$menuXml) { throw new \Exception("Menu file does not contain a valid XML."); } $this->structure = $this->convertXmlToMenuObj($menuXml); $this->setNotificationsSupport(((String)$menuXml['hasNotifications']=='1'?true:false)); \Engine\Plugins\Manager::getInstance()->executeEvent('Menu-afterLoad', ['menuStructure'=>$this->getStructure()]); return $this; } protected function convertXmlToMenuObj(\SimpleXMLElement $sectionXml, ?\Engine\Menu\Structure\Section $parentSection=null) { if (!$parentSection) { $parentSection = new \App\Menu\Structure\Section('root', null); } foreach ($sectionXml->children() as $secXml) { $idCurSec=(String)$secXml['id']; $accLev=(String)$secXml['access']; if (!$accLev) $accLev=null; $type = ((String)$secXml['type']?:'menuItem'); $jsAction = (String)$secXml['jsAction']; if (!$jsAction) { $jsAction = null; } $sectionObj = new \App\Menu\Structure\Section(($idCurSec ? $idCurSec : (String)$secXml['path']), (String)$secXml['path'], $accLev); $sectionObj->setNonMVC((Boolean)$secXml['nonMVC']); $sectionObj->setType($type); $sectionObj->setJsAction($jsAction); $parentSection->addChildSection($sectionObj); if ($secXml->section) { $this->convertXmlToMenuObj($secXml, $sectionObj); } } return $parentSection; } public function setUserAccess(\App\UserAccess\UserAccessAbstract $userAccess) { $this->userAccess = $userAccess; return $this; } public function shouldDisplaySection(\Engine\Menu\Structure\Section $section) { if ( $this->getStructure()->getChildById($section->getId())!=$section ) { throw new \Exception('The current section '.$section->getId().' does not exist in the menu.'); } if ($section->isAlways()) { return true; } if (in_array($section->getType(), ['separator', 'title'])) { return true; } if ($section->isInactive() or $section->isHidden()) { return false; } return $this->userAccess->hasAccessToSection($section->getId()); } public function getFile() { return $this->file; } } 