<?php
namespace CioItpThemeFilters\Controller;
use CioItpThemeFilters\CioItpThemeFilters;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* @RouteScope(scopes={"storefront"})
*/
class CompatibleWithController extends StorefrontController
{
private EntityRepository $propertyGroupOptionRepository;
private RouterInterface $router;
private SystemConfigService $systemConfigService;
public function __construct(
EntityRepository $propertyGroupOptionRepository,
RouterInterface $router,
SystemConfigService $systemConfigService
) {
$this->propertyGroupOptionRepository = $propertyGroupOptionRepository;
$this->router = $router;
$this->systemConfigService = $systemConfigService;
}
/**
* @Route("compatible/with/{propertyGroupOptionName}", name="compatible.with.filter", methods={"GET"})
*/
public function setCookieAndRedirect(string $propertyGroupOptionName, SalesChannelContext $salesChannelContext, Request $request): Response
{
$propertyGroupOptionId = $this->propertyGroupOptionRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', $propertyGroupOptionName)),
$salesChannelContext->getContext()
)->firstId();
if (!$propertyGroupOptionId) {
// Wenn die Option nicht gefunden wurde, zurück zur Startseite (im aktuellen SalesChannel)
$baseUrl = rtrim((string) $request->attributes->get('sw-storefront-url'), '/');
$path = $this->router->generate('frontend.home.page', [], UrlGeneratorInterface::ABSOLUTE_PATH);
return new RedirectResponse($baseUrl . $path, Response::HTTP_FOUND);
}
$categoryId = $this->systemConfigService->get('CioItpThemeFilters.config.allProductsCategory', $salesChannelContext->getSalesChannelId());
$path = $categoryId
? $this->router->generate('frontend.navigation.page', ['navigationId' => $categoryId], UrlGeneratorInterface::ABSOLUTE_PATH)
: $this->router->generate('frontend.home.page', [], UrlGeneratorInterface::ABSOLUTE_PATH);
$baseUrl = rtrim((string) $request->attributes->get('sw-storefront-url'), '/');
$url = $baseUrl . $path;
$response = new Response('', Response::HTTP_FOUND);
$response->headers->set('Location', $url);
$response->headers->setCookie(
new Cookie(
CioItpThemeFilters::FILTER_COOKIE_NAME,
$propertyGroupOptionId,
strtotime('+24 hours'),
'/',
null,
false,
false
)
);
return $response;
}
}