src/EventSubscriber/EasyAdminSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Conferences;
  4. use App\Entity\Communications;
  5. use App\Entity\User;
  6. use App\Entity\Nomail;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Mime\Email;
  17. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  18. class EasyAdminSubscriber implements EventSubscriberInterface
  19. {
  20.     //private $slugger;
  21.     private $entityManager;
  22.     private $userPasswordHasher;
  23.     private $mailer;
  24.     public function __construct(ManagerRegistry $doctrineUserPasswordHasherInterface $userPasswordHasherMailerInterface $mailer)//$slugger)
  25.     {
  26.         //$this->slugger = $slugger;
  27.         $this->entityManager $doctrine->getManager();
  28.         $this->userPasswordHasher $userPasswordHasher;
  29.         $this->mailer $mailer;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             AfterEntityPersistedEvent::class => ['launchAfterPers'],
  35.             AfterEntityUpdatedEvent::class => ['launchAfterUp'],
  36.             BeforeEntityPersistedEvent::class => ['launchBeforePers'],
  37.             BeforeEntityUpdatedEvent::class => ['launchBeforeUp'],
  38.         ];
  39.     }
  40.     public function launchAfterPers(AfterEntityPersistedEvent $event){
  41.         //$this->createCommunications($event);
  42.         $this->resetPlainPass($event);
  43.     }
  44.     public function launchAfterUp(AfterEntityUpdatedEvent $event){
  45.         //$this->createCommunications1($event);
  46.         $this->resetPlainPass($event);
  47.     }
  48.     public function launchBeforePers(BeforeEntityPersistedEvent $event){
  49.         $this->addUser($event);
  50.     }
  51.     public function launchBeforeUp(BeforeEntityUpdatedEvent $event){
  52.         $this->updateUser($event);
  53.     }
  54.     public function resetPlainPass($event) {
  55.         $entity $event->getEntityInstance();
  56.         if (!($entity instanceof User)) {
  57.             return;
  58.         }
  59.         $entity->setPlainpassword('');
  60.         $this->entityManager->persist($entity);
  61.         $this->entityManager->flush();
  62.     }
  63.     public function updateUser(BeforeEntityUpdatedEvent $event)
  64.       {
  65.           $entity $event->getEntityInstance();
  66.           if (!($entity instanceof User)) {
  67.               return;
  68.           }
  69.           if($entity->getEmail() == 'none') {$this->makeFakeMail($entity);}
  70.           $this->setPassword($entity);
  71.       }
  72.     public function addUser(BeforeEntityPersistedEvent $event)
  73.       {
  74.           $entity $event->getEntityInstance();
  75.           if (!($entity instanceof User)) {
  76.               return;
  77.           }
  78.           if($entity->getEmail() == 'none') {$this->makeFakeMail($entity);}
  79.           $this->setPassword($entity);
  80.       }
  81.     public function setPassword(User $entity): void
  82.       {
  83.           $plainpass $entity->getPlainpassword();
  84.           if($plainpass != '') {
  85.             $uemail       $entity->getEmail();
  86.             $firstname    $entity->getFirstname();
  87.             $lastname     $entity->getLastname();
  88.           $entity->setPassword(
  89.               $this->userPasswordHasher->hashPassword(
  90.                   $entity,
  91.                   $plainpass
  92.               )
  93.           );
  94.           $entity->setPlainpassword('');
  95.           $this->entityManager->persist($entity);
  96.           $this->entityManager->flush();
  97.           
  98.           $sendemail = (new TemplatedEmail())
  99.                 ->to($uemail)
  100.                 ->subject('Account information')
  101.                 ->htmlTemplate('emails/newpass.html.twig')
  102.                 ->context([
  103.                     'expiration_date' => new \DateTime('+7 days'),
  104.                     'uemail'        => $uemail,
  105.                     'firstname'     => $firstname,
  106.                     'lastname'      => $lastname,
  107.                     'link'     => 'https://preview.monteil-audiovisuel.fr/login',
  108.                     'password'   => $plainpass,
  109.                 ]);
  110.                 if(strpos($uemail,'@fakemail.fake') == false && strpos($uemail,'@vincent.com') == false ) {
  111.                     $this->mailer->send($sendemail);
  112.                 }
  113.          }
  114.       }
  115.     public function makeFakeMail($entity) {
  116.         //on a pas de mail, on en crée un faux
  117.         $nomail = new Nomail();
  118.         $this->entityManager->persist($nomail);
  119.         $this->entityManager->flush();
  120.         $newid $nomail->getId();
  121.         $newmail $newid.'@fakemail.fake';
  122.         $entity->setEmail($newmail);
  123.         //$this->entityManager->persist($entity);
  124.         //$this->entityManager->flush();
  125.     }
  126.     public function createCommunications(AfterEntityPersistedEvent $event)
  127.     {
  128.         $entity $event->getEntityInstance();
  129.         if (!($entity instanceof Conferences)) {
  130.             return;
  131.         }
  132.         $conferenciers $entity->getConferenciers();
  133.         $confid $entity->getId();
  134.         $heure_debut "09:00";
  135.         $duree 15;
  136.         $pause 10;
  137.         foreach($conferenciers as $conferencier){
  138.             //pour chaque conferencier on verifie si une communication pour cet id de conference existe
  139.             $conferencierid $conferencier->getId();
  140.             $commu $this->entityManager->getRepository(Communications::class)->findExistant($conferencier,$entity);
  141.             if($commu){
  142.                 //si oui on update
  143.                 $comm $commu[0];
  144.                 $comm->setTitle('titre');
  145.                 $comm->setDescription('description');
  146.                 $comm->setHeureDebut($heure_debut);
  147.                 $comm->setDuree($duree);
  148.                 $comm->setConferencier($conferencier);
  149.                 $comm->setConference($entity);
  150.                 $this->entityManager->persist($comm);
  151.                 $this->entityManager->flush();
  152.             } else {
  153.                 //si non on la crée
  154.                 $com = new Communications();
  155.                 $com->setTitle('titre');
  156.                 $com->setDescription('description');
  157.                 $com->setHeureDebut($heure_debut);
  158.                 $com->setDuree($duree);
  159.                 $com->setConferencier($conferencier);
  160.                 $com->setConference($entity);
  161.                 $this->entityManager->persist($com);
  162.                 $this->entityManager->flush();
  163.             }
  164.             //$heure_debut += $duree + $pause;
  165.         }
  166.     }
  167.     public function createCommunications1(AfterEntityUpdatedEvent $event)
  168.     {
  169.         $entity $event->getEntityInstance();
  170.         if (!($entity instanceof Conferences)) {
  171.             return;
  172.         }
  173.         $conferenciers $entity->getConferenciers();
  174.         $confid $entity->getId();
  175.         $heure_debut "09:00";
  176.         $duree 15;
  177.         $pause 10;
  178.         foreach($conferenciers as $conferencier){
  179.             //pour chaque conferencier on verifie si une communication pour cet id de conference existe
  180.             $conferencierid $conferencier->getId();
  181.             $commu $this->entityManager->getRepository(Communications::class)->findExistant($conferencier,$entity);
  182.             if($commu){
  183.                 //si oui on update
  184.                 $comm $commu;
  185.                 $comm->setHeureDebut($heure_debut);
  186.                 $comm->setDuree($duree);
  187.                 $this->entityManager->persist($comm);
  188.                 $this->entityManager->flush();
  189.             } else {
  190.                 //si non on la crée
  191.                 $com = new Communications();
  192.                 $com->setTitle('titre');
  193.                 $com->setDescription('description');
  194.                 $com->setHeureDebut($heure_debut);
  195.                 $com->setDuree($duree);
  196.                 $com->setConferencier($conferencier);
  197.                 $com->setConference($entity);
  198.                 $this->entityManager->persist($com);
  199.                 $this->entityManager->flush();
  200.             }
  201.             //$heure_debut += $duree + $pause;
  202.         }
  203.         
  204.         
  205.     }
  206. }