custom/plugins/TonurSeoFilterLandingpages6/src/Storefront/Controller/LandingpageController.php line 64

Open in your IDE?
  1. <?php
  2. namespace Tonur\SeoFilterLandingpages\Storefront\Controller;
  3. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Controller\StorefrontController;
  11. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Tonur\SeoFilterLandingpages\Core\Content\Landingpage\SeoFilterLandingpageEntity;
  17. use Tonur\SeoFilterLandingpages\Storefront\Page\Landingpage\LandingpagePageLoader;
  18. /**
  19.  * @RouteScope(scopes={"storefront"})
  20.  */
  21. class LandingpageController extends StorefrontController
  22. {
  23.     /**
  24.      * @var LandingpagePageLoader
  25.      */
  26.     private $landingpagePageLoader;
  27.     /** @var SalesChannelRepository  */
  28.     private $seoFilterLandingpageRepo;
  29.     /**
  30.      * @var SeoUrlPlaceholderHandlerInterface
  31.      */
  32.     private $seoUrlPlaceholderHandler;
  33.     private SystemConfigService $systemConfigService;
  34.     public function __construct(
  35.         LandingpagePageLoader $landingpagePageLoader,
  36.         SalesChannelRepository $seoFilterLandingpageRepo,
  37.         SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
  38.         SystemConfigService $systemConfigService
  39.     )
  40.     {
  41.         $this->landingpagePageLoader $landingpagePageLoader;
  42.         $this->seoFilterLandingpageRepo $seoFilterLandingpageRepo;
  43.         $this->seoUrlPlaceholderHandler $seoUrlPlaceholderHandler;
  44.         $this->systemConfigService $systemConfigService;
  45.     }
  46.     /**
  47.      * @Route("/landingpage/{landingpageId}", name="frontend.repertus.landingpage.page", options={"seo"="true"}, methods={"GET"})
  48.      *
  49.      * @param Request $request
  50.      * @param SalesChannelContext $context
  51.      * @return Response
  52.      */
  53.     public function loadLandingpage(SalesChannelContext $contextRequest $request): Response
  54.     {
  55.         $page $this->landingpagePageLoader->load($request$context);
  56.         return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', ['page' => $page]);
  57.     }
  58.     /**
  59.      * @RouteScope(scopes={"storefront"})
  60.      *
  61.      * @Route("/widgets/landingpage/{navigationId}/check", name="frontend.repertus.landingpage.check", methods={"GET"}, defaults={"XmlHttpRequest": true})
  62.      * @param string $navigationId
  63.      * @param Request $request
  64.      * @param SalesChannelContext $salesChannelContext
  65.      * @return JsonResponse
  66.      */
  67.     public function checkForLandingpage(string $navigationIdRequest $requestSalesChannelContext $salesChannelContext): JsonResponse
  68.     {
  69.         $properties array_filter(explode('|'$request->query->get('properties''')));
  70.         $manufacturers array_filter(explode('|'$request->query->get('manufacturer''')));
  71.         $pagination array_filter(explode('|'$request->query->get('p''')));
  72.         $criteria = new Criteria();
  73.         $criteria
  74.             ->addAssociation('category')
  75.             ->addFilter(new EqualsFilter('category.id'$navigationId));
  76.         $entitySearchResult $this->seoFilterLandingpageRepo->search($criteria$salesChannelContext);
  77.         /** @var SeoFilterLandingpageEntity $landingpage */
  78.         $landingpage null;
  79.         if (empty($pagination) || (int)$pagination[0] === 1) {
  80.             /** @var SeoFilterLandingpageEntity $possibleLandingpage */
  81.             foreach ($entitySearchResult->getEntities() as $possibleLandingpage) {
  82.                 if ($this->isSameArray($properties$possibleLandingpage->getPropertyIds() ?? []) &&
  83.                     $this->isSameArray($manufacturers$possibleLandingpage->getManufacturerIds() ?? [])
  84.                 ) {
  85.                     $landingpage $possibleLandingpage;
  86.                     break;
  87.                 }
  88.             }
  89.         }
  90.         $response = ['isLandingpage' => false];
  91.         if ($landingpage) {
  92.             $response['isLandingpage'] = true;
  93.             $host $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
  94.                 . $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  95.             $url $this->seoUrlPlaceholderHandler->replace(
  96.                 $this->seoUrlPlaceholderHandler->generate(
  97.                     'frontend.repertus.landingpage.page',
  98.                     ['landingpageId' => $landingpage->getId()]
  99.                 ),
  100.                 $host,
  101.                 $salesChannelContext
  102.             );
  103.             $this->addOrderParam($url$request$salesChannelContext);
  104.             $this->addShippingFreeParam($url$request$salesChannelContext);
  105.             $this->addMinPriceParam($url$request$salesChannelContext);
  106.             $this->addMaxPriceParam($url$request$salesChannelContext);
  107.             $response['url'] = $url;
  108.         }
  109.         return new JsonResponse($response);
  110.     }
  111.     private function isSameArray(array $array1, array $array2)
  112.     {
  113.         return
  114.             count($array1) === count($array2) &&
  115.             empty(array_diff($array1$array2));
  116.     }
  117.     private function addOrderParam(string &$urlRequest $requestSalesChannelContext $context): void
  118.     {
  119.         $order $request->get('order');
  120.         if (!$order) {
  121.             return;
  122.         }
  123.         $defaultProductSorting $this->systemConfigService->getString(
  124.             'core.listing.defaultSorting',
  125.             $context->getSalesChannelId()
  126.         );
  127.         if ($defaultProductSorting && strcmp($order$defaultProductSorting) === 0) {
  128.             return;
  129.         }
  130.         $data = ['order' => $order];
  131.         $url .= (strpos($url,'?') !== false '&' '?') . http_build_query($data);
  132.     }
  133.     private function addShippingFreeParam(string &$urlRequest $requestSalesChannelContext $salesChannelContext)
  134.     {
  135.         $param $request->get('shipping-free');
  136.         if (!$param) {
  137.             return;
  138.         }
  139.         $data = ['shipping-free' => $param];
  140.         $url .= (strpos($url,'?') !== false '&' '?') . http_build_query($data);
  141.     }
  142.     private function addMinPriceParam(string &$urlRequest $requestSalesChannelContext $salesChannelContext)
  143.     {
  144.         $param $request->get('min-price');
  145.         if (!$param) {
  146.             return;
  147.         }
  148.         $data = ['min-price' => $param];
  149.         $url .= (strpos($url,'?') !== false '&' '?') . http_build_query($data);
  150.     }
  151.     private function addMaxPriceParam(string &$urlRequest $requestSalesChannelContext $salesChannelContext)
  152.     {
  153.         $param $request->get('max-price');
  154.         if (!$param) {
  155.             return;
  156.         }
  157.         $data = ['max-price' => $param];
  158.         $url .= (strpos($url,'?') !== false '&' '?') . http_build_query($data);
  159.     }
  160. }