src/Flexy/ShopBundle/EventSubscriber/EasyAdminSubscriber.php line 43

  1. <?php 
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\Flexy\ShopBundle\EventSubscriber;
  4. use App\Entity\BlogPost;
  5. use App\Flexy\ShopBundle\Entity\Brand;
  6. use App\Flexy\ShopBundle\Entity\Order\DemandeFund;
  7. use App\Flexy\ShopBundle\Entity\Order\Order;
  8. use App\Flexy\ShopBundle\Entity\Product\CategoryProduct;
  9. use App\Flexy\ShopBundle\Entity\Product\ImportExcel;
  10. use App\Flexy\ShopBundle\Entity\Product\Product;
  11. use App\Flexy\ShopBundle\Entity\Product\ProductShop;
  12. use App\Flexy\ShopBundle\Entity\Resource\Agent;
  13. use App\Flexy\ShopBundle\Entity\Store\Store;
  14. use App\Flexy\ShopBundle\Entity\Vendor\Vendor;
  15. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  16. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  22. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  23. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  24. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  25. class EasyAdminSubscriber implements EventSubscriberInterface
  26. {
  27.     public function __construct(private readonly Security $security, private readonly UserPasswordHasherInterface $passwordEncoder, private readonly EntityManagerInterface $em)
  28.     {
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             BeforeEntityPersistedEvent::class => ['beforePersist'],
  34.             BeforeEntityUpdatedEvent::class => ['beforeUpdated'],
  35.             AfterCrudActionEvent::class => ['afterAction']
  36.         ];
  37.     }
  38.     public function afterAction(AfterCrudActionEvent $event){
  39.         
  40.         
  41.         
  42.             
  43.             if($event->getResponseParameters()->get("pageName") == "detail"){
  44.                 $entity $event->getResponseParameters()->get("entity")->getInstance();
  45.                 
  46.                 if ($entity instanceof Order) {
  47.                     $entity->setIsChecked(true);
  48.                     $this->em->persist($entity);
  49.                     $this->em->flush();
  50.                 }
  51.             }
  52.     }
  53.     public function beforePersist(BeforeEntityPersistedEvent $event)
  54.     {
  55.         $entity $event->getEntityInstance();
  56.         
  57.         if ($entity instanceof ImportExcel) {
  58.             
  59.            
  60.            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  61.         
  62.         
  63.             
  64.            $spreadsheet $reader->load("uploads/".$entity->getFile());
  65.            
  66.            
  67.            $spreadsheetToArray $spreadsheet->getActiveSheet()->toArray();
  68.            
  69.            $headers=$spreadsheetToArray[0];
  70.            $dataFiltred=[];
  71.            $allData=[];
  72.            foreach($spreadsheetToArray as $singleRow){
  73.             if($headers == $singleRow){
  74.                 continue;
  75.             }
  76.             foreach($headers as $key=>$value){
  77.                 if($value===null or $value===""){
  78.                     continue;
  79.                 }
  80.                 $dataFiltred[str_replace(" ","",(string) $value)]=$singleRow[$key];
  81.             }
  82.             $allData[]= $dataFiltred;
  83.         }
  84.         dd($allData);
  85.        
  86.        $vendor $this->em->getRepository(Vendor::class)->findOneBy(["user"=>$this->security->getUser()]);
  87.        if($vendor){
  88.            $counterImported=0;
  89.            $counterIgnored 0;
  90.         foreach($allData as $singleData){
  91.            
  92.             $product = new Product();
  93.             
  94.             //BySamir : ToModify
  95.             $product->setName((string)$singleData["NOM_PRODUIT"]);
  96.             $product->setSkuCode((string)$singleData["CODE_EAN"]);
  97.             $product->setName((string)$singleData["NOM_PRODUIT"]);
  98.             $cat1 =  $this->em->getRepository(CategoryProduct::class)->findOneBy(["name"=>$singleData["CAT1"]]);
  99.             $cat2 =  $this->em->getRepository(CategoryProduct::class)->findOneBy(["name"=>$singleData["CAT2"]]);
  100.             $cat3 =  $this->em->getRepository(CategoryProduct::class)->findOneBy(["name"=>$singleData["CAT3"]]);
  101.             $cat4 =  $this->em->getRepository(CategoryProduct::class)->findOneBy(["name"=>$singleData["CAT4"]]);
  102.             $cat5 =  $this->em->getRepository(CategoryProduct::class)->findOneBy(["name"=>$singleData["CAT5"]]);
  103.             $brand =  $this->em->getRepository(Brand::class)->findOneBy(["name"=>$singleData["MARQUE"]]);
  104.             if($cat1){
  105.                 $product->addCategoryProduct($cat1);
  106.             }
  107.             if($cat2){
  108.                 $product->addCategoryProduct($cat2);
  109.             }
  110.             if($cat3){
  111.                 $product->addCategoryProduct($cat3);
  112.             }
  113.             if($cat4){
  114.                 $product->addCategoryProduct($cat4);
  115.             }
  116.             if($cat5){
  117.                 $product->addCategoryProduct($cat5);
  118.             }
  119.             
  120.             if($brand){
  121.                 $product->setBrand($brand);
  122.             }else{
  123.                 $brand = new Brand();
  124.                 $brand->setName((string)$singleData["MARQUE"]);
  125.                 $product->setBrand($brand);
  126.             }
  127.             
  128.             $product->setPrice((float)$singleData["PRIX_VENTE"] * 100 );
  129.             $product->setOldPrice((float)$singleData["ANCIEN_PRIX"] * 100 );
  130.             $product->setQuantity((int)$singleData["STOCK"]);
  131.             if($singleData["IMAGE"]){
  132.                 $newImagePath = static::slugify($singleData["NOM_PRODUIT"]);
  133.                 copy($singleData["IMAGE"], "uploads/".$newImagePath);
  134.                 $product->setImage($newImagePath);
  135.             }
  136.             
  137.             $product->setShortDescription((string)$singleData["DESCRIPTION_COURTE"]);
  138.             $product->setDescription((string)$singleData["DESCRIPTION_COMPLETE"]);
  139.             $product->setVendor($vendor);
  140.             
  141.             $this->em->persist($product);
  142.             
  143.             $counterImported $counterImported 1;
  144.         }
  145.         
  146.         $entity->setTotalLinesImported($counterImported);
  147.         $entity->setTotalLinesIgnored($counterIgnored);
  148.        }
  149.      
  150.            
  151.            
  152.         }
  153.         if ($entity instanceof DemandeFund) {
  154.             foreach($entity->getOrders() as $singleOrder){
  155.                 $singleOrder->setDemandeFund($entity);
  156.                 $entity->addOrder($singleOrder);
  157.             }
  158.            
  159.         }
  160.         if ($entity instanceof Vendor) {
  161.             $entity->setSimulateUsername("");
  162.             $entity->getUser()->setRoles(["ROLE_VENDOR"]);
  163.             $entity->getUser()->setPassword(
  164.                 $this->passwordEncoder->hashPassword(
  165.                          $entity->getUser(),
  166.                          $entity->getUser()->getPassword()
  167.              ));
  168.              //dd($entity);
  169.         }
  170.         if ($entity instanceof Store) {
  171.             $entity->getUser()->setRoles(["ROLE_STORE"]);
  172.             $entity->getUser()->setPassword(
  173.                 $this->passwordEncoder->hashPassword(
  174.                          $entity->getUser(),
  175.                          $entity->getUser()->getPassword()
  176.              ));
  177.         }
  178.         if ($entity instanceof Agent) {
  179.             $entity->getUser()->setRoles(["ROLE_AGENT"]);
  180.             $entity->getUser()->setPassword(
  181.                 $this->passwordEncoder->hashPassword(
  182.                          $entity->getUser(),
  183.                          $entity->getUser()->getPassword()
  184.              ));
  185.              //dd($entity);
  186.         }
  187.         if ($entity instanceof Product ) {
  188.             $entity->setSkuCodeShop("OM".$entity->getCreatedAt()->format("ymdhs").$entity->getId());
  189.             $vendor $this->em->getRepository(Vendor::class)->findOneBy(["user"=>$this->security->getUser()]);
  190.             
  191.             $entity->setVendor($vendor);
  192.            
  193.         }
  194.      
  195.     }
  196.     public function beforeUpdated(BeforeEntityUpdatedEvent $event)
  197.     {
  198.         $entity $event->getEntityInstance();
  199.         
  200.         if ($entity instanceof Product) {
  201.             $imagePath $entity->getImage();
  202.             $entity->setImage($imagePath);
  203.            
  204.         }
  205.         if ($entity instanceof DemandeFund) {
  206.             foreach($entity->getOrders() as $singleOrder){
  207.                 $singleOrder->setDemandeFund($entity);
  208.                 $entity->addOrder($singleOrder);
  209.             }
  210.            
  211.         }
  212.         if ($entity instanceof Vendor) {
  213.             //dd($entity->getUser()->getPassword());
  214.             /*$entity->getUser()->setRoles(["ROLE_VENDOR"]);
  215.             $entity->getUser()->setPassword(
  216.                 $this->passwordEncoder->encodePassword(
  217.                          $entity->getUser(),
  218.                          $entity->getUser()->getPassword()
  219.              ));*/
  220.         }
  221.         if ($entity instanceof Product ) {
  222.             $entity->setSkuCodeShop($entity->getSkuCodeShop().$entity->getId());
  223.             $vendor $this->em->getRepository(Vendor::class)->findOneBy(["user"=>$this->security->getUser()]);
  224.             
  225.             
  226.            
  227.         }
  228.      
  229.     }
  230.     public static function slugify($textstring $divider '-')
  231. {
  232.   // replace non letter or digits by divider
  233.   $text preg_replace('~[^\pL\d]+~u'$divider, (string) $text);
  234.   // transliterate
  235.   $text iconv('utf-8''us-ascii//TRANSLIT'$text);
  236.   // remove unwanted characters
  237.   $text preg_replace('~[^\-\w]+~'''$text);
  238.   // trim
  239.   $text trim($text$divider);
  240.   // remove duplicate divider
  241.   $text preg_replace('~-+~'$divider$text);
  242.   // lowercase
  243.   $text strtolower($text);
  244.   if (empty($text)) {
  245.     return 'n-a';
  246.   }
  247.   return $text;
  248. }
  249. }