<?php
namespace NetInventors\NetiNextStoreLocator\Storefront\Controller;
use NetInventors\NetiNextStoreLocator\Storefront\Page\Store\Detail\StoreDetailPageLoader;
use NetInventors\NetiNextStoreLocator\Storefront\Page\Store\Listing\StoreListingPageLoader;
use NetInventors\NetiNextStoreLocator\Storefront\Route\ContactRoute;
use NetInventors\NetiNextStoreLocator\Storefront\Route\GetStoresRoute;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class StoreLocatorController extends StorefrontController
{
private StoreDetailPageLoader $storeDetailPageLoader;
private StoreListingPageLoader $storeListingPageLoader;
private SystemConfigService $systemConfig;
private ContactRoute $contactRoute;
private GetStoresRoute $getStoresRoute;
public function __construct(
StoreDetailPageLoader $storeDetailPageLoader,
StoreListingPageLoader $storeListingPageLoader,
SystemConfigService $systemConfig,
ContactRoute $contactRoute,
GetStoresRoute $getStoresRoute
) {
$this->storeDetailPageLoader = $storeDetailPageLoader;
$this->storeListingPageLoader = $storeListingPageLoader;
$this->systemConfig = $systemConfig;
$this->contactRoute = $contactRoute;
$this->getStoresRoute = $getStoresRoute;
}
/**
* @Route("/StoreLocator", name="frontend.store_locator.index", methods={"GET"})
*/
public function index(Request $request, SalesChannelContext $context): Response
{
if (
$this->systemConfig->get('NetiNextStoreLocator.config.active', $context->getSalesChannel()->getId())
!== true
) {
throw new NotFoundHttpException();
}
$page = $this->storeListingPageLoader->load($request, $context);
return $this->renderStorefront(
'storefront/store_locator/index.twig',
[
'page' => $page,
]
);
}
/**
* @HttpCache()
* @Route("/StoreLocator/getStores", name="frontend.store_locator.get_stores", methods={"GET"},
* defaults={"XmlHttpRequest"=true})
*/
public function getStores(Request $request, SalesChannelContext $context): JsonResponse
{
if (
$this->systemConfig->get('NetiNextStoreLocator.config.active', $context->getSalesChannel()->getId())
!== true
) {
throw new NotFoundHttpException();
}
return $this->getStoresRoute->getStores($request, $context);
}
/**
* @Route("/StoreLocator/detail/{id}", name="frontend.store_locator.detail", methods={"GET"})
*/
public function detail(Request $request, SalesChannelContext $context): Response
{
if (
$this->systemConfig->get('NetiNextStoreLocator.config.active', $context->getSalesChannel()->getId())
!== true
) {
throw new NotFoundHttpException();
}
$page = $this->storeDetailPageLoader->load($request, $context);
return $this->renderStorefront(
'storefront/store_locator/detail.twig',
[
'page' => $page,
]
);
}
/**
* @deprecated Use `\NetInventors\NetiNextStoreLocator\Storefront\Route\ContactRoute::contact` instead.
* @Route("/StoreLocator/contact", name="frontend.store_locator.contact", methods={"POST"},
* defaults={"XmlHttpRequest"=true})
*/
public function contact(Request $request, SalesChannelContext $context): JsonResponse
{
if (
$this->systemConfig->get('NetiNextStoreLocator.config.active', $context->getSalesChannel()->getId())
!== true
) {
throw new NotFoundHttpException();
}
return $this->contactRoute->contact($request, $context);
}
/**
* @Route("/StoreLocator/cms", name="frontend.store_locator.cms", methods={"POST"})
*
*/
public function cms(Request $request, SalesChannelContext $context): RedirectResponse
{
$data = $request->request->all();
$redirectResponse = $this->redirectToRoute('frontend.store_locator.index');
$url = $redirectResponse->getTargetUrl() . "?search=" . $data['search'];
if (isset($data['additionalParams']) && "" !== $data['additionalParams']) {
$url .= "&" . $data['additionalParams'];
}
$redirectResponse->setTargetUrl($url);
return $redirectResponse;
}
}