*/ class MenuService { /** * identifier to use to cache the structures * @var string */ const CACHE_KEY = 'tao_structures'; /** * Use caching mechanism for the structure. * For performances issues, cache should be enabled. * @var boolean */ const USE_CACHE = true; /** * to stock the extension structure * * @access protected * @var array */ protected static $structure = []; // --- OPERATIONS --- /** * Load the extension structure file. * Return the SimpleXmlElement object (don't forget to cast it) * * @access protected * @author Jerome Bogaerts, * @param string extensionId * @return SimpleXMLElement */ public static function getStructuresFilePath($extensionId) { $extension = \common_ext_ExtensionsManager::singleton()->getExtensionById($extensionId); $extra = $extension->getManifest()->getExtra(); if (isset($extra['structures'])) { $structureFilePath = $extra['structures']; } else { $structureFilePath = $extension->getDir() . 'actions/structures.xml'; } if (file_exists($structureFilePath)) { return $structureFilePath; } else { return null; } } /** * Get the structure content (from the structure.xml file) of each extension filtered by group. * @param $groupId * @return array */ public static function getPerspectivesByGroup($groupId) { $perspectives = []; foreach (self::getAllPerspectives() as $perspective) { if ($perspective -> getGroup() === $groupId) { $perspectives[] = $perspective; } } return $perspectives; } /** * Get the structure content (from the structure.xml file) of each extension. * @return array */ public static function getAllPerspectives() { $structure = self::readStructure(); return $structure['perspectives']; } /** * Reads the structure data. * This method manages to cache the structure if needed. * @return array() the structure data */ public static function readStructure() { if (count(self::$structure) == 0) { if (self::USE_CACHE == false) { //rebuild structure each time self::$structure = self::buildStructures(); } else { //cache management try { self::$structure = \common_cache_FileCache::singleton()->get(self::CACHE_KEY); } catch (\common_cache_NotFoundException $e) { self::$structure = self::buildStructures(); \common_cache_FileCache::singleton()->put(self::$structure, self::CACHE_KEY); } } } return self::$structure; } /** * Get the structure content (from the structure.xml file) of each extension. * @return array */ protected static function buildStructures() { $perspectives = []; $toAdd = []; $sorted = \helpers_ExtensionHelper::sortByDependencies(\common_ext_ExtensionsManager::singleton()->getEnabledExtensions()); foreach (array_keys($sorted) as $extId) { $file = self::getStructuresFilePath($extId); if (!is_null($file)) { $xmlStructures = new \SimpleXMLElement($file, null, true); $extStructures = $xmlStructures->xpath("/structures/structure"); foreach ($extStructures as $xmlStructure) { $perspective = Perspective::fromSimpleXMLElement($xmlStructure, $extId); if (!isset($perspectives[$perspective->getId()])) { $perspectives[$perspective->getId()] = $perspective; } else { foreach ($perspective->getChildren() as $section) { $perspectives[$perspective->getId()]->addSection($section); } } } foreach ($xmlStructures->xpath("/structures/toolbar/toolbaraction") as $xmlStructure) { $perspective = Perspective::fromLegacyToolbarAction($xmlStructure, $extId); $perspectives[$perspective->getId()] = $perspective; if (isset($xmlStructure['structure'])) { $toAdd[$perspective->getId()] = (string)$xmlStructure['structure']; } } } } foreach ($toAdd as $to => $from) { if (isset($perspectives[$from]) && isset($perspectives[$to])) { foreach ($perspectives[$from]->getChildren() as $section) { $perspectives[$to]->addSection($section); } } } usort($perspectives, function ($a, $b) { return $a->getLevel() - $b->getLevel(); }); return [ 'perspectives' => $perspectives ]; } /** * Get the perspective for the extension/section in parameters * or null if not found * * @access public * @author Jerome Bogaerts, * @param string extension * @param string perspectiveId * @return Structure */ public static function getPerspective($extension, $perspectiveId) { $returnValue = null; foreach (self::getAllPerspectives() as $perspective) { if ($perspective->getId() == $perspectiveId) { $returnValue = $perspective; break; } } if (empty($returnValue)) { \common_logger::w('Structure ' . $perspectiveId . ' not found for extension ' . $extension); } return $returnValue; } /** * Short description of method getSection * * @access public * @author Jerome Bogaerts, * @param string extension * @param string perspectiveId * @param string sectionId * @return Section */ public static function getSection($extension, $perspectiveId, $sectionId) { $returnValue = null; $structure = self::getPerspective($extension, $perspectiveId); foreach ($structure->getChildren() as $section) { if ($section->getId() == $sectionId) { $returnValue = $section; break; } } if (empty($returnValue)) { \common_logger::w('Section ' . $sectionId . ' not found found for perspective ' . $perspectiveId); } return $returnValue; } public static function flushCache() { self::$structure = []; \common_cache_FileCache::singleton()->remove(self::CACHE_KEY); } }