vendor/shopware/storefront/Controller/FormController.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\ContactForm\ContactFormService;
  4. use Shopware\Core\Content\Newsletter\NewsletterSubscriptionServiceInterface;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  7. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Framework\Captcha\Annotation\Captcha;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @RouteScope(scopes={"storefront"})
  14.  */
  15. class FormController extends StorefrontController
  16. {
  17.     const SUBSCRIBE 'subscribe';
  18.     const UNSUBSCRIBE 'unsubscribe';
  19.     /**
  20.      * @var ContactFormService
  21.      */
  22.     private $contactFormService;
  23.     /**
  24.      * @var NewsletterSubscriptionServiceInterface
  25.      */
  26.     private $newsletterService;
  27.     public function __construct(
  28.         ContactFormService $contactFormService,
  29.         NewsletterSubscriptionServiceInterface $newsletterService
  30.     ) {
  31.         $this->contactFormService $contactFormService;
  32.         $this->newsletterService $newsletterService;
  33.     }
  34.     /**
  35.      * @Route("/form/contact", name="frontend.form.contact.send", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  36.      * @Captcha
  37.      */
  38.     public function sendContactForm(RequestDataBag $dataSalesChannelContext $context): JsonResponse
  39.     {
  40.         $response = [];
  41.         try {
  42.             $message $this->contactFormService->sendContactForm($data$context);
  43.             if (!$message) {
  44.                 $message $this->trans('contact.success');
  45.             }
  46.             $response[] = [
  47.                 'type' => 'success',
  48.                 'alert' => $message,
  49.             ];
  50.         } catch (ConstraintViolationException $formViolations) {
  51.             $violations = [];
  52.             foreach ($formViolations->getViolations() as $violation) {
  53.                 $violations[] = $violation->getMessage();
  54.             }
  55.             $response[] = [
  56.                 'type' => 'danger',
  57.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  58.                     'type' => 'danger',
  59.                     'list' => $violations,
  60.                 ]),
  61.             ];
  62.         }
  63.         return new JsonResponse($response);
  64.     }
  65.     /**
  66.      * @Route("/form/newsletter", name="frontend.form.newsletter.register.handle", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  67.      * @Captcha
  68.      */
  69.     public function handleNewsletter(RequestDataBag $dataSalesChannelContext $context): JsonResponse
  70.     {
  71.         $subscribe $data->get('option') === self::SUBSCRIBE;
  72.         if ($subscribe) {
  73.             $response $this->handleSubscribe($data$context);
  74.         } else {
  75.             $response $this->handleUnsubscribe($data$context);
  76.         }
  77.         return new JsonResponse($response);
  78.     }
  79.     private function handleSubscribe(RequestDataBag $dataSalesChannelContext $context): array
  80.     {
  81.         try {
  82.             $this->newsletterService->subscribe($data$context);
  83.             $response[] = [
  84.                 'type' => 'success',
  85.                 'alert' => $this->trans('newsletter.subscriptionPersistedSuccess'),
  86.             ];
  87.             $response[] = [
  88.                 'type' => 'info',
  89.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  90.                     'type' => 'info',
  91.                     'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
  92.                 ]),
  93.             ];
  94.         } catch (ConstraintViolationException $exception) {
  95.             $errors = [];
  96.             foreach ($exception->getViolations() as $error) {
  97.                 $errors[] = $error->getMessage();
  98.             }
  99.             $response[] = [
  100.                 'type' => 'danger',
  101.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  102.                     'type' => 'danger',
  103.                     'list' => $errors,
  104.                 ]),
  105.             ];
  106.         } catch (\Exception $exception) {
  107.             $response[] = [
  108.                 'type' => 'danger',
  109.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  110.                     'type' => 'danger',
  111.                     'list' => [$this->trans('error.message-default')],
  112.                 ]),
  113.             ];
  114.         }
  115.         return $response;
  116.     }
  117.     private function handleUnsubscribe(RequestDataBag $dataSalesChannelContext $context): array
  118.     {
  119.         try {
  120.             $this->newsletterService->unsubscribe($data$context);
  121.             $response[] = [
  122.                 'type' => 'success',
  123.                 'alert' => $this->trans('newsletter.subscriptionRevokeSuccess'),
  124.             ];
  125.         } catch (ConstraintViolationException $exception) {
  126.             $errors = [];
  127.             foreach ($exception->getViolations() as $error) {
  128.                 $errors[] = $error->getMessage();
  129.             }
  130.             $response[] = [
  131.                 'type' => 'danger',
  132.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  133.                     'type' => 'danger',
  134.                     'list' => $errors,
  135.                 ]),
  136.             ];
  137.         } catch (\Exception $exception) {
  138.             $response = [];
  139.         }
  140.         return $response;
  141.     }
  142. }