<?php
namespace App\Controller\Customer;
use App\Entity\Phone;
use App\Form\SimplePhoneType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ContactController extends AbstractController
{
/**
* Kontaktni formular
* @param Request $request
* @param ValidatorInterface $validator
* @param MailerInterface $mailer
* @return Response
* @throws TransportExceptionInterface
*/
public function contact(Request $request, ValidatorInterface $validator, MailerInterface $mailer): Response
{
$order = new Phone();
$form = $this->createForm(SimplePhoneType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$errors = $validator->validate($order);
if (count($errors) > 0) {
/*
* Uses a __toString method on the $errors variable which is a
* ConstraintViolationList object. This gives us a nice string
* for debugging.
*/
$errorsString = (string)$errors;
return new Response($errorsString);
}
$email = (new Email())
->from('info@super-kuryr.cz')
->to("uctarna@super-kuryr.cz")
->subject('Nová objednávka SuperKurýr')
->html(
$this->renderView(
'support/emails/new_order.html.twig',
[
'email' => $order->getEmail(),
'name' => $order->getName(),
'phone' => $order->getPhone(),
'detail' => $order->getDetail()
]
)
);
$mailer->send($email);
return $this->redirect($this->generateUrl('thank_you'));
}
return $this->render('customer/contact.html.twig', [
'form' => $form->createView()
]);
}
}