platform/src/Storefront/Controller/CmsController.php line 95

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