<?php
namespace App\EventSubscriber;
use App\Entity\Conferences;
use App\Entity\Communications;
use App\Entity\User;
use App\Entity\Nomail;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
class EasyAdminSubscriber implements EventSubscriberInterface
{
//private $slugger;
private $entityManager;
private $userPasswordHasher;
private $mailer;
public function __construct(ManagerRegistry $doctrine, UserPasswordHasherInterface $userPasswordHasher, MailerInterface $mailer)//$slugger)
{
//$this->slugger = $slugger;
$this->entityManager = $doctrine->getManager();
$this->userPasswordHasher = $userPasswordHasher;
$this->mailer = $mailer;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['launchAfterPers'],
AfterEntityUpdatedEvent::class => ['launchAfterUp'],
BeforeEntityPersistedEvent::class => ['launchBeforePers'],
BeforeEntityUpdatedEvent::class => ['launchBeforeUp'],
];
}
public function launchAfterPers(AfterEntityPersistedEvent $event){
//$this->createCommunications($event);
$this->resetPlainPass($event);
}
public function launchAfterUp(AfterEntityUpdatedEvent $event){
//$this->createCommunications1($event);
$this->resetPlainPass($event);
}
public function launchBeforePers(BeforeEntityPersistedEvent $event){
$this->addUser($event);
}
public function launchBeforeUp(BeforeEntityUpdatedEvent $event){
$this->updateUser($event);
}
public function resetPlainPass($event) {
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$entity->setPlainpassword('');
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
public function updateUser(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
if($entity->getEmail() == 'none') {$this->makeFakeMail($entity);}
$this->setPassword($entity);
}
public function addUser(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
if($entity->getEmail() == 'none') {$this->makeFakeMail($entity);}
$this->setPassword($entity);
}
public function setPassword(User $entity): void
{
$plainpass = $entity->getPlainpassword();
if($plainpass != '') {
$uemail = $entity->getEmail();
$firstname = $entity->getFirstname();
$lastname = $entity->getLastname();
$entity->setPassword(
$this->userPasswordHasher->hashPassword(
$entity,
$plainpass
)
);
$entity->setPlainpassword('');
$this->entityManager->persist($entity);
$this->entityManager->flush();
$sendemail = (new TemplatedEmail())
->to($uemail)
->subject('Account information')
->htmlTemplate('emails/newpass.html.twig')
->context([
'expiration_date' => new \DateTime('+7 days'),
'uemail' => $uemail,
'firstname' => $firstname,
'lastname' => $lastname,
'link' => 'https://preview.monteil-audiovisuel.fr/login',
'password' => $plainpass,
]);
if(strpos($uemail,'@fakemail.fake') == false && strpos($uemail,'@vincent.com') == false ) {
$this->mailer->send($sendemail);
}
}
}
public function makeFakeMail($entity) {
//on a pas de mail, on en crée un faux
$nomail = new Nomail();
$this->entityManager->persist($nomail);
$this->entityManager->flush();
$newid = $nomail->getId();
$newmail = $newid.'@fakemail.fake';
$entity->setEmail($newmail);
//$this->entityManager->persist($entity);
//$this->entityManager->flush();
}
public function createCommunications(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Conferences)) {
return;
}
$conferenciers = $entity->getConferenciers();
$confid = $entity->getId();
$heure_debut = "09:00";
$duree = 15;
$pause = 10;
foreach($conferenciers as $conferencier){
//pour chaque conferencier on verifie si une communication pour cet id de conference existe
$conferencierid = $conferencier->getId();
$commu = $this->entityManager->getRepository(Communications::class)->findExistant($conferencier,$entity);
if($commu){
//si oui on update
$comm = $commu[0];
$comm->setTitle('titre');
$comm->setDescription('description');
$comm->setHeureDebut($heure_debut);
$comm->setDuree($duree);
$comm->setConferencier($conferencier);
$comm->setConference($entity);
$this->entityManager->persist($comm);
$this->entityManager->flush();
} else {
//si non on la crée
$com = new Communications();
$com->setTitle('titre');
$com->setDescription('description');
$com->setHeureDebut($heure_debut);
$com->setDuree($duree);
$com->setConferencier($conferencier);
$com->setConference($entity);
$this->entityManager->persist($com);
$this->entityManager->flush();
}
//$heure_debut += $duree + $pause;
}
}
public function createCommunications1(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Conferences)) {
return;
}
$conferenciers = $entity->getConferenciers();
$confid = $entity->getId();
$heure_debut = "09:00";
$duree = 15;
$pause = 10;
foreach($conferenciers as $conferencier){
//pour chaque conferencier on verifie si une communication pour cet id de conference existe
$conferencierid = $conferencier->getId();
$commu = $this->entityManager->getRepository(Communications::class)->findExistant($conferencier,$entity);
if($commu){
//si oui on update
$comm = $commu;
$comm->setHeureDebut($heure_debut);
$comm->setDuree($duree);
$this->entityManager->persist($comm);
$this->entityManager->flush();
} else {
//si non on la crée
$com = new Communications();
$com->setTitle('titre');
$com->setDescription('description');
$com->setHeureDebut($heure_debut);
$com->setDuree($duree);
$com->setConferencier($conferencier);
$com->setConference($entity);
$this->entityManager->persist($com);
$this->entityManager->flush();
}
//$heure_debut += $duree + $pause;
}
}
}