diff --git a/Api/Data/MenuInterface.php b/Api/Data/MenuInterface.php new file mode 100755 index 0000000..2678fc8 --- /dev/null +++ b/Api/Data/MenuInterface.php @@ -0,0 +1,150 @@ +menuRepository = $menuRepository; + $this->nodeRepository = $nodeRepository; + $this->nodeTypeProvider = $nodeTypeProvider; + $this->searchCriteriaFactory = $searchCriteriaFactory; + $this->filterGroupBuilder = $filterGroupBuilder; + $this->eventManager = $eventManager; + $this->templateResolver = $templateResolver; + $this->submenuTemplate = $this->getMenuTemplate( + 'Ecomteck_Megamenu::menu/sub_menu.phtml' + ); + $this->setTemplate($this->getMenuTemplate($this->_template)); + } + + /** + * Return unique ID(s) for each object in system + * + * @return string[] + */ + public function getIdentities() + { + return [\Ecomteck\Megamenu\Model\Menu::CACHE_TAG, Block::CACHE_TAG]; + } + + protected function getCacheLifetime() + { + return 60*60*24*365; + } + + /** + * @return array|\Ecomteck\Megamenu\Model\Menu + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function loadMenu() + { + if (!$this->menu) { + $storeId = $this->_storeManager->getStore()->getId(); + $this->menu = $this->menuRepository->get($this->getData('menu'), $storeId); + } + return $this->menu; + } + + /** + * @return array|\Ecomteck\Megamenu\Model\Menu|null + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function getMenu() + { + $menu = $this->loadMenu(); + if (!$menu->getMenuId()) { + return null; + } + + return $menu; + } + + /** + * @return array + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function getCacheKeyInfo() + { + $info = [ + \Ecomteck\Megamenu\Model\Menu::CACHE_TAG, + 'menu_' . $this->loadMenu()->getId(), + 'store_' . $this->_storeManager->getStore()->getId(), + 'template_' . $this->getTemplate() + ]; + + $nodeCacheKeyInfo = $this->getNodeCacheKeyInfo(); + if ($nodeCacheKeyInfo) { + $info = array_merge($info, $nodeCacheKeyInfo); + } + + return $info; + } + + /** + * @return array + */ + private function getNodeCacheKeyInfo() + { + $info = []; + $nodeType = ''; + $request = $this->getRequest(); + + switch ($request->getRouteName()) { + case 'cms': + $nodeType = 'cms_page'; + break; + case 'catalog': + $nodeType = 'category'; + break; + } + + $transport = [ + 'node_type' => $nodeType, + 'request' => $request + ]; + + $transport = new DataObject($transport); + $this->eventManager->dispatch( + 'ecomteck_menu_cache_node_type', + ['transport' => $transport] + ); + + if ($transport->getNodeType()) { + $nodeType = $transport->getNodeType(); + } + + if ($nodeType) { + $info = $this->getNodeTypeProvider($nodeType)->getNodeCacheKeyInfo(); + } + + if ($this->getParentNode()) { + $info[] = 'parent_node_' . $this->getParentNode()->getNodeId(); + } + + return $info; + } + + /** + * @param int $level + * @param null $parent + * @return string + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function getMenuHtml($level = 0, $parent = null) + { + $nodes = $this->getNodes($level, $parent); + $html = ''; + $i = 0; + foreach ($nodes as $node) { + $children = $this->getMenuHtml($level + 1, $node); + $classes = [ + 'level' . $level, + $node->getClasses() ?: '', + ]; + if (!empty($children)) { + $classes[] = 'parent'; + } + if ($i == 0) { + $classes[] = 'first'; + } + if ($i == count($nodes) - 1) { + $classes[] = 'last'; + } + if ($level == 0) { + $classes[] = 'level-top'; + } + $html .= '