<?php
namespace App\EventSubscriber;
use App\Entity\Commune;
use App\Entity\CommercesTemp;
use App\Entity\Commerces;
use App\Entity\TypeAnnonceur;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use mysql_xdevapi\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class commercesSubscriber implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['addCommerceTem'],
BeforeEntityUpdatedEvent::class => ['updateCommerceTem'],
BeforeEntityDeletedEvent::class =>['deleteCommerceTem']
];
}
public function addCommerceTem(AfterEntityPersistedEvent $event)
{
try{
$entity = $event->getEntityInstance();
// dd($entity,$entity->getCommune()->getIdProvince());
if (($entity instanceof Commerces)) {
$communess = $entity ->getCommune();
$com = $communess->getEditionEnPreparation();
// dd($communess, $com);
if( $com != null){
$categorie= $entity->getRubrique()->getCategorieRubrique();
// dd($categorie,$entity->getTypeAnnonceur());
$type = $categorie != null ? $this->entityManager->getRepository(TypeAnnonceur::class)->find($categorie->getId()) : $entity->getTypeAnnonceur();
$entity->setTypeAnnonceur($type);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$commerceTemp = new CommercesTemp();
$commerceTemp->setCommune($entity->getCommune());
$commerceTemp->setProvince($entity->getProvince());
$commerceTemp->setRubrique($entity->getRubrique());
$commerceTemp->setPersonneDeContact($entity->getPersonneDeContact());
$commerceTemp->setDenominationCommerciale($entity->getDenominationCommerciale());
$commerceTemp->setTypeAnnonceur($entity->getTypeAnnonceur());
$commerceTemp->setNom($entity->getNom());
$commerceTemp->setTel($entity->getTel());
$commerceTemp->setAdresse($entity->getAdresse());
$commerceTemp->setNumMaison($entity->getNumMaison());
$commerceTemp->setCp($entity->getCp());
$commerceTemp->setVille($entity->getVille());
$commerceTemp->setEmail($entity->getEmail());
$commerceTemp->setUrl($entity->getUrl());
$commerceTemp->setCommentaire($entity->getCommentaire());
$commerceTemp->setNoteInterne($entity->getNoteInterne());
$commerceTemp->setCreatedBy($entity->getCreatedBy());
$commerceTemp->setColor($entity->getColor());
$commerceTemp->setNumeroPub($entity->getNumeroPub());
$commerceTemp->setLastUpdate($entity->getLastUpdate());
$commerceTemp->setLastContact($entity->getLastContact());
$commerceTemp->setImage('');
$commerceTemp->setPrix(0);
$commerceTemp->setFormat(0);
$commerceTemp->setVente(0);
$commerceTemp->setLigne(0);
$commerceTemp->setNEdition($entity->getNEdition());
$commerceTemp->setAncienId($entity->getId());
$this->entityManager->persist($commerceTemp);
$this->entityManager->flush();
}
//dd($commerceTemp);
}
} catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
public function updateCommerceTem(BeforeEntityUpdatedEvent $event)
{
try{
$entity = $event->getEntityInstance();
if (($entity instanceof Commerces)) {
$categorie= $entity->getRubrique()->getCategorieRubrique();
// dd($categorie,$entity->getTypeAnnonceur());
// $type = $categorie != null ? $this->entityManager->getRepository(TypeAnnonceur::class)->find($categorie->getId()) : $entity->getTypeAnnonceur();
$type = $entity->getTypeAnnonceur();
// dd($categorie,$entity->getTypeAnnonceur(),$type);
$entity->setTypeAnnonceur($type);
$entity->setProvince($entity->getCommune()->getIdProvince());
$this->entityManager->persist($entity);
$this->entityManager->flush();
$commerceT = $this->entityManager->getRepository(CommercesTemp::class)->findOneBy(["ancienId"=>$entity->getId()]);
// dd($commerceT);
if(!$commerceT) {
$commerceT = $this->entityManager->getRepository(CommercesTemp::class)->findOneBy(["nom" => $entity->getNom()]);
}
/*if(!$commerceT){
$conn = $this->entityManager->getConnection();
$sql = "SELECT * from commerces_temp where commune_id=".$entity->getCommune()." AND (ancien_id =".$entity->getId()." OR nom=".$entity->getNom().");";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
dd("in update",$result);
}*/
if($commerceT){
// dd($entityManager1,$entityManager,$entity);
$commerceT->setCommune($entity->getCommune());
$commerceT->setProvince($entity->getCommune()->getIdProvince());
$commerceT->setRubrique($entity->getRubrique());
$commerceT->setPersonneDeContact($entity->getPersonneDeContact());
$commerceT->setDenominationCommerciale($entity->getDenominationCommerciale());
$commerceT->setTypeAnnonceur($entity->getTypeAnnonceur());
$commerceT->setNom($entity->getNom());
$commerceT->setTel($entity->getTel());
$commerceT->setAdresse($entity->getAdresse());
$commerceT->setNumMaison($entity->getNumMaison());
$commerceT->setCp($entity->getCp());
$commerceT->setVille($entity->getVille());
$commerceT->setEmail($entity->getEmail());
$commerceT->setUrl($entity->getUrl());
$commerceT->setCommentaire($entity->getCommentaire());
$commerceT->setNoteInterne($entity->getNoteInterne());
$commerceT->setCreatedBy($entity->getCreatedBy());
$commerceT->setColor($entity->getColor());
$commerceT->setNumeroPub($entity->getNumeroPub());
$commerceT->setLastUpdate($entity->getLastUpdate());
$commerceT->setLastContact($entity->getLastContact());
//Champs ne doivent pas changer dans commercesTemp
// $entityManager->setImage($entity->getImage());
// $entityManager->setPrix($entity->getPrix());
// $entityManager->setFormat($entity->getFormat());
// $entityManager->setVente($entity->getVente());
// $entityManager->setLigne($entity->getLigne());
// $entityManager->setNEdition($entity->getNEdition());
/* $this->entityManager->persist($commerceT);
$this->entityManager->flush();*/
}
}
} catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
public function deleteCommerceTem(BeforeEntityDeletedEvent $event)
{
try{
$entity = $event->getEntityInstance();
if (($entity instanceof Commerces)) {
$commerceTem1 = $this->entityManager->getRepository(CommercesTemp::class)->findOneBy(["ancienId"=>$entity->getId()]);
if(!$commerceTem1){
$commerceTem1 = $this->entityManager->getRepository(CommercesTemp::class)->findOneBy(["nom"=>$entity->getNom()]);
}
// dd($commerceTem1);
$id= $commerceTem1 ? $commerceTem1->getId(): null;
if($id){
$conn = $this->entityManager->getConnection();
$sql = "DELETE from commerces_temp where id =".$id;
$stmt = $conn->prepare($sql);
$stmt->execute();
}
// dd($commerceTem1,$id);
// $this->entityManager->createQueryBuilder()->delete('CommercesTemp','c')->where('s.id = :id')->setParameter('id', $id)->getQuery()->execute();
// $this->entityManager->createNativeQuery("DELETE from commercesTemp where id= ?",$id);
/* $this->entityManager->remove($commerceTem1);
$this->entityManager->persist($commerceTem1);
$this->entityManager->flush();*/
}
} catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
}