app/Plugin/SalesRestrictions42/Event/CsvImportProductExtEvent.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : SalesRestrictions4
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\SalesRestrictions42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\EventArgs;
  14. use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CsvImportProductExtEvent implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private $productCustomerRankRepository;
  20.     public function __construct(
  21.             EntityManagerInterface $entityManager,
  22.             ProductCustomerRankRepository $productCustomerRankRepository
  23.             )
  24.     {
  25.         $this->entityManager $entityManager;
  26.         $this->productCustomerRankRepository $productCustomerRankRepository;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  35.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  36.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  37.         ];
  38.     }
  39.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  40.     {
  41.         $header $event->getArgument('header');
  42.         $key $event->getArgument('key');
  43.         if($key == trans('salesrestrictions.csv.common.id')){
  44.             $header['description'] = trans('salesrestrictions.csv.product.sales_restrictions.description');
  45.             $header['required'] = false;
  46.         }
  47.         $event->setArgument('header',$header);
  48.     }
  49.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  50.     {
  51.         $row $event->getArgument('row');
  52.         $lineNo $event->getArgument('lineNo');
  53.         $errors $event->getArgument('errors');
  54.         if(isset($row[trans('salesrestrictions.csv.common.id')])){
  55.             if($row[trans('salesrestrictions.csv.common.id')] !== ''){
  56.                 $sales_restrictions explode(','$row[trans('salesrestrictions.csv.common.id')]);
  57.                 foreach($sales_restrictions as $value){
  58.                     if($value != '' && !is_numeric($value)){
  59.                         $message trans('salesrestrictions.csv.product.sales_restrictions.error', [
  60.                             '%line%' => $lineNo,
  61.                             '%name%' => trans('salesrestrictions.csv.common.id'),
  62.                         ]);
  63.                         $errors[] = $message;
  64.                         break;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         $event->setArgument('errors',$errors);
  70.     }
  71.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  72.     {
  73.         $row $event->getArgument('row');
  74.         $ProductClass $event->getArgument('ProductClass');
  75.         if(isset($row[trans('salesrestrictions.csv.common.id')])){
  76.             $Product $ProductClass->getProduct();
  77.             $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  78.             foreach($plgProducts as $plgProduct){
  79.                 $this->entityManager->remove($plgProduct);
  80.             }
  81.             $this->entityManager->flush();
  82.             $sales_restrictions explode(','$row[trans('salesrestrictions.csv.common.id')]);
  83.             if(count($sales_restrictions) > 0){
  84.                 foreach($sales_restrictions as $value){
  85.                     if(is_numeric($value)){
  86.                         $plgProduct = new \Plugin\SalesRestrictions42\Entity\ProductCustomerRank();
  87.                         $plgProduct->setProduct($Product);
  88.                         $plgProduct->setCustomerRankId($value);
  89.                         $this->entityManager->persist($plgProduct);
  90.                     }
  91.                 }
  92.                 $this->entityManager->flush();
  93.             }
  94.         }
  95.     }
  96. }