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

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