/** * Prepare cached data for CMS usage * * * @access public * * @param bool $forceFlush ,default is bool false * @return array cached CMS data serialized */ public function prepareCachedCMSData($forceFlush = false) { if ($forceFlush === false) { $cachedCMSDataKeys = array(self::MENUS_KEY, self::MENUS_PATHS_KEY); $existingCachedCMSDataKeys = $this->cache->cacheAdapter->hasItems($cachedCMSDataKeys); } if ($forceFlush === true || $forceFlush === false && count($existingCachedCMSDataKeys) !== count($cachedCMSDataKeys)) { $menuItems = $this->menuItem->getMenuItems(); $menuItemsPaths = $this->query->setEntity('CMS\\Entity\\MenuItem')->entityRepository->getMenuItemsSorted(array(), true, true, true, "p.path as path", false); $menuItemsPathsFlat = array(); array_walk_recursive($menuItemsPaths, function ($value) use(&$menuItemsPathsFlat) { $menuItemsPathsFlat[] = $value; }); $items = array(self::MENUS_KEY => serialize($menuItems), self::MENUS_PATHS_KEY => serialize($menuItemsPathsFlat)); $this->cache->setItems($items); } else { $items = $this->getCachedCMSData(); $items = array(self::MENUS_KEY => serialize($items[self::MENUS_KEY]), self::MENUS_PATHS_KEY => serialize($items[self::MENUS_PATHS_KEY])); } return $items; }
/** * Get menu items sorted according to menu and menu items' parents * Merge static menus with dynamic ones * Merged result is reordered according to first level menu items' weights * * @access public * @param bool $includeStatic ,default is true * * @return array all menus sorted * @throws Exception query must be instance of Query */ public function getMenuItems($includeStatic = true) { if (!$this->query instanceof Query) { throw new Exception("query must be instance of Query"); } // get dynamic menus $menuItems = $this->query->setEntity('CMS\\Entity\\MenuItem')->entityRepository->getMenuItemsSorted(array(), true, true, false, null, true); if ($includeStatic === true) { $weight = array(); // merge static and dynamic menus foreach ($this->staticMenus as $staticMenuTitle => $staticMenuArray) { if (array_key_exists($staticMenuTitle, $menuItems)) { $menuItems[$staticMenuTitle] = array_merge($staticMenuArray, $menuItems[$staticMenuTitle]); } else { $menuItems[$staticMenuTitle] = $staticMenuArray; } } foreach ($menuItems as $menuTitle => $menuArray) { $weight[$menuTitle] = array(); $sortedMenuArray = array(); // collect menu weights foreach ($menuArray as $menuItemTitle => $menuItemArray) { $weight[$menuTitle][] = $menuItemArray['weight']; } // reorder weights per menu asort($weight[$menuTitle]); // reoreder Merged result according to first level menu items' weights foreach ($weight[$menuTitle] as $menuItemWeight) { foreach ($menuArray as $menuItemTitle => $menuItemArray) { if ($menuItemArray['weight'] === $menuItemWeight) { $sortedMenuArray[$menuItemTitle] = $menuItemArray; } } } $menuItems[$menuTitle] = $sortedMenuArray; } } return $menuItems; }