<?php
namespace App\EventSubscriber;
use App\Entity\Conferences;
use App\Entity\Communications;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
//private $slugger;
private $entityManager;
public function __construct(ManagerRegistry $doctrine)//$slugger)
{
//$this->slugger = $slugger;
$this->entityManager = $doctrine->getManager();
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['createCommunications'],
AfterEntityUpdatedEvent::class => ['createCommunications1'],
];
}
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[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;
}
}
}