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.             if($entity->getUser()->getUserIdentifier() == null or $entity->getUser()->getPassword() == null){
  180.                 $entity->getUser()->setUsername(static::slugify($entity->getFirstName().$entity->getLastName().$entity->getPhone()));
  181.                 $entity->getUser()->setPassword(static::slugify($entity->getFirstName().$entity->getLastName().$entity->getPhone()));
  182.             }
  183.             $entity->getUser()->setRoles(["ROLE_AGENT"]);
  184.             $entity->getUser()->setPassword(
  185.                 $this->passwordEncoder->hashPassword(
  186.                          $entity->getUser(),
  187.                          $entity->getUser()->getPassword()
  188.              ));
  189.              
  190.                 
  191.     
  192.             
  193.         }
  194.         if ($entity instanceof Product ) {
  195.             $entity->setSkuCodeShop("OM".$entity->getCreatedAt()->format("ymdhs").$entity->getId());
  196.             $vendor $this->em->getRepository(Vendor::class)->findOneBy(["user"=>$this->security->getUser()]);
  197.             
  198.             $entity->setVendor($vendor);
  199.            
  200.         }
  201.      
  202.     }
  203.     public function beforeUpdated(BeforeEntityUpdatedEvent $event)
  204.     {
  205.         $entity $event->getEntityInstance();
  206.         
  207.         if ($entity instanceof Product) {
  208.             $imagePath $entity->getImage();
  209.             $entity->setImage($imagePath);
  210.            
  211.         }
  212.         if ($entity instanceof DemandeFund) {
  213.             foreach($entity->getOrders() as $singleOrder){
  214.                 $singleOrder->setDemandeFund($entity);
  215.                 $entity->addOrder($singleOrder);
  216.             }
  217.            
  218.         }
  219.         if ($entity instanceof Vendor) {
  220.             //dd($entity->getUser()->getPassword());
  221.             /*$entity->getUser()->setRoles(["ROLE_VENDOR"]);
  222.             $entity->getUser()->setPassword(
  223.                 $this->passwordEncoder->encodePassword(
  224.                          $entity->getUser(),
  225.                          $entity->getUser()->getPassword()
  226.              ));*/
  227.         }
  228.         if ($entity instanceof Product ) {
  229.             $entity->setSkuCodeShop($entity->getSkuCodeShop().$entity->getId());
  230.             $vendor $this->em->getRepository(Vendor::class)->findOneBy(["user"=>$this->security->getUser()]);
  231.             
  232.             
  233.            
  234.         }
  235.      
  236.     }
  237.     public static function slugify($textstring $divider '-')
  238. {
  239.   // replace non letter or digits by divider
  240.   $text preg_replace('~[^\pL\d]+~u'$divider, (string) $text);
  241.   // transliterate
  242.   $text iconv('utf-8''us-ascii//TRANSLIT'$text);
  243.   // remove unwanted characters
  244.   $text preg_replace('~[^\-\w]+~'''$text);
  245.   // trim
  246.   $text trim($text$divider);
  247.   // remove duplicate divider
  248.   $text preg_replace('~-+~'$divider$text);
  249.   // lowercase
  250.   $text strtolower($text);
  251.   if (empty($text)) {
  252.     return 'n-a';
  253.   }
  254.   return $text;
  255. }
  256. }