src/EventSubscriber/BannierePubSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\BannierePub;
  4. use App\Entity\Commune;
  5. use App\Entity\AnnuairePapier;
  6. use Doctrine\ORM\EntityManagerInterface;
  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 mysql_xdevapi\Exception;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class BannierePubSubscriber implements EventSubscriberInterface
  14. {
  15.     private $entityManager;
  16.     public function __construct(EntityManagerInterface $entityManager)
  17.     {
  18.         $this->entityManager $entityManager;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             BeforeEntityPersistedEvent::class => ['setCommunes'],
  24.             AfterEntityUpdatedEvent::class => ['updateCommune']
  25.         ];
  26.     }
  27.     public function setCommunes(BeforeEntityPersistedEvent $event)
  28.     {
  29.         try{
  30.             $entity $event->getEntityInstance();
  31.             if (( $entity instanceof BannierePub )) {
  32.                 //  dd($entity->getProvince());
  33.                 if(count($entity->getProvince()) >= 1){
  34.                     $provinces $entity->getProvince();
  35.                     $communees $entity->getCommune();
  36.                     $ids = [];
  37.                     $comm=[];
  38.                     if (count($communees) >= 1) {
  39.                         foreach ($communees as $c) {
  40.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  41.                             array_push($comm$commune);
  42.                         }
  43.                     }
  44.                     foreach ($provinces as $p) {
  45.                         $communes $this->entityManager->getRepository(Commune::class)->findBy(["idProvince" => $p->getId()]);
  46.                     foreach ($communes as $c){
  47.                         $commune $this->entityManager->getRepository(Commune::class)->find($c);
  48.                         array_push($ids$commune);
  49.                     }
  50.                         $result array_unique(array_merge($ids$comm));
  51.                         $entity->setCommune($result);
  52.                         $this->entityManager->persist($entity);
  53.                         $this->entityManager->flush();
  54.                     }
  55.                 }
  56.             }
  57.         } catch (\Doctrine\ORM\ORMException $e){
  58.             return $e;
  59.         }
  60.     }
  61.     public function updateCommune(AfterEntityUpdatedEvent $event)
  62.     {
  63.         try {
  64.             $entity $event->getEntityInstance();
  65.             if (($entity instanceof BannierePub)) {
  66.                 //dd($entity->getProvince());
  67.                 if (count($entity->getProvince()) >= 1) {
  68.                     $provinces $entity->getProvince();
  69.                     $communees $entity->getCommune();
  70.                     $ids = [];
  71.                     $comm=[];
  72.                     if (count($communees) >= 1) {
  73.                         foreach ($communees as $c) {
  74.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  75.                             array_push($comm$commune);
  76.                         }
  77.                     }
  78.                     foreach ($provinces as $p) {
  79.                         $communes $this->entityManager->getRepository(Commune::class)->findBy(["idProvince" => $p->getId()]);
  80.                         foreach ($communes as $c) {
  81.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  82.                             array_push($ids$commune);
  83.                         }
  84.                         $result =array_unique(array_merge($ids$comm), SORT_REGULAR);
  85.                         $entity->setCommune($result);
  86.                         $this->entityManager->persist($entity);
  87.                         $this->entityManager->flush();
  88.                 }
  89.                 }
  90.             }
  91.         } catch (\Doctrine\ORM\ORMException $e) {
  92.             return $e;
  93.         }
  94.     }
  95. }