vendor/dvdw/directory/src/Content/Product/Cms/ShopDirectoryCmsElementResolver.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DvdwDirectory\Content\Product\Cms;
  3. use DvdwDirectory\Content\Category\Service\ShopCategoryIdLoaderInterface;
  4. use DvdwDirectory\Content\Product\Struct\FilterByParticipationStruct;
  5. use DvdwDirectory\Content\Product\Struct\FilterByHasPromotionStruct;
  6. use DvdwDirectory\Content\Product\Struct\FilterByPaidStruct;
  7. use DvdwDirectory\Content\Product\Struct\ProductListingLimitStruct;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  9. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  10. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  11. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  12. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  13. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  14. use Shopware\Core\Content\Product\SalesChannel\Listing\AbstractProductListingRoute;
  15. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingFeaturesSubscriber;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class ShopDirectoryCmsElementResolver extends AbstractCmsElementResolver
  20. {
  21.     private AbstractProductListingRoute $listingRoute;
  22.     private ShopCategoryIdLoaderInterface $categoryIdLoader;
  23.     public function __construct(
  24.         AbstractProductListingRoute $listingRoute,
  25.         ShopCategoryIdLoaderInterface $categoryIdLoader
  26.     )
  27.     {
  28.         $this->listingRoute $listingRoute;
  29.         $this->categoryIdLoader $categoryIdLoader;
  30.     }
  31.     public function getType(): string
  32.     {
  33.         return 'shop-directory';
  34.     }
  35.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  36.     {
  37.         return null;
  38.     }
  39.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  40.     {
  41.         $data = new ProductListingStruct();
  42.         $slot->setData($data);
  43.         $request $resolverContext->getRequest();
  44.         $this->restrictFilters($slot$request);
  45.         if ($this->isCustomSorting($slot)) {
  46.             $this->restrictSortings($request$slot);
  47.             $this->addDefaultSorting($request$slot);
  48.         }
  49.         $context $resolverContext->getSalesChannelContext();
  50.         $category $this->categoryIdLoader->load($request$context);
  51.         $navigationId $category['categoryId'];
  52.         $requestedSlot $category['slotConfig'] !== null $category['slotConfig'] : $slot;
  53.         $this->extendContext($requestedSlot$context);
  54.         $listing $this->listingRoute
  55.             ->load($navigationId$request$context, new Criteria())
  56.             ->getResult();
  57.         $data->setListing($listing);
  58.     }
  59.     private function isCustomSorting(CmsSlotEntity $slot): bool
  60.     {
  61.         $config $slot->getTranslation('config');
  62.         return $config['useCustomSorting']['value'] ?? false;
  63.     }
  64.     private function addDefaultSorting(Request $requestCmsSlotEntity $slot): void
  65.     {
  66.         if ($request->get('order')) {
  67.             return;
  68.         }
  69.         $config $slot->getTranslation('config');
  70.         if (isset($config['defaultSorting']['value'])) {
  71.             $request->request->set('order'$config['defaultSorting']['value']);
  72.             return;
  73.         }
  74.         // if we have no specific order given at this point, set the order to be the highest priority available sorting
  75.         if ($request->get('availableSortings')) {
  76.             $availableSortings $request->get('availableSortings');
  77.             arsort($availableSortingsSORT_DESC SORT_NUMERIC);
  78.             $request->request->set('order'array_key_first($availableSortings));
  79.         }
  80.     }
  81.     private function restrictSortings(Request $requestCmsSlotEntity $slot): void
  82.     {
  83.         $config $slot->getTranslation('config');
  84.         if (!isset($config['availableSortings']['value'])) {
  85.             return;
  86.         }
  87.         $request->request->set('availableSortings'$config['availableSortings']['value']);
  88.     }
  89.     private function restrictFilters(CmsSlotEntity $slotRequest $request): void
  90.     {
  91.         // set up the default behavior
  92.         $defaults = ['manufacturer-filter''rating-filter''shipping-free-filter''price-filter''property-filter'];
  93.         $request->request->set(ProductListingFeaturesSubscriber::PROPERTY_GROUP_IDS_REQUEST_PARAMnull);
  94.         $config $slot->get('config');
  95.         if (isset($config['propertyWhitelist']['value']) && count($config['propertyWhitelist']['value']) > 0) {
  96.             $request->request->set(ProductListingFeaturesSubscriber::PROPERTY_GROUP_IDS_REQUEST_PARAM$config['propertyWhitelist']['value']);
  97.         }
  98.         if (!isset($config['filters']['value'])) {
  99.             return;
  100.         }
  101.         // apply config settings
  102.         $config explode(','$config['filters']['value']);
  103.         foreach ($defaults as $filter) {
  104.             if (in_array($filter$configtrue)) {
  105.                 continue;
  106.             }
  107.             $request->request->set($filterfalse);
  108.         }
  109.     }
  110.     private function extendContext(CmsSlotEntity $slotSalesChannelContext $context): void
  111.     {
  112.         $config $slot->get('config');
  113.         $filterByParticipation $config['filterByParticipation']['value'] ?? false;
  114.         $filterByHasPromotion $config['filterByHasPromotion']['value'] ?? false;
  115.         $filterByPaid $config['filterByPaid']['value'] ?? false;
  116.         $limit $config['limit']['value'] ?? 25;
  117.         $context->addExtension(
  118.             FilterByParticipationStruct::KEY,
  119.             new FilterByParticipationStruct($filterByParticipation)
  120.         );
  121.         $context->addExtension(
  122.             FilterByHasPromotionStruct::KEY,
  123.             new FilterByHasPromotionStruct($filterByHasPromotion)
  124.         );
  125.         $context->addExtension(
  126.             FilterByPaidStruct::KEY,
  127.             new FilterByPaidStruct($filterByPaid)
  128.         );
  129.         $context->addExtension(
  130.             ProductListingLimitStruct::KEY,
  131.             new ProductListingLimitStruct($limit)
  132.         );
  133.     }
  134. }