Data.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © 2018 Porto. All rights reserved.
  4. */
  5. namespace Smartwave\Megamenu\Helper;
  6. use Magento\Framework\View\Result\PageFactory;
  7. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  8. {
  9. protected $_objectManager;
  10. protected $_categoryHelper;
  11. protected $_categoryFactory;
  12. protected $_categoryFlatConfig;
  13. protected $_filterProvider;
  14. protected $resultPageFactory;
  15. public function __construct(
  16. \Magento\Framework\App\Helper\Context $context,
  17. \Magento\Catalog\Helper\Category $categoryHelper,
  18. \Magento\Catalog\Model\CategoryFactory $categoryFactory,
  19. \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
  20. \Magento\Framework\ObjectManagerInterface $objectManager,
  21. \Magento\Cms\Model\Template\FilterProvider $filterProvider,
  22. PageFactory $resultPageFactory,
  23. \Magento\Store\Model\StoreManagerInterface $storeManager
  24. ) {
  25. $this->_storeManager = $storeManager;
  26. $this->_objectManager= $objectManager;
  27. $this->_categoryFactory = $categoryFactory;
  28. $this->_categoryFlatConfig = $categoryFlatState;
  29. $this->_categoryHelper = $categoryHelper;
  30. $this->resultPageFactory = $resultPageFactory;
  31. $this->_filterProvider = $filterProvider;
  32. parent::__construct($context);
  33. }
  34. public function getBaseUrl($url_type=\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
  35. {
  36. return $this->_storeManager->getStore()->getBaseUrl($url_type);
  37. }
  38. public function getConfig($config_path)
  39. {
  40. return $this->scopeConfig->getValue(
  41. $config_path,
  42. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  43. );
  44. }
  45. public function getModel($model) {
  46. return $this->_objectManager->create($model);
  47. }
  48. public function getCurrentStore() {
  49. return $this->_storeManager->getStore();
  50. }
  51. public function getFirstLevelCategories($sorted = false, $asCollection = false, $toLoad = true) {
  52. return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
  53. }
  54. public function getCategoryModel($id)
  55. {
  56. $_category = $this->_categoryFactory->create();
  57. $_category->load($id);
  58. return $_category;
  59. }
  60. public function getActiveChildCategories($category)
  61. {
  62. $children = [];
  63. $subcategories = $category->getChildrenCategories();
  64. foreach($subcategories as $category) {
  65. if (!$category->getIsActive()) {
  66. continue;
  67. }
  68. $children[] = $category;
  69. }
  70. return $children;
  71. }
  72. public function getBlockContent($content = '') {
  73. if(!$this->_filterProvider)
  74. return $content;
  75. return $this->_filterProvider->getBlockFilter()->filter(trim($content));
  76. }
  77. public function getResultPageFactory() {
  78. return $this->resultPageFactory;
  79. }
  80. public function getSubmenuItemsHtml($children, $level=0, $max_level=2)
  81. {
  82. $html = '';
  83. if(count($children) && ($level < $max_level)){
  84. $html .= '<ul';
  85. if($level == 0)
  86. $html .=' class="columns5"';
  87. $html .= '>';
  88. foreach($children as $child) {
  89. $html .= '<li class="menu-item level'.$level;
  90. $activeChildren = $this->getActiveChildCategories($child);
  91. if(count($activeChildren))
  92. $html .= ' menu-parent-item';
  93. $html .= '">';
  94. $html .='<a href="'.$child->getUrl().'" data-id="'.$child->getId().'"><span>'.$child->getName().'</span></a>';
  95. if(count($activeChildren))
  96. $html .= $this->getSubmenuItemsHtml($activeChildren, $level+1, $max_level);
  97. $html .= '</li>';
  98. }
  99. $html .= '</ul>';
  100. }
  101. return $html;
  102. }
  103. }