Topmenu.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Smartwave\Megamenu\Block;
  3. class Topmenu extends \Magento\Framework\View\Element\Template
  4. {
  5. protected $_categoryHelper;
  6. protected $_categoryFlatConfig;
  7. protected $_topMenu;
  8. protected $_categoryFactory;
  9. protected $_helper;
  10. protected $_filterProvider;
  11. protected $_blockFactory;
  12. protected $_megamenuConfig;
  13. protected $_storeManager;
  14. public function __construct(
  15. \Magento\Framework\View\Element\Template\Context $context,
  16. \Magento\Catalog\Helper\Category $categoryHelper,
  17. \Smartwave\Megamenu\Helper\Data $helper,
  18. \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
  19. \Magento\Catalog\Model\CategoryFactory $categoryFactory,
  20. \Magento\Theme\Block\Html\Topmenu $topMenu,
  21. \Magento\Cms\Model\Template\FilterProvider $filterProvider,
  22. \Magento\Cms\Model\BlockFactory $blockFactory
  23. ) {
  24. $this->_categoryHelper = $categoryHelper;
  25. $this->_categoryFlatConfig = $categoryFlatState;
  26. $this->_categoryFactory = $categoryFactory;
  27. $this->_topMenu = $topMenu;
  28. $this->_helper = $helper;
  29. $this->_filterProvider = $filterProvider;
  30. $this->_blockFactory = $blockFactory;
  31. $this->_storeManager = $context->getStoreManager();
  32. parent::__construct($context);
  33. }
  34. public function getCategoryHelper()
  35. {
  36. return $this->_categoryHelper;
  37. }
  38. public function getCategoryModel($id)
  39. {
  40. $_category = $this->_categoryFactory->create();
  41. $_category->load($id);
  42. return $_category;
  43. }
  44. public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
  45. {
  46. return $this->_topMenu->getHtml($outermostClass, $childrenWrapClass, $limit);
  47. }
  48. public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
  49. {
  50. return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
  51. }
  52. public function getChildCategories($category)
  53. {
  54. if ($this->_categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
  55. $subcategories = (array)$category->getChildrenNodes();
  56. } else {
  57. $subcategories = $category->getChildren();
  58. }
  59. return $subcategories;
  60. }
  61. public function getActiveChildCategories($category)
  62. {
  63. $children = [];
  64. if ($this->_categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
  65. $subcategories = (array)$category->getChildrenNodes();
  66. } else {
  67. $subcategories = $category->getChildren();
  68. }
  69. foreach($subcategories as $category) {
  70. if (!$category->getIsActive()) {
  71. continue;
  72. }
  73. $children[] = $category;
  74. }
  75. return $children;
  76. }
  77. public function getBlockContent($content = '') {
  78. if(!$this->_filterProvider)
  79. return $content;
  80. return $this->_filterProvider->getBlockFilter()->filter(trim($content));
  81. }
  82. public function getCustomBlockHtml($type='after') {
  83. $html = '';
  84. $block_ids = $this->_megamenuConfig['custom_links']['staticblock_'.$type];
  85. if (!$block_ids) return '';
  86. $block_ids = preg_replace('/\s/', '', $block_ids);
  87. $ids = explode(',', $block_ids);
  88. $store_id = $this->_storeManager->getStore()->getId();
  89. foreach($ids as $block_id) {
  90. $block = $this->_blockFactory->create();
  91. $block->setStoreId($store_id)->load($block_id);
  92. if(!$block) continue;
  93. $block_content = $block->getContent();
  94. if(!$block_content) continue;
  95. $content = $this->_filterProvider->getBlockFilter()->setStoreId($store_id)->filter($block_content);
  96. if(substr($content, 0, 4) == '<ul>')
  97. $content = substr($content, 4);
  98. if(substr($content, strlen($content) - 5) == '</ul>')
  99. $content = substr($content, 0, -5);
  100. $html .= $content;
  101. }
  102. return $html;
  103. }
  104. public function getSubmenuItemsHtml($children, $level = 1, $max_level = 0, $column_width=12, $menu_type = 'fullwidth', $columns = null)
  105. {
  106. $html = '';
  107. if(!$max_level || ($max_level && $max_level == 0) || ($max_level && $max_level > 0 && $max_level-1 >= $level)) {
  108. $column_class = "";
  109. if($level == 1 && $columns && ($menu_type == 'fullwidth' || $menu_type == 'staticwidth')) {
  110. $column_class = "col-md-".$column_width." ";
  111. $column_class .= "mega-columns columns".$columns;
  112. }
  113. $html = '<ul class="subchildmenu '.$column_class.'">';
  114. foreach($children as $child) {
  115. $cat_model = $this->getCategoryModel($child->getId());
  116. $sw_menu_hide_item = $cat_model->getData('sw_menu_hide_item');
  117. if (!$sw_menu_hide_item) {
  118. $sub_children = $this->getActiveChildCategories($child);
  119. $sw_menu_cat_label = $cat_model->getData('sw_menu_cat_label');
  120. $sw_menu_icon_img = $cat_model->getData('sw_menu_icon_img');
  121. $sw_menu_font_icon = $cat_model->getData('sw_menu_font_icon');
  122. $item_class = 'level'.$level.' ';
  123. if(count($sub_children) > 0)
  124. $item_class .= 'parent ';
  125. $html .= '<li class="ui-menu-item '.$item_class.'">';
  126. if(count($sub_children) > 0) {
  127. $html .= '<div class="open-children-toggle"></div>';
  128. }
  129. if($level == 1 && $sw_menu_icon_img) {
  130. $html .= '<div class="menu-thumb-img"><a class="menu-thumb-link" href="'.$this->_categoryHelper->getCategoryUrl($child).'"><img src="' . $this->_helper->getBaseUrl().'catalog/category/' . $sw_menu_icon_img . '" alt="'.$child->getName().'"/></a></div>';
  131. }
  132. $html .= '<a href="'.$this->_categoryHelper->getCategoryUrl($child).'" title="'.$child->getName().'">';
  133. if ($level > 1 && $sw_menu_icon_img)
  134. $html .= '<img class="menu-thumb-icon" src="' . $this->_helper->getBaseUrl().'catalog/category/' . $sw_menu_icon_img . '" alt="'.$child->getName().'"/>';
  135. elseif($sw_menu_font_icon)
  136. $html .= '<em class="menu-thumb-icon '.$sw_menu_font_icon.'"></em>';
  137. $html .= '<span>'.$child->getName();
  138. if($sw_menu_cat_label)
  139. $html .= '<span class="cat-label cat-label-'.$sw_menu_cat_label.'">'.$this->_megamenuConfig['cat_labels'][$sw_menu_cat_label].'</span>';
  140. $html .= '</span></a>';
  141. if(count($sub_children) > 0) {
  142. $html .= $this->getSubmenuItemsHtml($sub_children, $level+1, $max_level, $column_width, $menu_type);
  143. }
  144. $html .= '</li>';
  145. }
  146. }
  147. $html .= '</ul>';
  148. }
  149. return $html;
  150. }
  151. public function getMegamenuHtml()
  152. {
  153. $html = '';
  154. $categories = $this->getStoreCategories(true,false,true);
  155. $this->_megamenuConfig = $this->_helper->getConfig('sw_megamenu');
  156. $max_level = $this->_megamenuConfig['general']['max_level'];
  157. $html .= $this->getCustomBlockHtml('before');
  158. foreach($categories as $category) {
  159. if (!$category->getIsActive()) {
  160. continue;
  161. }
  162. $cat_model = $this->getCategoryModel($category->getId());
  163. $sw_menu_hide_item = $cat_model->getData('sw_menu_hide_item');
  164. if(!$sw_menu_hide_item) {
  165. $children = $this->getActiveChildCategories($category);
  166. $sw_menu_cat_label = $cat_model->getData('sw_menu_cat_label');
  167. $sw_menu_icon_img = $cat_model->getData('sw_menu_icon_img');
  168. $sw_menu_font_icon = $cat_model->getData('sw_menu_font_icon');
  169. $sw_menu_cat_columns = $cat_model->getData('sw_menu_cat_columns');
  170. $sw_menu_float_type = $cat_model->getData('sw_menu_float_type');
  171. if(!$sw_menu_cat_columns){
  172. $sw_menu_cat_columns = 4;
  173. }
  174. $menu_type = $cat_model->getData('sw_menu_type');
  175. if(!$menu_type)
  176. $menu_type = $this->_megamenuConfig['general']['menu_type'];
  177. $custom_style = '';
  178. if($menu_type=="staticwidth")
  179. $custom_style = ' style="width: 500px;"';
  180. $sw_menu_static_width = $cat_model->getData('sw_menu_static_width');
  181. if($menu_type=="staticwidth" && $sw_menu_static_width)
  182. $custom_style = ' style="width: '.$sw_menu_static_width.';"';
  183. $item_class = 'level0 ';
  184. $item_class .= $menu_type.' ';
  185. $menu_top_content = $cat_model->getData('sw_menu_block_top_content');
  186. $menu_left_content = $cat_model->getData('sw_menu_block_left_content');
  187. $menu_left_width = $cat_model->getData('sw_menu_block_left_width');
  188. if(!$menu_left_content || !$menu_left_width)
  189. $menu_left_width = 0;
  190. $menu_right_content = $cat_model->getData('sw_menu_block_right_content');
  191. $menu_right_width = $cat_model->getData('sw_menu_block_right_width');
  192. if(!$menu_right_content || !$menu_right_width)
  193. $menu_right_width = 0;
  194. $menu_bottom_content = $cat_model->getData('sw_menu_block_bottom_content');
  195. if($sw_menu_float_type)
  196. $sw_menu_float_type = 'fl-'.$sw_menu_float_type.' ';
  197. if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_top_content || $menu_left_content || $menu_right_content || $menu_bottom_content)))
  198. $item_class .= 'parent ';
  199. $html .= '<li class="ui-menu-item '.$item_class.$sw_menu_float_type.'">';
  200. if(count($children) > 0) {
  201. $html .= '<div class="open-children-toggle"></div>';
  202. }
  203. $html .= '<a href="'.$this->_categoryHelper->getCategoryUrl($category).'" class="level-top" title="'.$category->getName().'">';
  204. if ($sw_menu_icon_img)
  205. $html .= '<img class="menu-thumb-icon" src="' . $this->_helper->getBaseUrl().'catalog/category/' . $sw_menu_icon_img . '" alt="'.$category->getName().'"/>';
  206. elseif($sw_menu_font_icon)
  207. $html .= '<em class="menu-thumb-icon '.$sw_menu_font_icon.'"></em>';
  208. $html .= '<span>'.$category->getName().'</span>';
  209. if($sw_menu_cat_label)
  210. $html .= '<span class="cat-label cat-label-'.$sw_menu_cat_label.'">'.$this->_megamenuConfig['cat_labels'][$sw_menu_cat_label].'</span>';
  211. $html .= '</a>';
  212. if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_top_content || $menu_left_content || $menu_right_content || $menu_bottom_content))) {
  213. $html .= '<div class="level0 submenu"'.$custom_style.'>';
  214. if(($menu_type=="fullwidth" || $menu_type=="staticwidth")) {
  215. $html .= '<div class="container">';
  216. }
  217. if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_top_content) {
  218. $html .= '<div class="menu-top-block">'.$this->getBlockContent($menu_top_content).'</div>';
  219. }
  220. if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_left_content || $menu_right_content))) {
  221. $html .= '<div class="row">';
  222. if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_left_content && $menu_left_width > 0) {
  223. $html .= '<div class="menu-left-block col-md-'.$menu_left_width.'">'.$this->getBlockContent($menu_left_content).'</div>';
  224. }
  225. $html .= $this->getSubmenuItemsHtml($children, 1, $max_level, 12-$menu_left_width-$menu_right_width, $menu_type, $sw_menu_cat_columns);
  226. if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_right_content && $menu_right_width > 0) {
  227. $html .= '<div class="menu-right-block col-md-'.$menu_right_width.'">'.$this->getBlockContent($menu_right_content).'</div>';
  228. }
  229. $html .= '</div>';
  230. }
  231. if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_bottom_content) {
  232. $html .= '<div class="menu-bottom-block">'.$this->getBlockContent($menu_bottom_content).'</div>';
  233. }
  234. if(($menu_type=="fullwidth" || $menu_type=="staticwidth")) {
  235. $html .= '</div>';
  236. }
  237. $html .= '</div>';
  238. }
  239. $html .= '</li>';
  240. }
  241. }
  242. $html .= $this->getCustomBlockHtml('after');
  243. return $html;
  244. }
  245. }