custom/plugins/MaxiaTaxSwitch6/src/Storefront/Controller/TaxSwitchController.php line 98

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaTaxSwitch6\Storefront\Controller;
  3. use Maxia\MaxiaTaxSwitch6\Storefront\Service\TaxSwitchService;
  4. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Controller\StorefrontController;
  9. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @RouteScope(scopes={"storefront"})
  17.  */
  18. class TaxSwitchController extends StorefrontController
  19. {
  20.     /**
  21.      * @var TaxSwitchService
  22.      */
  23.     private $taxSwitchService;
  24.     /**
  25.      * @param TaxSwitchService $taxSwitchService
  26.      */
  27.     public function __construct(TaxSwitchService $taxSwitchService)
  28.     {
  29.         $this->taxSwitchService $taxSwitchService;
  30.     }
  31.     /**
  32.      * @param Request $request
  33.      *
  34.      * @RouteScope(scopes={"storefront"})
  35.      * @NoStore
  36.      * @Route(
  37.      *     "/maxia-tax-switch",
  38.      *     name="frontend.plugins.maxia-tax-switch.update",
  39.      *     options={"seo"="false"},
  40.      *     methods={"GET"},
  41.      *     defaults={"XmlHttpRequest"=true}
  42.      * )
  43.      * @return \Symfony\Component\HttpFoundation\Response
  44.      */
  45.     public function updateSetting(Request $request)
  46.     {
  47.         /** @var SalesChannelContext $context */
  48.         $context $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  49.         $config $this->taxSwitchService->getContext($context);
  50.         if (!$config['pluginEnabled']) {
  51.             throw new NotFoundHttpException();
  52.         }
  53.         $isNet = (bool)$request->query->get('net');
  54.         $this->taxSwitchService->setDisplayNet($context$isNet);
  55.         $returnUrl $this->getReturnUrl($request$config['urlParameterName']);
  56.         if ($request->isXmlHttpRequest()) {
  57.             $response = new JsonResponse(['success' => true'returnUrl' => $returnUrl]);
  58.         } else {
  59.             $response $this->redirect($returnUrl);
  60.         }
  61.         $response->headers->set('X-Robots-Tag''noindex,nofollow');
  62.         return $response;
  63.     }
  64.     /**
  65.      * @param Request $request
  66.      *
  67.      * @RouteScope(scopes={"storefront"})
  68.      * @NoStore
  69.      * @Route(
  70.      *     "/maxia-tax-switch/popup",
  71.      *     name="frontend.plugins.maxia-tax-switch.popup",
  72.      *     options={"seo"="false"},
  73.      *     methods={"GET"},
  74.      *     defaults={"XmlHttpRequest"=true}
  75.      * )
  76.      * @return Response
  77.      */
  78.     public function showPopup(Request $request)
  79.     {
  80.         /** @var SalesChannelContext $context */
  81.         $context $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  82.         $config $this->taxSwitchService->getContext($context);
  83.         if ($config['pluginEnabled']) {
  84.             $context->addExtension('maxiaTaxSwitch', new ArrayEntity($config));
  85.             if (!$config['popupTestMode']) {
  86.                 $hidePopupFlag $request->getSession()->get('maxia-tax-switch-hide-popup'false);
  87.                 // hide modal if customer is logged in
  88.                 if ($context->getCustomer() !== null) {
  89.                     $hidePopupFlag true;
  90.                 }
  91.                 $request->getSession()->set('maxia-tax-switch-hide-popup'true);
  92.             } else {
  93.                 $hidePopupFlag false;
  94.             }
  95.             return new JsonResponse([
  96.                 'sessionHideModalFlag' => $hidePopupFlag,
  97.                 'template' => $this->renderView(
  98.                     '@Storefront/storefront/plugins/maxia_tax_switch/modal.html.twig',
  99.                     ['context' => $context]
  100.                 )
  101.             ]);
  102.         }
  103.         return $this->redirect($request->getBaseUrl());
  104.     }
  105.     /**
  106.      * Returns the absolute redirect url based on the 'return' parameter or referer of the request.
  107.      *
  108.      * @param Request $request
  109.      * @return string
  110.      */
  111.     protected function getReturnUrl(Request $request)
  112.     {
  113.         /** @var SalesChannelContext $context */
  114.         $context $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  115.         $config $this->taxSwitchService->getContext($context);
  116.         $returnPath $request->get('return');
  117.         if (!$returnPath && $request->headers->get('Referer')) {
  118.             $returnPath $request->headers->get('Referer');
  119.         }
  120.         $url parse_url($returnPath);
  121.         if (!empty($url['host']) && $url['host'] !== $request->getHttpHost()) {
  122.             $url $request->get('return''/');
  123.         }
  124.         $url['host'] = $request->getHttpHost();
  125.         $url['scheme'] = $request->getScheme();
  126.         if ($request->getPort()) {
  127.             $url['port'] = $request->getPort();
  128.         }
  129.         if (isset($url['query'])) {
  130.             parse_str($url['query'] ?? ''$query);
  131.             if ($query) {
  132.                 if (isset($query[$config['urlParameterName']])) {
  133.                     unset($query[$config['urlParameterName']]);
  134.                 }
  135.                 $url['query'] = http_build_query($query);
  136.             }
  137.         }
  138.         return $this->buildUrl($url);
  139.     }
  140.     /**
  141.      * Build URL string from parse_url result.
  142.      *
  143.      * @param array $url
  144.      * @return bool
  145.      */
  146.     protected function buildUrl(array $url)
  147.     {
  148.         if (!$url) {
  149.             return false;
  150.         }
  151.         $newUrl '';
  152.         if (isset($url['scheme'])) {
  153.             $newUrl .= $url['scheme'] . '://';
  154.         }
  155.         if (isset($url['user'])) {
  156.             $newUrl .= $url['user'];
  157.             if (isset($url['pass'])) {
  158.                 $newUrl .= ':'.$url['pass'];
  159.             }
  160.             $newUrl .= '@';
  161.         }
  162.         if (isset($url['host'])) {
  163.             $newUrl .= $url['host'];
  164.         }
  165.         if (isset($url['port'])) {
  166.             $newUrl .= ':'.$url['port'];
  167.         }
  168.         if (isset($url['path'])) {
  169.             $newUrl .= $url['path'];
  170.         }
  171.         if (isset($url['query'])) {
  172.             parse_str($url['query'], $params);
  173.             if ($params) {
  174.                 $newUrl .= '?'.http_build_query($params);
  175.             }
  176.         }
  177.         if (isset($url['fragment'])) {
  178.             $newUrl .= '#'.$url['fragment'];
  179.         }
  180.         return $newUrl;
  181.     }
  182. }