app/Plugin/ProductOption42/Event/CsvImportProductExtEvent.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  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\ProductOption42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\EventArgs;
  14. use Plugin\ProductOption42\Entity\ProductOption;
  15. use Plugin\ProductOption42\Repository\OptionRepository;
  16. use Plugin\ProductOption42\Repository\ProductOptionRepository;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CsvImportProductExtEvent implements EventSubscriberInterface
  19. {
  20.     private $entityManager;
  21.     private $optionRepository;
  22.     private $productOptionRepository;
  23.     public function __construct(
  24.             EntityManagerInterface $entityManager,
  25.             OptionRepository $optionRepository,
  26.             ProductOptionRepository $productOptionRepository
  27.             )
  28.     {
  29.         $this->entityManager $entityManager;
  30.         $this->optionRepository $optionRepository;
  31.         $this->productOptionRepository $productOptionRepository;
  32.     }
  33.     /**
  34.      * @return array
  35.      */
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  40.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  41.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  42.         ];
  43.     }
  44.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  45.     {
  46.         $header $event->getArgument('header');
  47.         $key $event->getArgument('key');
  48.         if($key == trans('productoption.csv.product.label.id')){
  49.             $header['description'] = trans('productoption.admin.product.product_csv.product_assign_description');
  50.             $header['required'] = false;
  51.         }
  52.         $event->setArgument('header',$header);
  53.     }
  54.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  55.     {
  56.         $row $event->getArgument('row');
  57.         $lineNo $event->getArgument('lineNo');
  58.         $errors $event->getArgument('errors');
  59.         if(isset($row[trans('productoption.csv.product.label.id')])){
  60.             if($row[trans('productoption.csv.product.label.id')] !== '' && preg_match("/[^0-9,]/"$row[trans('productoption.csv.product.label.id')])){
  61.                 $message trans('productoption.admin.product.product_csv.not_correct', [
  62.                     '%line%' => $lineNo,
  63.                     '%name%' => trans('productoption.csv.product.label.id'),
  64.                 ]);
  65.                 $errors[] = $message;
  66.             }
  67.         }
  68.         $event->setArgument('errors',$errors);
  69.     }
  70.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  71.     {
  72.         $row $event->getArgument('row');
  73.         $ProductClass $event->getArgument('ProductClass');
  74.         $Product $ProductClass->getProduct();
  75.         if(isset($row[trans('productoption.csv.product.label.id')])){
  76.             // 一旦クリア
  77.             $currentProductOptions $Product->getProductOptions();
  78.             foreach ($currentProductOptions as $currentProductOption) {
  79.                 $this->entityManager->remove($currentProductOption);
  80.                 $Product->removeProductOption($currentProductOption);
  81.             }
  82.             $this->entityManager->persist($Product);
  83.             $this->entityManager->flush();
  84.         }
  85.         if(isset($row[trans('productoption.csv.product.label.id')]) && strlen($row[trans('productoption.csv.product.label.id')]) > 0){
  86.             $productOptions explode(','$row[trans('productoption.csv.product.label.id')]);
  87.             foreach($productOptions as $option_id){
  88.                 if(is_numeric($option_id)){
  89.                     $Option $this->optionRepository->find($option_id);
  90.                     if($Option){
  91.                         $ProductOption = new ProductOption();
  92.                         $ProductOption->setProduct($Product);
  93.                         $ProductOption->setOption($Option);
  94.                         $this->productOptionRepository->save($ProductOption);
  95.                         $Product->addProductOption($ProductOption);
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }