<?php
namespace App\Controller;
use App\Events;
use Symfony\Component\HttpFoundation\Request;
use Boab\CmsBundle\Controller\BaseController;
use Symfony\Component\Routing\RouterInterface;
use Boab\CmsBundle\Controller\PublicControllerInterface;
use App\Form\ContactType;
use App\Model\Mail;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class ContactController extends BaseController implements PublicControllerInterface
{
protected $router;
protected $entityManager;
public function __construct(
RouterInterface $router,
EntityManagerInterface $entityManager,
private LoggerInterface $logger)
{
$this->router = $router;
$this->entityManager = $entityManager;
}
public function contactAction(Request $request, MailerInterface $mailer)
{
$action = $this->router->generate('app.contact_us');
$form = $this->createForm(ContactType::class, new Mail, [
'action' => $action,
'method' => 'POST',
]);
$view = $this->viewManager->load('page/contact_form.html.twig');
$view['form'] = $form->createView();
return new Response($view->render());
}
public function checkContactAction(Request $request, MailerInterface $mailer)
{
$form = $this->createForm(ContactType::class, new Mail);
$form->handleRequest($request);
if($form->isSubmitted() && !$form->isValid()) {
$this->flash->setErrors($this->getFormErrors($form));
return $this->redirect($this->router->generate('contact_us'), 301);
}
try{
$mail = $form->getData();
$this->eventDispatcher->dispatch($mail, Events::VISITOR_SEND_CONTACT);
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
}
$this->flash->setSuccess('Message sent successfully');
return $this->redirect($this->router->generate('contact_us', ['status'=>'success']));
}
}