CategoryCollection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Smartwave\Porto\Block;
  3. class CategoryCollection extends \Magento\Framework\View\Element\Template
  4. {
  5. protected $_categoryHelper;
  6. protected $categoryFlatConfig;
  7. protected $topMenu;
  8. /**
  9. * @param \Magento\Framework\View\Element\Template\Context $context
  10. * @param \Magento\Catalog\Helper\Category $categoryHelper
  11. * @param array $data
  12. */
  13. public function __construct(
  14. \Magento\Framework\View\Element\Template\Context $context,
  15. \Magento\Catalog\Helper\Category $categoryHelper,
  16. \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
  17. \Magento\Theme\Block\Html\Topmenu $topMenu
  18. ) {
  19. $this->_categoryHelper = $categoryHelper;
  20. $this->categoryFlatConfig = $categoryFlatState;
  21. $this->topMenu = $topMenu;
  22. parent::__construct($context);
  23. }
  24. /**
  25. * Return categories helper
  26. */
  27. public function getCategoryHelper()
  28. {
  29. return $this->_categoryHelper;
  30. }
  31. /**
  32. * Return categories helper
  33. * getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
  34. * example getHtml('level-top', 'submenu', 0)
  35. */
  36. public function getHtml()
  37. {
  38. return $this->topMenu->getHtml();
  39. }
  40. /**
  41. * Retrieve current store categories
  42. *
  43. * @param bool|string $sorted
  44. * @param bool $asCollection
  45. * @param bool $toLoad
  46. * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\Resource\Category\Collection|array
  47. */
  48. public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
  49. {
  50. return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
  51. }
  52. /**
  53. * Retrieve child store categories
  54. *
  55. */
  56. public function getChildCategories($category)
  57. {
  58. if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
  59. $subcategories = (array)$category->getChildrenNodes();
  60. } else {
  61. $subcategories = $category->getChildren();
  62. }
  63. return $subcategories;
  64. }
  65. public function getChildCategoryHtml($category, $icon_open_class="porto-icon-plus-squared", $icon_close_class="porto-icon-minus-squared") {
  66. $html = '';
  67. if($childrenCategories = $this->getChildCategories($category)) {
  68. $html .= '<ul>';
  69. $i = 0;
  70. foreach($childrenCategories as $childrenCategory) {
  71. if (!$childrenCategory->getIsActive()) {
  72. continue;
  73. }
  74. $i++;
  75. $html .= '<li><a href="'.$this->_categoryHelper->getCategoryUrl($childrenCategory).'">'.$childrenCategory->getName().'</a>';
  76. $html .= $this->getChildCategoryHtml($childrenCategory, $icon_open_class, $icon_close_class);
  77. $html .= '</li>';
  78. }
  79. $html .= '</ul>';
  80. if($i > 0)
  81. $html .= '<a href="javascript:void(0)" class="expand-icon"><em class="'.$icon_open_class.'"></em></a>';
  82. }
  83. return $html;
  84. }
  85. public function getCategorySidebarHtml($icon_open_class="porto-icon-plus-squared", $icon_close_class="porto-icon-minus-squared") {
  86. $html = '';
  87. $categories = $this->getStoreCategories(true,false,true);
  88. $html .= '<ul class="category-sidebar">';
  89. foreach($categories as $category) {
  90. if (!$category->getIsActive()) {
  91. continue;
  92. }
  93. $html .= '<li>';
  94. $html .= '<a href="'.$this->_categoryHelper->getCategoryUrl($category).'">'.$category->getName().'</a>';
  95. $html .= $this->getChildCategoryHtml($category, $icon_open_class, $icon_close_class);
  96. $html .= '</li>';
  97. }
  98. $html .= '</ul>';
  99. $html .= '<script type="text/javascript">
  100. require([
  101. \'jquery\'
  102. ], function ($) {
  103. $(".category-sidebar li > .expand-icon").click(function(){
  104. if($(this).parent().hasClass("opened")){
  105. $(this).parent().children("ul").slideUp();
  106. $(this).parent().removeClass("opened");
  107. $(this).children(".'.$icon_close_class.'").removeClass("'.$icon_close_class.'").addClass("'.$icon_open_class.'");
  108. } else {
  109. $(this).parent().children("ul").slideDown();
  110. $(this).parent().addClass("opened");
  111. $(this).children(".'.$icon_open_class.'").removeClass("'.$icon_open_class.'").addClass("'.$icon_close_class.'");
  112. }
  113. });
  114. });
  115. </script>';
  116. return $html;
  117. }
  118. }