src/Controller/Front/RegistrationsFrontController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Mime\Address;
  12. use OlaSoft\Common;
  13. use App\Entity\Registrations;
  14. use App\Form\RegistrationsType;
  15. use App\Entity\RegistrationsStatus;
  16. class RegistrationsFrontController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/pre-inscription/", name="registration")
  20.      */
  21.     public function index(Request $requestMailerInterface $mailer )
  22.     {
  23.         $em $this->getDoctrine()->getManager();
  24.         $item = new Registrations();
  25.         $form $this->createForm(RegistrationsType::class, $item);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()){
  28.             $browser Common::getBrowser(nulltrue);
  29.             $item->setIp($request->getClientIP());
  30.             $item->setDate(new \DateTime);
  31.             $initialStatus $em->getRepository(RegistrationsStatus::class)->findOneBy(['isInitialStatus'=>1,'isEnabled'=>1]);
  32.             if($initialStatus)
  33.                 $item->setStatus($initialStatus);
  34.             $em->persist($item);
  35.             $item->setCode(strtoupper(Common::generateToken(6).'-'.dechex($item->getId() * 100000)));
  36.             $em->persist($item);
  37.             $em->flush();
  38.             if($initialStatus){
  39.                 $sitename $this->get('parameter_bag')->get('sitename');
  40.                 $msg $initialStatus->getOutgoingMessage();
  41.                 $params = [
  42.                     "{{name}}" => $item->getName(),
  43.                     "{{code}}" => $item->getCode()
  44.                 ];
  45.                 if($msg){
  46.                     $email = (new TemplatedEmail())
  47.                         ->to(new Address($item->getEmail(),$item->getName()))
  48.                         ->priority(Email::PRIORITY_HIGH)
  49.                         ->subject($sitename." - Pré-inscription bien reçue")
  50.                         ->htmlTemplate('Mails/registration.html.twig')
  51.                         ->context(['item' => $item'msg'=>$msg'params'=>$params]);
  52.                     if($initialStatus->getOutgoingCc()){
  53.                         $adresses preg_split("/[\s,;]+/"$initialStatus->getOutgoingCc());
  54.                         $email->cc(...$adresses);
  55.                     }
  56.                     if($initialStatus->getOutgoingCci()){
  57.                         $adresses preg_split("/[\s,;]+/"$initialStatus->getOutgoingCci());
  58.                         $email->bcc(...$adresses);
  59.                     }
  60.                     $mailer->send($email);
  61.                 }
  62.                 $msg $initialStatus->getIncomingMessage();
  63.                 $params array_merge(
  64.                     $params,
  65.                     ["{{link}}" => "https://".$_SERVER['HTTP_HOST'].$this->generateUrl("registrations-admin")]
  66.                 );
  67.                 if($msg){
  68.                     $email = (new TemplatedEmail())
  69.                         ->priority(Email::PRIORITY_HIGH)
  70.                         ->subject("Site Web - Nouvelle inscription reçue")
  71.                         ->htmlTemplate('Mails/registration.html.twig')
  72.                         ->context(['item' => $item'msg'=>$msg'params'=>$params]);
  73.                     if($initialStatus->getIncomingCc()){
  74.                         $adresses preg_split("/[\s,;]+/"$initialStatus->getIncomingCc());
  75.                         $email->cc(...$adresses);
  76.                     }
  77.                     if($initialStatus->getIncomingCci()){
  78.                         $adresses preg_split("/[\s,;]+/"$initialStatus->getIncomingCci());
  79.                         $email->bcc(...$adresses);
  80.                     }
  81.                     if($initialStatus->getIncomingRecipient()){
  82.                         $adresses preg_split("/[\s,;]+/"$initialStatus->getIncomingRecipient());
  83.                         $email->to(...$adresses);
  84.                         $mailer->send($email);
  85.                     }
  86.                 }
  87.             }
  88.             return $this->render('Default/submit-success.html.twig',['token'=>$item->getCode()]);
  89.         }
  90.         return $this->render('Default/registration.html.twig',['form'=>$form->createView()]);
  91.     }
  92. }