src/Controller/Customer/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Entity\Phone;
  4. use App\Form\SimplePhoneType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Validator\Validator\ValidatorInterface;
  12. class ContactController extends AbstractController
  13. {
  14.     /**
  15.      * Kontaktni formular
  16.      * @param Request $request
  17.      * @param ValidatorInterface $validator
  18.      * @param MailerInterface $mailer
  19.      * @return Response
  20.      * @throws TransportExceptionInterface
  21.      */
  22.     public function contact(Request $requestValidatorInterface $validatorMailerInterface $mailer): Response
  23.     {
  24.         $order = new Phone();
  25.         $form $this->createForm(SimplePhoneType::class, $order);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted()) {
  28.             $errors $validator->validate($order);
  29.             if (count($errors) > 0) {
  30.                 /*
  31.                  * Uses a __toString method on the $errors variable which is a
  32.                  * ConstraintViolationList object. This gives us a nice string
  33.                  * for debugging.
  34.                  */
  35.                 $errorsString = (string)$errors;
  36.                 return new Response($errorsString);
  37.             }
  38.             $email = (new Email())
  39.                 ->from('info@super-kuryr.cz')
  40.                 ->to("uctarna@super-kuryr.cz")
  41.                 ->subject('Nová objednávka SuperKurýr')
  42.                 ->html(
  43.                     $this->renderView(
  44.                         'support/emails/new_order.html.twig',
  45.                         [
  46.                             'email' => $order->getEmail(),
  47.                             'name' => $order->getName(),
  48.                             'phone' => $order->getPhone(),
  49.                             'detail' => $order->getDetail()
  50.                         ]
  51.                     )
  52.                 );
  53.             $mailer->send($email);
  54.             return $this->redirect($this->generateUrl('thank_you'));
  55.         }
  56.         return $this->render('customer/contact.html.twig', [
  57.             'form' => $form->createView()
  58.         ]);
  59.     }
  60. }