<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Controller;
use App\Form\Type\ContactType;
use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Webmozart\Assert\Assert;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\UserBundle\Form\Model\ChangePassword;
use Sylius\Bundle\UserBundle\Form\Model\PasswordReset;
use Sylius\Bundle\UserBundle\Form\Model\PasswordResetRequest;
use Sylius\Bundle\UserBundle\Form\Type\UserChangePasswordType;
use Sylius\Bundle\UserBundle\Form\Type\UserRequestPasswordResetType;
use Sylius\Bundle\UserBundle\Form\Type\UserResetPasswordType;
use Sylius\Bundle\UserBundle\UserEvents;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Twig\Environment; // Add this import
final class ContactController extends ResourceController
{
private RouterInterface $router;
private FormFactoryInterface $formFactory;
private Environment $twig; // Change the type to Twig's Environment
private ChannelContextInterface $channelContext;
private CustomerContextInterface $customerContext;
private ContactEmailManagerInterface $contactEmailManager;
public function __construct(
RouterInterface $router,
FormFactoryInterface $formFactory,
Environment $twig,
ChannelContextInterface $channelContext,
CustomerContextInterface $customerContext,
ContactEmailManagerInterface $contactEmailManager
) {
$this->router = $router;
$this->formFactory = $formFactory;
$this->twig = $twig; // Assign Twig's Environment
$this->channelContext = $channelContext;
$this->customerContext = $customerContext;
$this->contactEmailManager = $contactEmailManager;
}
/**
* @param Request $request
*
* @return Response
*/
public function askForNewStock(Request $request): Response
{
$response = $this->forward('App\Controller\ContactController::requestAction', [$request
]);
return $response;
}
/**
* @param Request $request
*
* @return Response
*/
public function requestAction(Request $request): Response
{
$formType = $this->getSyliusAttribute($request, 'form', ContactType::class);
$form = $this->formFactory->create($formType, null, $this->getFormOptions());
//$form = $this->createForm(ContactType::class);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$data = $form->getData();
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
Assert::isInstanceOf($channel, ChannelInterface::class);
$contactEmail = $channel->getContactEmail();
if (null === $contactEmail) {
$errorMessage = $this->getSyliusAttribute(
$request,
'error_flash',
'sylius.contact.request_error'
);
/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('error', $errorMessage);
return new RedirectResponse($request->headers->get('referer'));
}
$this->contactEmailManager->sendContactRequest($data, [$contactEmail]);
$successMessage = $this->getSyliusAttribute(
$request,
'success_flash',
'sylius.contact.request_success'
);
/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('success', $successMessage);
$redirectRoute = $this->getSyliusAttribute($request, 'redirect', 'referer');
return new RedirectResponse($this->router->generate($redirectRoute));
}
$template = $this->getSyliusAttribute($request, 'template', '@SyliusShop/Contact/request.html.twig');
// Use Twig's render method instead of templatingEngine->renderResponse
return new Response($this->twig->render($template, [
'form' => $form->createView(),
]));
}
private function getSyliusAttribute(Request $request, string $attributeName, ?string $default): ?string
{
$attributes = $request->attributes->get('_sylius');
return $attributes[$attributeName] ?? $default;
}
private function getFormOptions(): array
{
$customer = $this->customerContext->getCustomer();
if (null === $customer) {
return [];
}
return ['email' => $customer->getEmail()];
}
/**
* {@inheritdoc}
*/
protected function addTranslatedFlash(string $type, string $message): void
{
$translator = $this->container->get('translator');
$this->container->get('session')->getFlashBag()->add($type, @$this->translator->trans($message, [], 'flashes'));
}
}