vendor/shopware/core/Content/Category/SalesChannel/NavigationRoute.php line 146

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Category\CategoryCollection;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  15. use Shopware\Core\Framework\Log\Package;
  16. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  17. use Shopware\Core\Framework\Routing\Annotation\Entity;
  18. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  19. use Shopware\Core\Framework\Routing\Annotation\Since;
  20. use Shopware\Core\Framework\Uuid\Uuid;
  21. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  22. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. /**
  26.  * @Route(defaults={"_routeScope"={"store-api"}})
  27.  */
  28. #[Package('content')]
  29. class NavigationRoute extends AbstractNavigationRoute
  30. {
  31.     /**
  32.      * @var SalesChannelRepositoryInterface
  33.      */
  34.     private $categoryRepository;
  35.     /**
  36.      * @var Connection
  37.      */
  38.     private $connection;
  39.     /**
  40.      * @internal
  41.      */
  42.     public function __construct(
  43.         Connection $connection,
  44.         SalesChannelRepositoryInterface $repository
  45.     ) {
  46.         $this->categoryRepository $repository;
  47.         $this->connection $connection;
  48.     }
  49.     public function getDecorated(): AbstractNavigationRoute
  50.     {
  51.         throw new DecorationPatternException(self::class);
  52.     }
  53.     /**
  54.      * @Since("6.2.0.0")
  55.      * @Entity("category")
  56.      * @Route("/store-api/navigation/{activeId}/{rootId}", name="store-api.navigation", methods={"GET", "POST"})
  57.      */
  58.     public function load(
  59.         string $activeId,
  60.         string $rootId,
  61.         Request $request,
  62.         SalesChannelContext $context,
  63.         Criteria $criteria
  64.     ): NavigationRouteResponse {
  65.         $depth $request->query->getInt('depth'$request->request->getInt('depth'2));
  66.         $metaInfo $this->getCategoryMetaInfo($activeId$rootId);
  67.         $active $this->getMetaInfoById($activeId$metaInfo);
  68.         $root $this->getMetaInfoById($rootId$metaInfo);
  69.         // Validate the provided category is part of the sales channel
  70.         $this->validate($activeId$active['path'], $context);
  71.         $isChild $this->isChildCategory($activeId$active['path'], $rootId);
  72.         // If the provided activeId is not part of the rootId, a fallback to the rootId must be made here.
  73.         // The passed activeId is therefore part of another navigation and must therefore not be loaded.
  74.         // The availability validation has already been done in the `validate` function.
  75.         if (!$isChild) {
  76.             $activeId $rootId;
  77.         }
  78.         $categories = new CategoryCollection();
  79.         if ($depth 0) {
  80.             // Load the first two levels without using the activeId in the query
  81.             $categories $this->loadLevels($rootId, (int) $root['level'], $context, clone $criteria$depth);
  82.         }
  83.         // If the active category is part of the provided root id, we have to load the children and the parents of the active id
  84.         $categories $this->loadChildren($activeId$context$rootId$metaInfo$categories, clone $criteria);
  85.         return new NavigationRouteResponse($categories);
  86.     }
  87.     private function loadCategories(array $idsSalesChannelContext $contextCriteria $criteria): CategoryCollection
  88.     {
  89.         $criteria->setIds($ids);
  90.         $criteria->addAssociation('media');
  91.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  92.         /** @var CategoryCollection $missing */
  93.         $missing $this->categoryRepository->search($criteria$context)->getEntities();
  94.         return $missing;
  95.     }
  96.     private function loadLevels(string $rootIdint $rootLevelSalesChannelContext $contextCriteria $criteriaint $depth 2): CategoryCollection
  97.     {
  98.         $criteria->addFilter(
  99.             new ContainsFilter('path''|' $rootId '|'),
  100.             new RangeFilter('level', [
  101.                 RangeFilter::GT => $rootLevel,
  102.                 RangeFilter::LTE => $rootLevel $depth 1,
  103.             ])
  104.         );
  105.         $criteria->addAssociation('media');
  106.         $criteria->setLimit(null);
  107.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  108.         /** @var CategoryCollection $levels */
  109.         $levels $this->categoryRepository->search($criteria$context)->getEntities();
  110.         $this->addVisibilityCounts($rootId$rootLevel$depth$levels$context);
  111.         return $levels;
  112.     }
  113.     private function getCategoryMetaInfo(string $activeIdstring $rootId): array
  114.     {
  115.         $result $this->connection->fetchAllAssociative('
  116.             # navigation-route::meta-information
  117.             SELECT LOWER(HEX(`id`)), `path`, `level`
  118.             FROM `category`
  119.             WHERE `id` = :activeId OR `parent_id` = :activeId OR `id` = :rootId
  120.         ', ['activeId' => Uuid::fromHexToBytes($activeId), 'rootId' => Uuid::fromHexToBytes($rootId)]);
  121.         if (!$result) {
  122.             throw new CategoryNotFoundException($activeId);
  123.         }
  124.         return FetchModeHelper::groupUnique($result);
  125.     }
  126.     private function getMetaInfoById(string $id, array $metaInfo): array
  127.     {
  128.         if (!\array_key_exists($id$metaInfo)) {
  129.             throw new CategoryNotFoundException($id);
  130.         }
  131.         return $metaInfo[$id];
  132.     }
  133.     private function loadChildren(string $activeIdSalesChannelContext $contextstring $rootId, array $metaInfoCategoryCollection $categoriesCriteria $criteria): CategoryCollection
  134.     {
  135.         $active $this->getMetaInfoById($activeId$metaInfo);
  136.         unset($metaInfo[$rootId], $metaInfo[$activeId]);
  137.         $childIds array_keys($metaInfo);
  138.         // Fetch all parents and first-level children of the active category, if they're not already fetched
  139.         $missing $this->getMissingIds($activeId$active['path'], $childIds$categories);
  140.         if (empty($missing)) {
  141.             return $categories;
  142.         }
  143.         $categories->merge(
  144.             $this->loadCategories($missing$context$criteria)
  145.         );
  146.         return $categories;
  147.     }
  148.     /**
  149.      * @param array<string> $childIds
  150.      */
  151.     private function getMissingIds(string $activeId, ?string $path, array $childIdsCategoryCollection $alreadyLoaded): array
  152.     {
  153.         $parentIds array_filter(explode('|'$path ?? ''));
  154.         $haveToBeIncluded array_merge($childIds$parentIds, [$activeId]);
  155.         $included $alreadyLoaded->getIds();
  156.         $included array_flip($included);
  157.         return array_diff($haveToBeIncluded$included);
  158.     }
  159.     private function validate(string $activeId, ?string $pathSalesChannelContext $context): void
  160.     {
  161.         $ids array_filter([
  162.             $context->getSalesChannel()->getFooterCategoryId(),
  163.             $context->getSalesChannel()->getServiceCategoryId(),
  164.             $context->getSalesChannel()->getNavigationCategoryId(),
  165.         ]);
  166.         foreach ($ids as $id) {
  167.             if ($this->isChildCategory($activeId$path$id)) {
  168.                 return;
  169.             }
  170.         }
  171.         throw new CategoryNotFoundException($activeId);
  172.     }
  173.     private function isChildCategory(string $activeId, ?string $pathstring $rootId): bool
  174.     {
  175.         if ($rootId === $activeId) {
  176.             return true;
  177.         }
  178.         if ($path === null) {
  179.             return false;
  180.         }
  181.         if (mb_strpos($path'|' $rootId '|') !== false) {
  182.             return true;
  183.         }
  184.         return false;
  185.     }
  186.     private function addVisibilityCounts(string $rootIdint $rootLevelint $depthCategoryCollection $levelsSalesChannelContext $context): void
  187.     {
  188.         $counts = [];
  189.         foreach ($levels as $category) {
  190.             if (!$category->getActive() || !$category->getVisible()) {
  191.                 continue;
  192.             }
  193.             $parentId $category->getParentId();
  194.             $counts[$parentId] = $counts[$parentId] ?? 0;
  195.             ++$counts[$parentId];
  196.         }
  197.         foreach ($levels as $category) {
  198.             $category->setVisibleChildCount($counts[$category->getId()] ?? 0);
  199.         }
  200.         // Fetch additional level of categories for counting visible children that are NOT included in the original query
  201.         $criteria = new Criteria();
  202.         $criteria->addFilter(
  203.             new ContainsFilter('path''|' $rootId '|'),
  204.             new EqualsFilter('level'$rootLevel $depth 1),
  205.             new EqualsFilter('active'true),
  206.             new EqualsFilter('visible'true)
  207.         );
  208.         $criteria->addAggregation(
  209.             new TermsAggregation('category-ids''parentId'nullnull, new CountAggregation('visible-children-count''id'))
  210.         );
  211.         $termsResult $this->categoryRepository
  212.             ->aggregate($criteria$context)
  213.             ->get('category-ids');
  214.         if (!($termsResult instanceof TermsResult)) {
  215.             return;
  216.         }
  217.         foreach ($termsResult->getBuckets() as $bucket) {
  218.             $key $bucket->getKey();
  219.             if ($key === null) {
  220.                 continue;
  221.             }
  222.             $parent $levels->get($key);
  223.             if ($parent instanceof CategoryEntity) {
  224.                 $parent->setVisibleChildCount($bucket->getCount());
  225.             }
  226.         }
  227.     }
  228. }