<?php
namespace App\Controller;
use App\Entity\Quotation;
//use App\Form\QuotationType;
use App\Form\QuotationType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\QuotationRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
//use Doctrine\Common\Persistence\ObjectManager;
class ContactController extends AbstractController
{
/**
* @var QuotationRepository
*/
private $repository;
public function __construct(QuotationRepository $repository, EntityManagerInterface $em)
{
$this->repository = $repository;
$this->em = $em;
}
/**
* @Route("/contact", name="contact")
*/
public function index()
{
return $this->render('quotation/index.html.twig', [
'controller_name' => 'QuotationController',
]);
}
/**
* @Route("/contact/create", name="contact_create")
*
*/
public function new(Request $request, MailerInterface $mailer)
{
$quotation = new Quotation();
$form = $this->createForm(QuotationType::class, $quotation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$quotation->setRequestDate(new \DateTime('now'));
$this->em->persist($quotation);
$this->em->flush();
$this->addFlash('success', 'Votre demande a bien été envoyée');
$adminEmail = 'contact@puush.io';
$useremail = $quotation->getEmail();
$company = $quotation->getCompany();
$firstname = $quotation->getFirstname();
$lastname = $quotation->getLastname();
$message = $quotation->getMessage();
$phone = $quotation->getPhone();
$email = (new TemplatedEmail())
->from('contact@puush.io')
->to(new Address($useremail))
->bcc($adminEmail)
->subject('Votre demande à Puush')
->htmlTemplate('/emails/quotation.html.twig')
->context([
'expiration_date' => new \DateTime('+7 days'),
'company' => $company,
'firstname' => $firstname,
'lastname' => $lastname,
'message' => $message,
'useremail' => $useremail,
'phone' => $phone,
])
;
$mailer->send($email);
return $this->redirectToRoute('home');
} elseif ($form->isSubmitted() && !($form->isValid())) {
$this->addFlash('error', 'Votre formulaire comporte une erreur');
}
return $this->render('contact/contact.html.twig', [
'contactForm' => $form->createView()
]);
}
}