<?php
namespace App\EventSubscriber;
use App\Entity\Combi;
use App\Entity\Save;
use App\Entity\CombiVariable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Flasher\SweetAlert\Prime\SweetAlertFactory;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\Core\Cache\CacheableRedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Service\CombiCSVService;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
class CombiSubscriber implements EventSubscriberInterface
{
private $entityManager;
private ManagerRegistry $manager;
private CombiCSVService $csvService;
public function __construct(
CombiCSVService $csvService,
ManagerRegistry $manager,
EntityManagerInterface $entityManager,
TokenStorageInterface $tokenStorage
) {
$this->csvService = $csvService;
$this->manager = $manager;
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['afterAdd'],
AfterEntityUpdatedEvent::class => ['afterUpdate']
];
}
//chaque combi possède un titre : interdit d'ajouter deux combis de mm titre dans le mm projet
public function afterAdd(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof Combi) {
$combis = $entity->getProjet()->getCombis();
$count = 0;
foreach ($combis as $combi) {
if (strtolower($combi->getTitle()) == strtolower($entity->getTitle())) {
$count++;
}
}
if ($count > 1) {
$this->entityManager->remove($entity);
}
$this->entityManager->flush();
}
}
//export après chaque modification
public function afterUpdate(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof Combi) {
//$projet=$entity->getProjet()->getProjet();
$this->csvService->exportCombiVariable($entity);
}
if ($entity instanceof CombiVariable) {
//$projet=$entity->getProjet()->getProjet();
$this->csvService->exportCombiVariable($entity);
}
}
}