src/Controller/ContactController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Form\Type\ContactFormType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ContactController extends AbstractController {
  17.     /**
  18.      * @Route("/contact", name="contact_page")
  19.      */
  20.     public function contactPage(MailerInterface $mailerRequest $request)
  21.     {
  22.         $defaultData = ['message' => 'Type your message here'];
  23.         $form $this->createFormBuilder()
  24.             ->add('firstName'TextType::class,
  25.                 [
  26.                     'label' => 'First Name',
  27.                     'required' => true,
  28.                 ]
  29.             )
  30.             ->add('lastName'TextType::class,
  31.                 [
  32.                     'label' => 'Last Name',
  33.                     'required' => true,
  34.                 ]
  35.             )
  36.             ->add('email'EmailType::class,
  37.                 [
  38.                     'label' => 'Email Address',
  39.                     'required' => true,
  40.                 ]
  41.             )
  42.             ->add('mobile'EmailType::class,
  43.                 [
  44.                     'label' => 'Mobile #',
  45.                     'required' => true,
  46.                 ]
  47.             )
  48.             ->add('comments'TextareaType::class,
  49.                 [
  50.                     'label' => 'Comments',
  51.                     'required' => true,
  52.                 ]
  53.             )
  54.             ->add(
  55.                 'created',
  56.                 HiddenType::class, [
  57.                     'data' => date('Y-m-d H:i:s')
  58.                 ]
  59.             )
  60.             ->add('save'SubmitType::class, [
  61.                 'attr' => [
  62.                     'class' => 'con-btn']
  63.                 ]
  64.             )
  65.             ->getForm();
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             // data is an array with "name", "email", and "message" keys
  69.             $data $form->getData();
  70.             $contact = new Contact();
  71.             $now = new \DateTime();
  72.             $contact->setFirstName($data['firstName']);
  73.             $contact->setLastName($data['lastName']);
  74.             $contact->setEmail($data['email']);
  75.             $contact->setMobile($data['mobile']);
  76.             $contact->setComments($data['comments']);
  77.             $contact->setCreated($data['created']);
  78.             $em $this->getDoctrine()->getManager();
  79.             $em->persist($contact);
  80.             $em->flush();
  81.             // Send Email
  82.             $body '<table style="padding: 8px; border-collapse: collapse; border: none; font-family: arial">';
  83.             $body .= '<tr><td colspan="2"><img src="https://huguenottunnel.nerdw.com/images/sanral-preferred-logo_cmyk.png"></td></tr>';
  84.             $body .= '<tr><td colspan="2">&nbsp;</td></tr>';
  85.             $body .= '<tr><td colspan="2">Thank you for submitting your information. We will be in touch soon.</td></tr>';
  86.             $body .= '<tr><td colspan="2">&nbsp;</td></tr>';
  87.             foreach($data as $key => $value){
  88.                 $body .= '<tr>';
  89.                 $body .= '    <td><b>'$key .'</b></td>';
  90.                 $body .= '    <td>'ucwords(str_replace('_'' ',$value)) .'</td>';
  91.                 $body .= '</tr>';
  92.             }
  93.             $body .= '</table>';
  94.             $email = (new Email())
  95.                 ->from('SANRAL <support@nerdw.com>')
  96.                 ->to($data['email'])
  97.                 ->subject('Your enquiry has been received')
  98.                 ->html($body);
  99.             $mailer->send($email);;
  100.             return $this->render('frontend/contact.html.twig',
  101.                 [
  102.                     'form' => $form->createView(),
  103.                     'created' => date('Y-m-d H:i:s'),
  104.                     'message' => 1,
  105.                     'page' => 'contact'
  106.                 ]);
  107.         }
  108.         return $this->render('frontend/contact.html.twig',
  109.             [
  110.                 'form' => $form->createView(),
  111.                 'created' => date('Y-m-d H:i:s'),
  112.                 'message' => 0,
  113.                 'page' => 'contact'
  114.             ]);
  115.     }
  116. }