![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Theme/Block/Html/ |
<?php /** * Copyright (c) 2019 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * cnc_theme_m2 * <[email protected]> */ declare(strict_types=1); namespace Cnc\Theme\Block\Html; use Cnc\Catalog\Model\Config as CatalogConfig; use Magento\Catalog\Helper\Output as OutputHelper; use Magento\Catalog\Model\Category\FileInfo as CategoryFileInfo; use Magento\Framework\Data\Tree\Node; use Magento\Framework\Data\Tree\Node\Collection; use Magento\Framework\Data\Tree\NodeFactory; use Magento\Framework\Data\TreeFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\UrlInterface; use Magento\Framework\View\Element\Template; use Magento\Theme\Block\Html\Topmenu as OriginalTopmenu; use Staempfli\ImageResizer\Model\Resizer as ImageResizer; class Topmenu extends OriginalTopmenu { /** * @var OutputHelper */ private $outputHelper; /** * Topmenu constructor. * * @param Template\Context $context * @param NodeFactory $nodeFactory * @param TreeFactory $treeFactory * @param OutputHelper $outputHelper * @param array $data */ public function __construct( Template\Context $context, NodeFactory $nodeFactory, TreeFactory $treeFactory, OutputHelper $outputHelper, array $data = [] ) { parent::__construct($context, $nodeFactory, $treeFactory, $data); $this->outputHelper = $outputHelper; } /** * @override in order to add icon related DOM/class * * Recursively generates top menu html from data that is specified in $menuTree * * @param Node $menuTree * @param string $childrenWrapClass * @param int $limit * @param array $colBrakes * @return string * @throws LocalizedException * @throws NoSuchEntityException */ protected function _getHtml( Node $menuTree, $childrenWrapClass, $limit, array $colBrakes = [] ) { $html = ''; $children = $menuTree->getChildren(); $childLevel = $this->getChildLevel($menuTree->getLevel()); $this->removeChildrenWithoutActiveParent($children, $childLevel); $counter = 1; $childrenCount = $children->count(); $parentPositionClass = $menuTree->getPositionClass(); $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-'; if ($menuTree->getLevel() !== null) { $sorted = []; /** @var Node $child */ foreach ($children as $child) { $sorted[strtolower($child->getName())] = $child; } ksort($sorted); } else { $sorted = $children; } /** @var Node $child */ foreach ($sorted as $child) { $child->setLevel($childLevel); $child->setIsFirst($counter === 1); $child->setIsLast($counter === $childrenCount); $child->setPositionClass($itemPositionClassPrefix . $counter); $outermostClassCode = ''; $outermostClass = $menuTree->getOutermostClass(); if ($childLevel === 0 && $outermostClass) { $outermostClassCode = ' class="' . $outermostClass . '" '; $this->setCurrentClass($child, $outermostClass); } if ($this->shouldAddNewColumn($colBrakes, $counter)) { $html .= '</ul></li><li class="column"><ul>'; } // @override START $iconHtml = ''; if ($child->getLevel() == 1 && ($iconCssClass = $child->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS)) ) { $iconHtml = '<span class="icon icon-' . $iconCssClass . '"></span>'; } $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>'; $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '>' . $iconHtml . '<span>' . $this->escapeHtml($child->getName()) . '</span>' . '</a>' . $this->_addSubMenu( $child, $childLevel, $childrenWrapClass, $limit ) . '</li>'; // @override END $counter++; } if (is_array($colBrakes) && !empty($colBrakes) && $limit) { $html = '<li class="column"><ul>' . $html . '</ul></li>'; } return $html; } /** * @override not modified, only because of private scope * * Retrieve child level based on parent level * * @param int $parentLevel * * @return int */ private function getChildLevel($parentLevel): int { return $parentLevel === null ? 0 : $parentLevel + 1; } /** * @override not modified, only because of private scope * * Remove children from collection when the parent is not active * * @param Collection $children * @param int $childLevel * @return void */ private function removeChildrenWithoutActiveParent(Collection $children, int $childLevel): void { /** @var Node $child */ foreach ($children as $child) { if ($childLevel === 0 && $child->getData('is_parent_active') === false) { $children->delete($child); } } } /** * @override not modified, only because of private scope * * Set current class. * * @param Node $child * @param string $outermostClass */ private function setCurrentClass(Node $child, string $outermostClass): void { $currentClass = $child->getClass(); if (empty($currentClass)) { $child->setClass($outermostClass); } else { $child->setClass($currentClass . ' ' . $outermostClass); } } /** * @override not modified, only because of private scope * * Check if new column should be added. * * @param array $colBrakes * @param int $counter * @return bool */ private function shouldAddNewColumn(array $colBrakes, int $counter): bool { return count($colBrakes) && $colBrakes[$counter]['colbrake']; } /** * Add sub menu HTML code for current menu item * * @param Node $child * @param string $childLevel * @param string $childrenWrapClass * @param int $limit * * @return string * @throws NoSuchEntityException * @throws LocalizedException */ protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit): string { $html = ''; if (!$child->hasChildren()) { return $html; } $colStops = []; if ($childLevel == 0 && $limit) { $colStops = $this->_columnBrake($child->getChildren(), $limit); } if (($iconCssClass = $child->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS)) ) { $child->setData('class', $iconCssClass); } $html .= '<div class="' . $childrenWrapClass . '-container">'; $html .= '<ul class="level' . $childLevel . ' ' . $childrenWrapClass . '">'; $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops); $html .= '</ul>'; // @override START if ($child->getLevel() == 1) { $hasMarketing1 = $this->hasPushMarketing($child); $hasMarketing2 = $this->hasPushMarketing($child, 2); if ($hasMarketing1 || $hasMarketing2) { $html .= '<div class="push-marketing">'; if ($hasMarketing1) { $html .= $this->getPushMarktingHtml($child); } if ($hasMarketing2) { $html .= $this->getPushMarktingHtml($child, 2); } $html .= '</div>'; } } // @override END $html .= '</div>'; return $html; } /** * @param Node $menuItem * @param int $pushId * * @return bool */ private function hasPushMarketing(Node $menuItem, $pushId = 1): bool { if ($menuItem->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_PREFIX . $pushId) && $menuItem->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_PREFIX . $pushId) ) { return true; } return false; } /** * @param Node $menuItem * @param int $pushId * * @return string * @throws NoSuchEntityException * @throws LocalizedException */ private function getPushMarktingHtml(Node $menuItem, $pushId = 1): string { $html = '<div class="push-item push-' . $pushId . '">'; // Title $title = $menuItem->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_PREFIX . $pushId); $html .= '<div class="content"><span class="title">' . $this->_escaper->escapeHtml($title) . '</span>'; // Text $text = $menuItem->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_PREFIX . $pushId); $html .= '<p class="text">' . $this->_escaper->escapeHtml($text) . '</p></div>'; // Image // @todo add lazy loading if ($image = $menuItem->getData(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_PREFIX . $pushId)) { $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(); $image = $mediaUrl . ltrim($image, '/'); /** @var ImageResizer $imageResizer */ $imageResizer = $this->getImageResizer(); // @todo Define final dimensions $resizedImageUrl = $imageResizer->resizeAndGetUrl( $image, 126, 126, [ 'quality' => 95 ] ); $html .= '<div class="picture"><img class="img" src="' . $this->_escaper->escapeHtmlAttr($resizedImageUrl) . '" alt="' . $this->_escaper->escapeHtmlAttr($title) . '" title="' . $this->_escaper->escapeHtmlAttr($title) . '"></div>'; } // CTA $textAttributeCode = CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_PREFIX . $pushId; $linkAttributeCode = CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_PREFIX . $pushId; if (($ctaText = $menuItem->getData($textAttributeCode)) && ($ctaLink = $menuItem->getData($linkAttributeCode)) ) { $hrefAttributeValue = $this->outputHelper->categoryAttribute( $menuItem, $ctaLink, CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_PREFIX . $pushId ); $html .= '<a href="' . $hrefAttributeValue . '" title="' . $this->_escaper->escapeHtmlAttr($ctaText) . '"> ' . $this->_escaper->escapeHtml($ctaText) . ' </a>'; } $html .= '</div>'; return $html; } }