src/Controller/ContactController.php line 47

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.     private $appRoot;
  18.     protected $entityManager;
  19.     public function __construct(RouterInterface $routerEntityManagerInterface $entityManager$projectDir)
  20.     {
  21.         $this->router $router;
  22.         $this->appRoot $projectDir;
  23.         $this->entityManager $entityManager;
  24.     }
  25.     public function contactAction(Request $requestMailerInterface $mailer)
  26.     {
  27.         $action $this->router->generate('contact_us');
  28.         $form $this->createForm(ContactType::class,new Mail, [
  29.             'action' => $action,
  30.             'method' => 'POST',
  31.         ]);
  32.         $form->handleRequest($request);
  33.         if($form->isSubmitted() && $form->isValid()) {
  34.             $mail $form->getData();
  35.             $this->sendContactMail($mailer$mail);
  36.             //$event = new ContactFormSubmitedEvent($mail);
  37.             //$this->eventDispatcher->dispatch($event,'contact.form_submited');   
  38.             return $this->redirect($this->router->generate('contact_us'));         
  39.         }
  40.         $view $this->viewManager->load('page/contact_form.html.twig');
  41.         $view['form'] = $form->createView();     
  42.         return new Response($view->render());
  43.     }
  44.     public function checkContactAction(Request $request$routeDocument=nullMailerInterface $mailer)
  45.     {
  46.         $contactForm $this->createForm(new ContactType(), []);
  47.         $contactForm->handleRequest($request);
  48.         if($contactForm->isSubmitted() && !$contactForm->isValid()) {
  49.             $this->flash->setErrors($this->getFormErrors($contactForm));
  50.             return $this->redirect($this->router->generate('contact_us'), 301);
  51.         }
  52.         $data $contactForm->getData();
  53.         $this->sendContactMail($mailer$data);
  54.         $this->flash->setSuccess('Message sent successfully');
  55.         return $this->redirect($this->router->generate('contact_us'));        
  56.     }
  57.     private function sendContactMail($mailer$mail)
  58.     {
  59.         $email = (new TemplatedEmail())
  60.             ->from(new Address('mailer@cmc-ghana.org''Contact Form'))
  61.             ->to('info@cmc-ghana.com')
  62.             ->subject($mail->getSubject())
  63.             ->htmlTemplate('mail/contact_message.html.twig')
  64.             ->context([
  65.                 "mail"=>$mail
  66.             ])
  67.         ;
  68.         $mailer->send($email);
  69.     }   
  70. }