src/Controller/Inscription/InscriptionController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Inscription;
  3. use App\Entity\Adresse;
  4. use App\Entity\Individu;
  5. use App\Entity\Optin;
  6. use App\Entity\Prospect;
  7. use App\Entity\Reabonnement;
  8. use App\Entity\RefSource;
  9. use App\Entity\User;
  10. use App\Entity\RefOptin;
  11. use App\Entity\RefTypeOptin;
  12. use App\Form\InscriptionType;
  13. use App\Form\ResetPasswordType;
  14. use App\Service\MailerService;
  15. use DateInterval;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use App\Service\PasswordTempGenerator;
  21. use App\Service\salesForce;
  22. use App\Entity\LogSalesforce;
  23. use App\Entity\RefTypeEnvoiSalesforce;
  24. class InscriptionController extends AbstractController
  25. {
  26.     private $passwordEncoder;
  27.     private $serviceSalesforce;
  28.     private $entityManager;
  29.     private $typeFormatMag;
  30.     public function __construct(EntityManagerInterface $entityManager,
  31.                                 UserPasswordEncoderInterface $passwordEncoder,
  32.                                 salesForce $serviceSalesforce)
  33.     {
  34.         $this->entityManager $entityManager;
  35.         $this->passwordEncoder $passwordEncoder;
  36.         $this->serviceSalesforce $serviceSalesforce;
  37.     }
  38.     public function inscription()
  39.     {
  40.         return $this->redirectToRoute('inscriptionForm',[
  41.             'format' => 'digital',
  42.             'prospect' => 0,
  43.         ]);
  44.     }
  45.     public function inscriptionForm(Request $request,$format,$prospect,PasswordTempGenerator $passwordGeneratorMailerService $mailerService){
  46.         $individu = new Individu();
  47.         $user = new User();
  48.         $adresse = new Adresse();
  49.         $adresse->setIdIndiv($individu);
  50.         $subscriptions = [];
  51.         $prospect $this->entityManager->getRepository(Prospect::class)->findOneByEmail($prospect);
  52.         $form $this->createForm(InscriptionType::class,[
  53.             'individu'=>$individu,
  54.             'adresse'=>$adresse,
  55.             'subscriptions'=>$subscriptions
  56.         ]);
  57.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  58.             $individu->setDateInscription(new \DateTime());
  59.             $dateCrea = new \DateTime();
  60.             $individu->setDateInactivite($dateCrea->add(new DateInterval("P3Y")));
  61.             $source $this->entityManager->getRepository(RefSource::class)->findOneBy(['libelle' => 'SITE_D4']);
  62.             $individu->setIdRefSource($source);
  63.             $this->entityManager->persist($individu);
  64.             $user->setIndividu($individu);
  65.             $user->setEmail($individu->getEmail());
  66.             // GESTION PASSWORD
  67.             $passwordTemp =  $passwordGenerator->generateRandomStrongPassword(8);
  68.             $newPassword $this->passwordEncoder->encodePassword($user,$passwordTemp);
  69.             $user->setPassword($newPassword);
  70.             $user->setChangePassword(false);
  71.             $user->setRoles(['ROLE_ABONNE']);
  72.             $this->entityManager->persist($user);
  73.             $adresse->setIdIndiv($individu);
  74.             $this->entityManager->persist($adresse);
  75.             if($prospect){
  76.                 $dateFinProspect = new \DateTime();
  77.                 $prospect->setIdIndiv($individu);
  78.                 $prospect->setPlusProspect(true);
  79.                 $prospect->setDateFinProspect($dateFinProspect);
  80.                 $this->entityManager->persist($prospect);
  81.             }
  82.             $formData $form->getViewData();
  83.             $magazine '';
  84.             $newsletters '';
  85.             $additionals '';
  86.             foreach ($formData['subscriptions'] as $key => $subscription) {
  87.                 $optin = new Optin();
  88.                 $optin->setIdIndiv($individu);
  89.                 $optin->setEnable($subscription);
  90.                 switch ($key) {
  91.                     case "magPrint":
  92.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(1));
  93.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(1));
  94.                         if($format === 'print'){
  95.                             $optin->setEnable(TRUE);
  96.                             $magazine 'Print (Free - 6 times a year for 3 years)';
  97.                         }
  98.                         break;
  99.                     case "magDigital":
  100.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(2));
  101.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(1));
  102.                         if($format === 'digital'){
  103.                             $optin->setEnable(TRUE);
  104.                             $magazine 'Digital (Free - 6 times a year for 3 years)';
  105.                         }
  106.                         break;
  107.                     case "magDigitPrint":
  108.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(3));
  109.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(1));
  110.                         if($format === 'print + digital'){
  111.                             $optin->setEnable(TRUE);
  112.                             $magazine 'Digital + Print (Free - 6 times a year for 3 years)';
  113.                         }
  114.                         break;
  115.                     case "nlEn":
  116.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(4));
  117.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(2));
  118.                         if($optin->getEnable()){
  119.                             $newsletters .= '<b>JEC Composites Market News (English, 52 issues/year)</b><br />';
  120.                         }
  121.                         break;
  122.                     case "nlFr":
  123.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(5));
  124.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(2));
  125.                         if($optin->getEnable()) {
  126.                             $newsletters .= '<b>JEC Composites Informations (French, 26 issues/year)</b><br />';
  127.                         }
  128.                         break;
  129.                     case "jecCompo":
  130.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(6));
  131.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(3));
  132.                         if($optin->getEnable()) {
  133.                             $additionals .= '<b>You wish to receive information from JEC Composites Magazine</b><br />';
  134.                         }
  135.                         break;
  136.                     case "jecGroup":
  137.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(7));
  138.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(3));
  139.                         if($optin->getEnable()) {
  140.                             $additionals .= '<b>You wish to receive information and specials offers from JEC Group</b><br />';
  141.                         }
  142.                         break;
  143.                     case "jecPartners":
  144.                         $optin->setIdRefOptin($this->entityManager->getRepository(RefOptin::class)->find(8));
  145.                         $optin->setIdRefTypeOptin($this->entityManager->getRepository(RefTypeOptin::class)->find(3));
  146.                         if($optin->getEnable()) {
  147.                             $additionals .= '<b>You wish to receive special offers and promotional communication from JEC Partners</b><br />';
  148.                         }
  149.                         break;
  150.                 }
  151.                 $this->entityManager->persist($optin);
  152.             }
  153.             $email $user->getEmail();
  154.             $this->entityManager->flush();
  155.             $contentJson $this->constructJson('indiv'$individu$adresse,null);
  156.             $this->serviceSalesforce->create();
  157.             $response $this->serviceSalesforce->envoiData('indiv'$contentJson);
  158.             $logSalesforce = new LogSalesforce();
  159.             $logSalesforce->setIdIndividu($individu);
  160.             $logSalesforce->setContenuEnvoi($contentJson);
  161.             $logSalesforce->setIdRefTypeEnvoiSalesforce($this->entityManager->getRepository(RefTypeEnvoiSalesforce::class)->findOneBy(['libelle' => 'indiv']));
  162.             $logSalesforce->setStatuHttp($response === false "400" $response->getStatusCode());
  163.             $this->entityManager->persist($logSalesforce);
  164.             if($response->getStatusCode() == 200 and json_decode($response->getContent(),true)['success'] == true){
  165.                 $individu->setIdSalesforce(json_decode($response->getContent(),true)['id']);
  166.                 $this->entityManager->persist($individu);
  167.             }
  168.             $this->entityManager->flush();
  169.             // envoi email confirmation inscription
  170.             $mailerService->sendEmail(MailerService::ID_CAMP_CONFIRM_CREA_COMPTE, [
  171.                 ['name' => 'adresse_email''value' => $email],
  172.                 ['name' => 'jec_magazine''value' => $magazine ?? " "],
  173.                 ['name' => 'jec_nl''value' => $newsletters ?? " "],
  174.                 ['name' => 'jec_additional''value' => $additionals ?? " "],
  175.                 ['name' => 'sujet_email_jec''value' => 'JEC Composites - Confirmation activation account']
  176.             ]);
  177.             // envoi email mot de passe temp
  178.             $mailerService->sendEmail(MailerService::ID_CAMP_REINIT_PASSWORD, [
  179.                 ['name' => 'adresse_email''value' => $email],
  180.                 ['name' => 'jec_password''value' => $passwordTemp],
  181.                 ['name' => 'sujet_email_jec''value' => 'JEC Composites - Your temporary password'],
  182.             ]);
  183.             return $this->redirectToRoute('inscriptionEnd');
  184.         }else{
  185.             return $this->render('Inscription/inscriptionForm.html.twig', [
  186.                 'format' => $format,
  187.                 'form' => $form->createView(),
  188.                 'prospect'=>$prospect
  189.             ]);
  190.         }
  191.     }
  192.     public function inscriptionEnd()
  193.     {
  194.         return $this->render('Inscription/inscriptionEnd.html.twig');
  195.     }
  196.     public function reset(Request $requestPasswordTempGenerator $passwordGeneratorMailerService $mailerService)
  197.     {
  198.         $form $this->CreateForm(ResetPasswordType::class);
  199.         $form->handleRequest($request);
  200.         $email $request->request->get('reset_password');
  201.         if($form->isSubmitted() && $form->isValid()){
  202.             //à mettre en place lors de la pseudonymisation
  203.             $querySql "SELECT id FROM jec_prod.user u WHERE (email).hashed = jec_prod.encode_search('".$email['email']."' ,'c6c71a47913df5a980a73ccb500ee68f194088b515e4538a3c15bc6a2fb1e30d065aadb03409ebadd1def081a5c0eed2c73e9656a024eb86c627b01462609e45')";
  204.             $conn $this->entityManager
  205.                 ->getConnection();
  206.             $stmt $conn->prepare($querySql);
  207.             $resultSql =$stmt->execute([])->fetch();
  208.             $user = (false === $resultSql) ? NULL $this->entityManager->getRepository(User::class)->findOneBy(['id' => $resultSql['id']]);
  209.             if(!$user){
  210.                 $this->addFlash('danger''No account matching this email address was found.');
  211.                 return $this->render('Inscription/lostPassword.html.twig', [
  212.                     'form' => $form->createView(),
  213.                     'path_lien_redirect' => 'inscription',
  214.                     'texte_lien_redirect' => 'Subscribe for free >'
  215.                 ]);
  216.             }else {
  217.                 $passwordTemp $passwordGenerator->generateRandomStrongPassword(8);
  218.                 $newPassword $this->passwordEncoder->encodePassword($user$passwordTemp);
  219.                 $user->setPassword($newPassword);
  220.                 $user->setChangePassword(false);
  221.                 if(!$user->hasRole('ROLE_BO')) {
  222.                     $user->setRoles(['ROLE_USER']);
  223.                 }
  224.                 $this->entityManager->persist($user);
  225.                 $this->entityManager->flush();
  226.                 //envoi mail
  227.                 $mailerService->sendEmail(MailerService::ID_CAMP_REINIT_PASSWORD, [
  228.                     ['name' => 'adresse_email''value' => $email['email']], 
  229.                     ['name' => 'jec_password''value' => $passwordTemp],
  230.                     ['name' => 'sujet_email_jec''value' => 'JEC Composites - Reset my password'],
  231.                 ]);
  232.                 $this->addFlash('success''Your temporary password has been sent. Please check your inbox.');
  233.                 return $this->render('Inscription/loginPage.html.twig', [
  234.                     'form' => $form->createView(),
  235.                     'last_username' => $email['email'],
  236.                     'error' => null,
  237.                 ]);
  238.             }
  239.         }
  240.         else{
  241.             return $this->render('Inscription/lostPassword.html.twig', [
  242.                 'form' => $form->createView(),
  243.             ]);
  244.         }
  245.     }
  246.     public function landingPageParrainage(Request $requeststring $email)
  247.     {
  248.         $prospect $this->entityManager->getRepository(Prospect::class)->findOneByEmail($email);
  249.         if($prospect->getPlusProspect() || $prospect->getActif()){
  250.             return $this->render('accueil.html.twig', [
  251.             ]);
  252.         }
  253.         else{
  254.             if($request->get('abonne')){
  255.                 return $this->render('Inscription/choose_mag.html.twig', [
  256.                     'prospect' => $email,
  257.                 ]);
  258.             } elseif($request->get('desabo')){
  259.                 $prospect->setPlusProspect(true);
  260.                 $prospect->setDateFinProspect(new \DateTime());
  261.                 $this->entityManager->persist($prospect);
  262.                 $this->entityManager->flush();
  263.                 $this->addFlash('success''Your changes have been saved successfully.');
  264.             }
  265.             return $this->render('landingPageParrainage.html.twig', [
  266.                 'email' => $email,
  267.                 'prospect' => $prospect
  268.             ]);
  269.         }
  270.     }
  271.     public function constructJson(string $typeJsonIndividu $individu, ?Adresse $adresse, ?Reabonnement $reabonnement)
  272.     {
  273.         $currentDate = new \DateTime();
  274.         $nlEn $this->entityManager->getRepository(Optin::class)->findOneBy( ['idIndiv' => $individu->getId(), 'idRefOptin' => 4])->getEnable();
  275.         $nlFr $this->entityManager->getRepository(Optin::class)->findOneBy( ['idIndiv' => $individu->getId(), 'idRefOptin' => 5])->getEnable();
  276.         $FormatMag $this->entityManager->getRepository(Optin::class)->TypeOptinMagazine($individu->getId());
  277.         $this->typeFormatMag $FormatMag $FormatMag->getIdRefOptin()->getLibelle() : null;
  278.         $inactif $individu->getDateDesabo() || $individu->getDateInactivite() < $currentDate true false;
  279.         if($this->typeFormatMag == 'DIGITAL+PRINT'){
  280.             $this->typeFormatMag 'Digital & Print';
  281.         }
  282.         switch ($typeJson){
  283.             case 'indiv':
  284.                 $contentJson = [
  285.                     "Name" => '',
  286.                     "Salutation__c" => ($individu->getIdRefCiv() ? $individu->getIdRefCiv()->getAbregee() : ''),
  287.                     "FirstName__c" => $individu->getFirstName(),
  288.                     "LastName__c" => $individu->getLastName(),
  289.                     "Email__c" => $individu->getEmail(),
  290.                     "Company__c" => $adresse->getCompanyName(),
  291.                     "JobTitle__c" => $individu->getJobTitle(),
  292.                     "Department__c" => ($individu->getIdRefDepartmentSector() ? $individu->getIdRefDepartmentSector()->getLibelle() : ''),
  293.                     "Function__c" => ($individu->getIdRefFonction() ? $individu->getIdRefFonction()->getLibelle() : ''),
  294.                     "BusinessPhone__c" => $individu->getBusinessPhone(),
  295.                     "MobilePhone__c" => $individu->getMobilePhone(),
  296.                     "SubscriptionDate__c" => $individu->getDateInscription()->format('Y-m-d'),
  297.                     "UnsubscriptionDate__c" => ($individu->getDateDesabo() ? $individu->getDateDesabo()->format('Y-m-d') : null),
  298.                     "ID_D4__c" => $individu->getId(),
  299.                     "NewsletterEN__c" => $nlEn,
  300.                     "NewsletterFR__c" => $nlFr,
  301.                     "Inactive__c" => $inactif,
  302.                     "inactivedate__c" => $individu->getDateInactivite()->format('Y-m-d'),
  303.                     "city__c" => $adresse->getTownCity(),
  304.                     "countrycode__c" => ($adresse->getIdRefPays() ? $adresse->getIdRefPays()->getCodePays() : ''),
  305.                     "postalcode__c" => $adresse->getPostCodeZip(),
  306.                     "street1__c" => $adresse->getAdr1(),
  307.                     "street2__c" => $adresse->getAdr2(),
  308.                     "street3__c" => $adresse->getAdr3(),
  309.                     "street4__c" => $adresse->getAdr4(),
  310.                     "subscriptionformat__c" => strtoupper($this->typeFormatMag),
  311.                     "source__c" => ($individu->getidRefSource() ? $individu->getidRefSource()->getIdRefTypeSource()->getLibelle().' - '.$individu->getidRefSource()->getLibelle() : '')
  312.                 ];
  313.                 break;
  314.             case 'reabo':
  315.                 $contentJson = [
  316.                     "ID_D4__c" => $reabonnement->getId(),
  317.                     "ResubDate__c" => $reabonnement->getDateReabo()->format('Y-m-d'),
  318.                     "Format__c" => strtoupper($this->typeFormatMag),
  319.                     "JCM_Subscription__r" => [
  320.                         "ID_D4__c" => $individu->getId()
  321.                     ],
  322.                     "Source__c" => $reabonnement->getidRefSource()->getIdRefTypeSource()->getLibelle().' - '.$reabonnement->getidRefSource()->getLibelle()
  323.                 ];
  324.                 break;
  325.             default:
  326.                 $contentJson = [];
  327.         }
  328.         return $contentJson;
  329.     }
  330. }