app/Plugin/CustomerRank42/Event/CsvImportProductExtEvent.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  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\CustomerRank42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\EventArgs;
  14. use Plugin\CustomerRank42\Entity\CustomerPrice;
  15. use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
  16. use Plugin\CustomerRank42\Repository\CustomerRankRepository;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CsvImportProductExtEvent implements EventSubscriberInterface
  19. {
  20.     private $entityManager;
  21.     private $customerRankRepository;
  22.     private $customerPriceRepository;
  23.     /**
  24.      * CustomerRankController constructor.
  25.      * @param CustomerRankRepository $customerRankRepository
  26.      */
  27.     public function __construct(
  28.             EntityManagerInterface $entityManager,
  29.             CustomerRankRepository $customerRankRepository,
  30.             CustomerPriceRepository $customerPriceRepository
  31.             )
  32.     {
  33.         $this->entityManager $entityManager;
  34.         $this->customerRankRepository $customerRankRepository;
  35.         $this->customerPriceRepository $customerPriceRepository;
  36.     }
  37.     /**
  38.      * @return array
  39.      */
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  44.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  45.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  46.         ];
  47.     }
  48.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  49.     {
  50.         $header $event->getArgument('header');
  51.         $key $event->getArgument('key');
  52.         $CustomerRanks $this->customerRankRepository->findAll();
  53.         foreach($CustomerRanks as $CustomerRank){
  54.             if($key == $CustomerRank->getName() . trans('customerrank.common.customer_price')){
  55.                 $header['description'] = trans('customerrank.admin.product.product_csv.customer_price_description');
  56.                 $header['required'] = false;
  57.             }
  58.         }
  59.         $event->setArgument('header',$header);
  60.     }
  61.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  62.     {
  63.         $row $event->getArgument('row');
  64.         $lineNo $event->getArgument('lineNo');
  65.         $errors $event->getArgument('errors');
  66.         $CustomerRanks $this->customerRankRepository->findAll();
  67.         foreach($CustomerRanks as $CustomerRank){
  68.             if(isset($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')])){
  69.                 if($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')] !== '' && !is_numeric($row[$CustomerRank->getName(). trans('customerrank.common.customer_price')])){
  70.                     $message trans('admin.common.csv_invalid_greater_than_zero', [
  71.                         '%line%' => $lineNo,
  72.                         '%name%' => $CustomerRank->getName(). trans('customerrank.common.customer_price'),
  73.                     ]);
  74.                     $errors[] = $message;
  75.                 }
  76.             }
  77.         }
  78.         $event->setArgument('errors',$errors);
  79.     }
  80.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  81.     {
  82.         $row $event->getArgument('row');
  83.         $ProductClass $event->getArgument('ProductClass');
  84.         $CustomerRanks $this->customerRankRepository->findAll();
  85.         foreach($CustomerRanks as $CustomerRank){
  86.             if(isset($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')])){
  87.                 $plgPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  88.                 if($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')] != ''){
  89.                     if(is_null($plgPrice)){
  90.                         $plgPrice = new CustomerPrice();
  91.                         $plgPrice->setProductClass($ProductClass);
  92.                         $plgPrice->setCustomerRank($CustomerRank);
  93.                     }
  94.                     $plgPrice->setPrice($row[$CustomerRank->getName() . trans('customerrank.common.customer_price')]);
  95.                     $this->entityManager->persist($plgPrice);
  96.                 }else{
  97.                     if(isset($plgPrice))$this->entityManager->remove($plgPrice);
  98.                 }
  99.             }
  100.             if(isset($plgPrice))unset($plgPrice);
  101.         }
  102.     }
  103. }