custom/static-plugins/CioItpThemeFilters/src/Controller/CompatibleWithController.php line 43

Open in your IDE?
  1. <?php
  2. namespace CioItpThemeFilters\Controller;
  3. use CioItpThemeFilters\CioItpThemeFilters;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Controller\StorefrontController;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Routing\RouterInterface;
  18. /**
  19.  * @RouteScope(scopes={"storefront"})
  20.  */
  21. class CompatibleWithController extends StorefrontController
  22. {
  23.     private EntityRepository $propertyGroupOptionRepository;
  24.     private RouterInterface $router;
  25.     private SystemConfigService $systemConfigService;
  26.     public function __construct(
  27.         EntityRepository    $propertyGroupOptionRepository,
  28.         RouterInterface     $router,
  29.         SystemConfigService $systemConfigService
  30.     ) {
  31.         $this->propertyGroupOptionRepository = $propertyGroupOptionRepository;
  32.         $this->router = $router;
  33.         $this->systemConfigService = $systemConfigService;
  34.     }
  35.     /**
  36.      * @Route("compatible/with/{propertyGroupOptionName}", name="compatible.with.filter", methods={"GET"})
  37.      */
  38.     public function setCookieAndRedirect(string $propertyGroupOptionName, SalesChannelContext $salesChannelContext, Request $request): Response
  39.     {
  40.         $propertyGroupOptionId = $this->propertyGroupOptionRepository->searchIds(
  41.             (new Criteria())->addFilter(new EqualsFilter('name', $propertyGroupOptionName)),
  42.             $salesChannelContext->getContext()
  43.         )->firstId();
  44.         if (!$propertyGroupOptionId) {
  45.             // Wenn die Option nicht gefunden wurde, zurück zur Startseite (im aktuellen SalesChannel)
  46.             $baseUrl = rtrim((string) $request->attributes->get('sw-storefront-url'), '/');
  47.             $path = $this->router->generate('frontend.home.page', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  48.             return new RedirectResponse($baseUrl . $path, Response::HTTP_FOUND);
  49.         }
  50.         $categoryId = $this->systemConfigService->get('CioItpThemeFilters.config.allProductsCategory', $salesChannelContext->getSalesChannelId());
  51.         $path = $categoryId
  52.             ? $this->router->generate('frontend.navigation.page', ['navigationId' => $categoryId], UrlGeneratorInterface::ABSOLUTE_PATH)
  53.             : $this->router->generate('frontend.home.page', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  54.         $baseUrl = rtrim((string) $request->attributes->get('sw-storefront-url'), '/');
  55.         $url = $baseUrl . $path;
  56.         $response = new Response('', Response::HTTP_FOUND);
  57.         $response->headers->set('Location', $url);
  58.         $response->headers->setCookie(
  59.             new Cookie(
  60.                 CioItpThemeFilters::FILTER_COOKIE_NAME,
  61.                 $propertyGroupOptionId,
  62.                 strtotime('+24 hours'),
  63.                 '/',
  64.                 null,
  65.                 false,
  66.                 false
  67.             )
  68.         );
  69.         return $response;
  70.     }
  71. }