vendor/shopware/storefront/Controller/CmsController.php line 84

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  4. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  5. use Shopware\Core\Content\Cms\SalesChannel\AbstractCmsRoute;
  6. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  7. use Shopware\Core\Content\Product\SalesChannel\FindVariant\AbstractFindProductVariantRoute;
  8. use Shopware\Core\Content\Product\SalesChannel\Listing\AbstractProductListingRoute;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Feature;
  11. use Shopware\Core\Framework\Routing\Annotation\Since;
  12. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Event\SwitchBuyBoxVariantEvent;
  15. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  16. use Shopware\Storefront\Page\Cms\CmsPageLoadedHook;
  17. use Shopware\Storefront\Page\Product\Configurator\ProductCombinationFinder;
  18. use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. /**
  26.  * @Route(defaults={"_routeScope"={"storefront"}})
  27.  *
  28.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  29.  */
  30. class CmsController extends StorefrontController
  31. {
  32.     private AbstractCmsRoute $cmsRoute;
  33.     private AbstractCategoryRoute $categoryRoute;
  34.     private AbstractProductListingRoute $listingRoute;
  35.     private AbstractProductDetailRoute $productRoute;
  36.     private ProductReviewLoader $productReviewLoader;
  37.     private EventDispatcherInterface $eventDispatcher;
  38.     private AbstractFindProductVariantRoute $findVariantRoute;
  39.     /**
  40.      * @deprecated tag:v6.5.0 - will be removed
  41.      */
  42.     private ProductCombinationFinder $productCombinationFinder;
  43.     /**
  44.      * @internal
  45.      */
  46.     public function __construct(
  47.         AbstractCmsRoute $cmsRoute,
  48.         AbstractCategoryRoute $categoryRoute,
  49.         AbstractProductListingRoute $listingRoute,
  50.         AbstractProductDetailRoute $productRoute,
  51.         ProductReviewLoader $productReviewLoader,
  52.         AbstractFindProductVariantRoute $findVariantRoute,
  53.         ProductCombinationFinder $productCombinationFinder,
  54.         EventDispatcherInterface $eventDispatcher
  55.     ) {
  56.         $this->cmsRoute $cmsRoute;
  57.         $this->categoryRoute $categoryRoute;
  58.         $this->listingRoute $listingRoute;
  59.         $this->productRoute $productRoute;
  60.         $this->productReviewLoader $productReviewLoader;
  61.         $this->eventDispatcher $eventDispatcher;
  62.         $this->findVariantRoute $findVariantRoute;
  63.         $this->productCombinationFinder $productCombinationFinder;
  64.     }
  65.     /**
  66.      * @Since("6.0.0.0")
  67.      * Route for cms data (used in XmlHttpRequest)
  68.      *
  69.      * @HttpCache()
  70.      * @Route("/widgets/cms/{id}", name="frontend.cms.page", methods={"GET", "POST"}, defaults={"id"=null, "XmlHttpRequest"=true})
  71.      */
  72.     public function page(?string $idRequest $requestSalesChannelContext $salesChannelContext): Response
  73.     {
  74.         if (!$id) {
  75.             throw new MissingRequestParameterException('id');
  76.         }
  77.         $page $this->cmsRoute->load($id$request$salesChannelContext)->getCmsPage();
  78.         $this->hook(new CmsPageLoadedHook($page$salesChannelContext));
  79.         $response $this->renderStorefront('@Storefront/storefront/page/content/detail.html.twig', ['cmsPage' => $page]);
  80.         $response->headers->set('x-robots-tag''noindex');
  81.         return $response;
  82.     }
  83.     /**
  84.      * @Since("6.0.0.0")
  85.      * Route to load a cms page which assigned to the provided navigation id.
  86.      * Navigation id is required to load the slot config for the navigation
  87.      *
  88.      * @Route("/widgets/cms/navigation/{navigationId}", name="frontend.cms.navigation.page", methods={"GET", "POST"}, defaults={"navigationId"=null, "XmlHttpRequest"=true})
  89.      */
  90.     public function category(?string $navigationIdRequest $requestSalesChannelContext $salesChannelContext): Response
  91.     {
  92.         if (!$navigationId) {
  93.             throw new MissingRequestParameterException('navigationId');
  94.         }
  95.         $category $this->categoryRoute->load($navigationId$request$salesChannelContext)->getCategory();
  96.         $page $category->getCmsPage();
  97.         if (!$page) {
  98.             throw new PageNotFoundException('');
  99.         }
  100.         $this->hook(new CmsPageLoadedHook($page$salesChannelContext));
  101.         $response $this->renderStorefront('@Storefront/storefront/page/content/detail.html.twig', ['cmsPage' => $page]);
  102.         $response->headers->set('x-robots-tag''noindex');
  103.         return $response;
  104.     }
  105.     /**
  106.      * @Since("6.0.0.0")
  107.      * @HttpCache()
  108.      *
  109.      * Route to load the listing filters
  110.      *
  111.      * @Route("/widgets/cms/navigation/{navigationId}/filter", name="frontend.cms.navigation.filter", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_routeScope"={"storefront"}})
  112.      */
  113.     public function filter(string $navigationIdRequest $requestSalesChannelContext $context): Response
  114.     {
  115.         // Allows to fetch only aggregations over the gateway.
  116.         $request->request->set('only-aggregations'true);
  117.         // Allows to convert all post-filters to filters. This leads to the fact that only aggregation values are returned, which are combinable with the previous applied filters.
  118.         $request->request->set('reduce-aggregations'true);
  119.         $listing $this->listingRoute
  120.             ->load($navigationId$request$context, new Criteria())
  121.             ->getResult();
  122.         $mapped = [];
  123.         foreach ($listing->getAggregations() as $aggregation) {
  124.             $mapped[$aggregation->getName()] = $aggregation;
  125.         }
  126.         $response = new JsonResponse($mapped);
  127.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'1');
  128.         $response->headers->set('x-robots-tag''noindex');
  129.         return $response;
  130.     }
  131.     /**
  132.      * @Since("6.4.0.0")
  133.      * @HttpCache()
  134.      *
  135.      * Route to load the cms element buy box product config which assigned to the provided product id.
  136.      * Product id is required to load the slot config for the buy box
  137.      *
  138.      * @Route("/widgets/cms/buybox/{productId}/switch", name="frontend.cms.buybox.switch", methods={"GET"}, defaults={"productId"=null, "XmlHttpRequest"=true, "_routeScope"={"storefront"}})
  139.      */
  140.     public function switchBuyBoxVariant(string $productIdRequest $requestSalesChannelContext $context): Response
  141.     {
  142.         /** @var string $elementId */
  143.         $elementId $request->query->get('elementId');
  144.         /** @var array|null $options */
  145.         $options json_decode($request->query->get('options'''), true);
  146.         if (Feature::isActive('v6.5.0.0')) {
  147.             $variantResponse $this->findVariantRoute->load(
  148.                 $productId,
  149.                 new Request(
  150.                     [
  151.                         'switchedGroup' => $request->query->get('switched'),
  152.                         'options' => $options ?? [],
  153.                     ]
  154.                 ),
  155.                 $context
  156.             );
  157.             $newProductId $variantResponse->getFoundCombination()->getVariantId();
  158.         } else {
  159.             $finderResponse $this->productCombinationFinder->find(
  160.                 $productId,
  161.                 $request->query->get('switched'),
  162.                 $options ?? [],
  163.                 $context
  164.             );
  165.             $newProductId $finderResponse->getVariantId();
  166.         }
  167.         $result $this->productRoute->load($newProductId$request$context, new Criteria());
  168.         $product $result->getProduct();
  169.         $configurator $result->getConfigurator();
  170.         $request->request->set('parentId'$product->getParentId());
  171.         $request->request->set('productId'$product->getId());
  172.         $reviews $this->productReviewLoader->load($request$context);
  173.         $reviews->setParentId($product->getParentId() ?? $product->getId());
  174.         $event = new SwitchBuyBoxVariantEvent($elementId$product$configurator$request$context);
  175.         $this->eventDispatcher->dispatch($event);
  176.         $response $this->renderStorefront('@Storefront/storefront/component/buy-widget/buy-widget.html.twig', [
  177.             'product' => $product,
  178.             'configuratorSettings' => $configurator,
  179.             'totalReviews' => $reviews->getTotalReviews(),
  180.             'elementId' => $elementId,
  181.         ]);
  182.         $response->headers->set('x-robots-tag''noindex');
  183.         return $response;
  184.     }
  185. }