src/Controller/ContactController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Boab\CmsBundle\Controller\BaseController;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Boab\CmsBundle\Controller\PublicControllerInterface;
  7. use App\Form\ContactType;
  8. use App\Model\Mail;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. class ContactController extends BaseController implements PublicControllerInterface
  15. {
  16.     protected $router;
  17.     protected $entityManager;
  18.     public function __construct(RouterInterface $routerEntityManagerInterface $entityManager)
  19.     {
  20.         $this->router $router;
  21.         $this->entityManager $entityManager;
  22.     }
  23.     public function contactAction(Request $requestMailerInterface $mailer)
  24.     {
  25.         $action $this->router->generate('contact_us');
  26.         $form $this->createForm(ContactType::class, new Mail, [
  27.             'action' => $action,
  28.             'method' => 'POST',
  29.         ]);
  30.         $form->handleRequest($request);
  31.         if($form->isSubmitted() && $form->isValid()) {
  32.             $mail $form->getData();
  33.             $this->sendContactMail($mailer$mail);
  34.             //$event = new ContactFormSubmitedEvent($mail);
  35.             //$this->eventDispatcher->dispatch($event,'contact.form_submited');   
  36.             return $this->redirect($this->router->generate('contact_us'));         
  37.         }
  38.         $view $this->viewManager->load('page/contact_form.html.twig');
  39.         $view['form'] = $form->createView();     
  40.         return new Response($view->render());
  41.     }
  42.     public function checkContactAction(Request $request$routeDocument=nullMailerInterface $mailer)
  43.     {
  44.         $contactForm $this->createForm(new ContactType(), []);
  45.         $contactForm->handleRequest($request);
  46.         if($contactForm->isSubmitted() && !$contactForm->isValid()) {
  47.             $this->flash->setErrors($this->getFormErrors($contactForm));
  48.             return $this->redirect($this->router->generate('contact_us'), 301);
  49.         }
  50.         $data $contactForm->getData();
  51.         $this->sendContactMail($mailer$data);
  52.         $this->flash->setSuccess('Message sent successfully');
  53.         return $this->redirect($this->router->generate('contact_us'));        
  54.     }
  55.     private function sendContactMail($mailer$mail)
  56.     {
  57.         $email = (new TemplatedEmail())
  58.             ->from(new Address('mailer@cmc-ghana.org''Contact Form'))
  59.             ->to('info@cmc-ghana.com')
  60.             ->subject($mail->getSubject())
  61.             ->htmlTemplate('mail/contact_message.html.twig')
  62.             ->context([
  63.                 "mail"=>$mail
  64.             ])
  65.         ;
  66.         $mailer->send($email);
  67.     }   
  68. }