src/Controller/ContactController.php line 95

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Controller;
  12. use App\Form\Type\ContactType;
  13. use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
  14. use Sylius\Component\Channel\Context\ChannelContextInterface;
  15. use Sylius\Component\Core\Model\ChannelInterface;
  16. use Sylius\Component\Customer\Context\CustomerContextInterface;
  17. use Symfony\Component\Form\FormFactoryInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  22. use Symfony\Component\Routing\RouterInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  24. use Webmozart\Assert\Assert;
  25. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  26. use Sylius\Bundle\UserBundle\Form\Model\ChangePassword;
  27. use Sylius\Bundle\UserBundle\Form\Model\PasswordReset;
  28. use Sylius\Bundle\UserBundle\Form\Model\PasswordResetRequest;
  29. use Sylius\Bundle\UserBundle\Form\Type\UserChangePasswordType;
  30. use Sylius\Bundle\UserBundle\Form\Type\UserRequestPasswordResetType;
  31. use Sylius\Bundle\UserBundle\Form\Type\UserResetPasswordType;
  32. use Sylius\Bundle\UserBundle\UserEvents;
  33. use Sylius\Component\User\Model\UserInterface;
  34. use Sylius\Component\User\Repository\UserRepositoryInterface;
  35. use Sylius\Component\User\Security\Generator\GeneratorInterface;
  36. use Symfony\Component\EventDispatcher\GenericEvent;
  37. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  38. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  39. use Twig\Environment; // Add this import
  40. final class ContactController extends ResourceController
  41. {
  42. private RouterInterface $router;
  43. private FormFactoryInterface $formFactory;
  44. private Environment $twig; // Change the type to Twig's Environment
  45. private ChannelContextInterface $channelContext;
  46. private CustomerContextInterface $customerContext;
  47. private ContactEmailManagerInterface $contactEmailManager;
  48. public function __construct(
  49. RouterInterface $router,
  50. FormFactoryInterface $formFactory,
  51. Environment $twig,
  52. ChannelContextInterface $channelContext,
  53. CustomerContextInterface $customerContext,
  54. ContactEmailManagerInterface $contactEmailManager
  55. ) {
  56. $this->router = $router;
  57. $this->formFactory = $formFactory;
  58. $this->twig = $twig; // Assign Twig's Environment
  59. $this->channelContext = $channelContext;
  60. $this->customerContext = $customerContext;
  61. $this->contactEmailManager = $contactEmailManager;
  62. }
  63. /**
  64. * @param Request $request
  65. *
  66. * @return Response
  67. */
  68. public function askForNewStock(Request $request): Response
  69. {
  70. $response = $this->forward('App\Controller\ContactController::requestAction', [$request
  71. ]);
  72. return $response;
  73. }
  74. /**
  75. * @param Request $request
  76. *
  77. * @return Response
  78. */
  79. public function requestAction(Request $request): Response
  80. {
  81. $formType = $this->getSyliusAttribute($request, 'form', ContactType::class);
  82. $form = $this->formFactory->create($formType, null, $this->getFormOptions());
  83. //$form = $this->createForm(ContactType::class);
  84. if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  85. $data = $form->getData();
  86. /** @var ChannelInterface $channel */
  87. $channel = $this->channelContext->getChannel();
  88. Assert::isInstanceOf($channel, ChannelInterface::class);
  89. $contactEmail = $channel->getContactEmail();
  90. if (null === $contactEmail) {
  91. $errorMessage = $this->getSyliusAttribute(
  92. $request,
  93. 'error_flash',
  94. 'sylius.contact.request_error'
  95. );
  96. /** @var FlashBagInterface $flashBag */
  97. $flashBag = $request->getSession()->getBag('flashes');
  98. $flashBag->add('error', $errorMessage);
  99. return new RedirectResponse($request->headers->get('referer'));
  100. }
  101. $this->contactEmailManager->sendContactRequest($data, [$contactEmail]);
  102. $successMessage = $this->getSyliusAttribute(
  103. $request,
  104. 'success_flash',
  105. 'sylius.contact.request_success'
  106. );
  107. /** @var FlashBagInterface $flashBag */
  108. $flashBag = $request->getSession()->getBag('flashes');
  109. $flashBag->add('success', $successMessage);
  110. $redirectRoute = $this->getSyliusAttribute($request, 'redirect', 'referer');
  111. return new RedirectResponse($this->router->generate($redirectRoute));
  112. }
  113. $template = $this->getSyliusAttribute($request, 'template', '@SyliusShop/Contact/request.html.twig');
  114. // Use Twig's render method instead of templatingEngine->renderResponse
  115. return new Response($this->twig->render($template, [
  116. 'form' => $form->createView(),
  117. ]));
  118. }
  119. private function getSyliusAttribute(Request $request, string $attributeName, ?string $default): ?string
  120. {
  121. $attributes = $request->attributes->get('_sylius');
  122. return $attributes[$attributeName] ?? $default;
  123. }
  124. private function getFormOptions(): array
  125. {
  126. $customer = $this->customerContext->getCustomer();
  127. if (null === $customer) {
  128. return [];
  129. }
  130. return ['email' => $customer->getEmail()];
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. protected function addTranslatedFlash(string $type, string $message): void
  136. {
  137. $translator = $this->container->get('translator');
  138. $this->container->get('session')->getFlashBag()->add($type, @$this->translator->trans($message, [], 'flashes'));
  139. }
  140. }