vendor/crehler/mojebambino-engine/src/Subscriber/NavigationSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Storefront\Controller\SubNavigationController;
  4. use Crehler\MojeBambinoEngine\Util\Lifecycle\ActivateDeactivate;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  7. use Shopware\Core\Content\Category\Tree\TreeItem;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class NavigationSubscriber implements EventSubscriberInterface
  13. {
  14.     private SalesChannelRepositoryInterface $categoryRepository;
  15.     public function __construct(
  16.         SalesChannelRepositoryInterface $categoryRepository
  17.     )
  18.     {
  19.         $this->categoryRepository $categoryRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             NavigationLoadedEvent::class => 'onNavigationLoaded',
  25.         ];
  26.     }
  27.     public function onNavigationLoaded(NavigationLoadedEvent $event): void
  28.     {
  29.         $navigation $event->getNavigation();
  30.         $context $event->getSalesChannelContext();
  31.         $activeCategory $navigation->getActive();
  32.         if (null === $activeCategory->getParent() && $activeCategory->getParentId()) {
  33.             $activeCategory->setParent(
  34.                 $this->loadCategory($activeCategory->getParentId(), $context)
  35.             );
  36.         }
  37.         if (null !== $activeCategory->getParent()
  38.             && isset($activeCategory->getParent()->getCustomFields()[ActivateDeactivate::INTERMEDIATE_CATEGORY])
  39.             && ($activeCategory->getParent()->getCustomFields()[ActivateDeactivate::INTERMEDIATE_CATEGORY])) {
  40.             $activeCategory->getParent()->setParent(
  41.                 $this->loadCategory($activeCategory->getParent()->getParentId(), $context)
  42.             );
  43.         }
  44.         if (in_array(SubNavigationController::PRODUCT_CATEGORYarray_keys($activeCategory->getPlainBreadcrumb()))) {
  45.             if ($activeCategory->getChildCount() > 0) {
  46.                 $activeCategory->setChildren($this->loadCategory($activeCategory->getId(), $context)->getChildren());
  47.             }
  48.             foreach ($navigation->getTree() as $treeItem) {
  49.                 if (!in_array($treeItem->getCategory()->getId(), array_keys($activeCategory->getPlainBreadcrumb()))) continue;
  50.                 $this->loadIntermediateChildren($treeItem$activeCategory1$context);
  51.             }
  52.         }
  53.         //jeżeli aktywna kategoria ma dzieci to dołatować je do drzewa
  54.         if ($activeCategory->getChildCount() > 0) {
  55.             foreach ($navigation->getTree() as $treeItem) {
  56.                 if (!in_array($treeItem->getCategory()->getId(), array_keys($activeCategory->getPlainBreadcrumb()))) continue;
  57.                 $this->checkTreeChildren($treeItem$activeCategory$context);
  58.             }
  59.         }
  60.     }
  61.     private function loadCategory(string $idSalesChannelContext $context): CategoryEntity
  62.     {
  63.         $criteria = new Criteria([$id]);
  64.         $criteria
  65.             ->getAssociation('children')
  66.             ->addAssociation('media');
  67.         return $this->categoryRepository->search($criteria$context)->get($id);
  68.     }
  69.     private function loadIntermediateChildren(TreeItem $treeItemCategoryEntity $activeCategoryint $level$context)
  70.     {
  71.         if (!in_array($treeItem->getCategory()->getId(), array_keys($activeCategory->getPlainBreadcrumb()))
  72.             and (!in_array($treeItem->getCategory()->getId(), $activeCategory->getChildren()->getKeys()))
  73.         ) {
  74.             return;
  75.         }
  76.         if (!empty($activeCategory->getChildren()) and !in_array($treeItem->getCategory()->getId(), $activeCategory->getChildren()->getKeys())) {
  77.             foreach ($treeItem->getChildren() as $child) {
  78.                 $this->loadIntermediateChildren($child$activeCategory$level++, $context);
  79.             }
  80.             return;
  81.         }
  82.         if ($treeItem->getCategory()->getChildCount() != count($treeItem->getChildren())) {
  83.             $category $this->loadCategory($treeItem->getCategory()->getId(), $context);
  84.             $categoryIds array_keys($treeItem->getChildren());
  85.             foreach ($category->getChildren()->sortByPosition() as $child) {
  86.                 if (!in_array($child->getId(), $categoryIds)) {
  87.                     $treeItem->addChildren(new TreeItem($child, []));
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     private function checkTreeChildren(TreeItem $treeItemCategoryEntity $activeCategory$contextbool $force false)
  93.     {
  94.         if ($force) {
  95.             if ($treeItem->getCategory()->getChildCount() != count($treeItem->getChildren())) {
  96.                 $category $this->loadCategory($treeItem->getCategory()->getId(), $context);
  97.                 $categoryIds array_keys($treeItem->getChildren());
  98.                 foreach ($category->getChildren()->sortByPosition() as $child) {
  99.                     if (!$child->getActive() || !$child->getVisible()) {
  100.                         continue;
  101.                     }
  102.                     if (!in_array($child->getId(), $categoryIds)) {
  103.                         $treeItem->addChildren(new TreeItem($child, []));
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         if ((in_array($treeItem->getCategory()->getId(), array_keys($activeCategory->getPlainBreadcrumb()))
  109.                 and ($treeItem->getCategory()->getChildCount() > 0)
  110.             ) or $force) {
  111.             if (!$force && array_key_last($activeCategory->getPlainBreadcrumb()) === $treeItem->getCategory()->getId()) {
  112.                 $force true;
  113.             }
  114.             foreach ($treeItem->getChildren() as $child) {
  115.                 $this->checkTreeChildren($child$activeCategory$context$force);
  116.             }
  117.         }
  118.     }
  119. }