<?php
namespace App\Controller\Front;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use OlaSoft\Common;
use App\Entity\Registrations;
use App\Form\RegistrationsType;
use App\Entity\RegistrationsStatus;
class RegistrationsFrontController extends AbstractController
{
/**
* @Route("/pre-inscription/", name="registration")
*/
public function index(Request $request, MailerInterface $mailer )
{
$em = $this->getDoctrine()->getManager();
$item = new Registrations();
$form = $this->createForm(RegistrationsType::class, $item);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$browser = Common::getBrowser(null, true);
$item->setIp($request->getClientIP());
$item->setDate(new \DateTime);
$initialStatus = $em->getRepository(RegistrationsStatus::class)->findOneBy(['isInitialStatus'=>1,'isEnabled'=>1]);
if($initialStatus)
$item->setStatus($initialStatus);
$em->persist($item);
$item->setCode(strtoupper(Common::generateToken(6).'-'.dechex($item->getId() * 100000)));
$em->persist($item);
$em->flush();
if($initialStatus){
$sitename = $this->get('parameter_bag')->get('sitename');
$msg = $initialStatus->getOutgoingMessage();
$params = [
"{{name}}" => $item->getName(),
"{{code}}" => $item->getCode()
];
if($msg){
$email = (new TemplatedEmail())
->to(new Address($item->getEmail(),$item->getName()))
->priority(Email::PRIORITY_HIGH)
->subject($sitename." - Pré-inscription bien reçue")
->htmlTemplate('Mails/registration.html.twig')
->context(['item' => $item, 'msg'=>$msg, 'params'=>$params]);
if($initialStatus->getOutgoingCc()){
$adresses = preg_split("/[\s,;]+/", $initialStatus->getOutgoingCc());
$email->cc(...$adresses);
}
if($initialStatus->getOutgoingCci()){
$adresses = preg_split("/[\s,;]+/", $initialStatus->getOutgoingCci());
$email->bcc(...$adresses);
}
$mailer->send($email);
}
$msg = $initialStatus->getIncomingMessage();
$params = array_merge(
$params,
["{{link}}" => "https://".$_SERVER['HTTP_HOST'].$this->generateUrl("registrations-admin")]
);
if($msg){
$email = (new TemplatedEmail())
->priority(Email::PRIORITY_HIGH)
->subject("Site Web - Nouvelle inscription reçue")
->htmlTemplate('Mails/registration.html.twig')
->context(['item' => $item, 'msg'=>$msg, 'params'=>$params]);
if($initialStatus->getIncomingCc()){
$adresses = preg_split("/[\s,;]+/", $initialStatus->getIncomingCc());
$email->cc(...$adresses);
}
if($initialStatus->getIncomingCci()){
$adresses = preg_split("/[\s,;]+/", $initialStatus->getIncomingCci());
$email->bcc(...$adresses);
}
if($initialStatus->getIncomingRecipient()){
$adresses = preg_split("/[\s,;]+/", $initialStatus->getIncomingRecipient());
$email->to(...$adresses);
$mailer->send($email);
}
}
}
return $this->render('Default/submit-success.html.twig',['token'=>$item->getCode()]);
}
return $this->render('Default/registration.html.twig',['form'=>$form->createView()]);
}
}