app/Plugin/ProductPlus/ProductPlusEvent.php line 286

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductPlus
  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\ProductPlus;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Event\TemplateEvent;
  17. use Plugin\ProductPlus\Entity\ProductItem;
  18. use Plugin\ProductPlus\Entity\ProductData;
  19. use Plugin\ProductPlus\Entity\ProductDataDetail;
  20. use Plugin\ProductPlus\Repository\ProductItemRepository;
  21. use Plugin\ProductPlus\Repository\ProductDataRepository;
  22. use Plugin\ProductPlus\Repository\ProductDataDetailRepository;
  23. use Plugin\ProductPlus\Service\ProductPlusService;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\Filesystem\Filesystem;
  26. use Symfony\Component\HttpFoundation\File\File;
  27. class ProductPlusEvent implements EventSubscriberInterface
  28. {
  29.     private $entityManager;
  30.     private $eccubeConfig;
  31.     private $productItemRepository;
  32.     private $productDataRepository;
  33.     private $productDataDetailRepository;
  34.     private $productPlusService;
  35.     public function __construct(
  36.             EntityManagerInterface $entityManager,
  37.             EccubeConfig $eccubeConfig,
  38.             ProductItemRepository $productItemRepository,
  39.             ProductDataRepository $productDataRepository,
  40.             ProductDataDetailRepository $productDataDetailRepository,
  41.             ProductPlusService $productPlusService
  42.             )
  43.     {
  44.         $this->entityManager $entityManager;
  45.         $this->eccubeConfig $eccubeConfig;
  46.         $this->productItemRepository $productItemRepository;
  47.         $this->productDataRepository $productDataRepository;
  48.         $this->productDataDetailRepository $productDataDetailRepository;
  49.         $this->productPlusService $productPlusService;
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  58.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
  59.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  60.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  61.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  62.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  63.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  64.         ];
  65.     }
  66.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  67.     {
  68.         $parameters $event->getParameters();
  69.         $ProductItems $this->productItemRepository->getList();
  70.         $EnabledItems $this->productPlusService->getEnabledProductPlusForm();
  71.         foreach($ProductItems as $key => $ProductItem){
  72.             foreach($EnabledItems as $EnabledItem){
  73.                 if($ProductItem->getId() == $EnabledItem->getId())unset($ProductItems[$key]);
  74.             }
  75.         }
  76.         $parameters['ProductItems'] = $ProductItems;
  77.         $event->setParameters($parameters);
  78.         $source $event->getSource();
  79.         if(preg_match("/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/",$source$result)){
  80.             $search $result[0];
  81.             $replace "{{ include('@ProductPlus/admin/Product/ext_edit.twig') }}" $search;
  82.             $source str_replace($search$replace$source);
  83.         }
  84.         $event->setSource($source);
  85.         $event->addSnippet('@ProductPlus/admin/Product/upload_js.twig');
  86.         $twig '@ProductPlus/admin/Product/datepicker_js.twig';
  87.         $event->addAsset($twig);
  88.     }
  89.     public function hookAdminProductEditComplete(EventArgs $event)
  90.     {
  91.         $Product $event->getArgument('Product');
  92.         $form $event->getArgument('form');
  93.         $request $event->getRequest();
  94.         $ProductItems $this->productItemRepository->getList();
  95.         foreach($ProductItems as $ProductItem){
  96.             if($form->has('productplus_'.$ProductItem->getId())){
  97.                 $ProductData $this->productDataRepository->findOneBy(['ProductItem' => $ProductItem'Product' => $Product]);
  98.                 if(!$ProductData){
  99.                     $ProductData = new ProductData();
  100.                     $ProductData->setProductItem($ProductItem);
  101.                     $ProductData->setProduct($Product);
  102.                 }
  103.                 if($ProductItem->getInputType() == ProductItem::IMAGE_TYPE) {
  104.                     $add_images $form->get('productplus_'.$ProductItem->getId().'_add_images')->getData();
  105.                     $i 0;
  106.                     foreach ($add_images as $add_image) {
  107.                         $i++;
  108.                         $Detail = new ProductDataDetail();
  109.                         $Detail
  110.                             ->setValue($add_image)
  111.                             ->setProductData($ProductData)
  112.                             ->setSortNo($i);
  113.                         $ProductData->addDetail($Detail);
  114.                         $this->entityManager->persist($Detail);
  115.                         $file = new File($this->eccubeConfig['eccube_temp_image_dir'].'/'.$add_image);
  116.                         $file->move($this->eccubeConfig['eccube_save_image_dir']);
  117.                     }
  118.                     $delete_images $form->get('productplus_'.$ProductItem->getId().'_delete_images')->getData();
  119.                     foreach ($delete_images as $delete_image) {
  120.                         $Detail $this->productDataDetailRepository->findOneBy(['value' => $delete_image]);
  121.                         if ($Detail instanceof ProductDataDetail) {
  122.                             $ProductData->removeDetail($Detail);
  123.                             $this->entityManager->remove($Detail);
  124.                         }
  125.                         $this->entityManager->persist($ProductData);
  126.                         $fs = new Filesystem();
  127.                         $fs->remove($this->eccubeConfig['eccube_save_image_dir'].'/'.$delete_image);
  128.                     }
  129.                     $this->entityManager->persist($ProductData);
  130.                     $Product->addProductData($ProductData);
  131.                     $this->entityManager->flush();
  132.                     $sortNos $request->get('productplus_'.$ProductItem->getId().'_sort_no_images');
  133.                     if ($sortNos) {
  134.                         foreach ($sortNos as $sortNo) {
  135.                             list($filename$sortNo_val) = explode('//'$sortNo);
  136.                             $Detail $this->productDataDetailRepository
  137.                                 ->findOneBy([
  138.                                     'value' => $filename,
  139.                                     'ProductData' => $ProductData,
  140.                                 ]);
  141.                             if($Detail){
  142.                                 $Detail->setSortNo($sortNo_val);
  143.                                 $this->entityManager->persist($Detail);
  144.                             }
  145.                         }
  146.                     }
  147.                     $this->entityManager->flush();
  148.                 }else{
  149.                     $value $form->get('productplus_'.$ProductItem->getId())->getData();
  150.                     if ($value instanceof \DateTime){
  151.                         $value $value->format('Y-m-d');
  152.                     }elseif ($ProductItem->getInputType() == ProductItem::CHECKBOX_TYPE && is_array($value)) {
  153.                         $value implode(','$value);
  154.                     }
  155.                     $ProductData $this->productDataRepository->regist($ProductData$ProductItem$value);
  156.                     $Product->addProductData($ProductData);
  157.                 }
  158.             }
  159.         }
  160.     }
  161.     public function hookAdminProductCopyComplete(EventArgs $event)
  162.     {
  163.         $Product $event->getArgument('Product');
  164.         $CopyProduct $event->getArgument('CopyProduct');
  165.         foreach ($Product->getProductDatas() as $oldProductData) {
  166.             $newProductData = new ProductData();
  167.             $newProductData->setProduct($CopyProduct);
  168.             $newProductData->setProductItem($oldProductData->getProductItem());
  169.             if($newProductData->getProductItem()->getInputType() == ProductItem::IMAGE_TYPE){
  170.                 foreach($oldProductData->getDetails() as $oldDetail){
  171.                     // 画像ファイルを新規作成
  172.                     $extension pathinfo($oldDetail->getValue(), PATHINFO_EXTENSION);
  173.                     $filename date('mdHis').uniqid('_').'.'.$extension;
  174.                     try {
  175.                         $fs = new Filesystem();
  176.                         $fs->copy($this->eccubeConfig['eccube_save_image_dir'].'/'.$oldDetail->getValue(), $this->eccubeConfig['eccube_save_image_dir'].'/'.$filename);
  177.                     } catch (\Exception $e) {
  178.                         // エラーが発生しても無視する
  179.                     }
  180.                     $newDetail = new ProductDataDetail();
  181.                     $newDetail->setValue($filename)
  182.                             ->setSortNo($oldDetail->getSortNo())
  183.                             ->setProductData($newProductData);
  184.                     $newProductData->addDetail($newDetail);
  185.                 }
  186.             }else{
  187.                 foreach($oldProductData->getDetails() as $oldDetail){
  188.                     $newDetail = new ProductDataDetail();
  189.                     $newDetail->setValue($oldDetail->getValue())
  190.                             ->setNumValue($oldDetail->getNumValue())
  191.                             ->setDateValue($oldDetail->getDateValue())
  192.                             ->setProductData($newProductData);
  193.                     $newProductData->addDetail($newDetail);
  194.                 }
  195.             }
  196.             $CopyProduct->addProductData($newProductData);
  197.             $this->entityManager->persist($newProductData);
  198.         }
  199.         $this->entityManager->persist($CopyProduct);
  200.         $this->entityManager->flush();
  201.     }
  202.     public function hookAdminProductCsvExport(EventArgs $event)
  203.     {
  204.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  205.         if ($ExportCsvRow->isDataNull()) {
  206.             $ProductClass $event->getArgument('ProductClass');
  207.             $Product $ProductClass->getProduct();
  208.             $Csv $event->getArgument('Csv');
  209.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  210.             $value null;
  211.             if($csvEntityName == 'Plugin\ProductPlus\Entity\ProductData'){
  212.                 $product_item_id $Csv->getReferenceFieldName();
  213.                 if($Csv->getFieldName() == 'product_item_id'){
  214.                     $value $Product->getIdData($product_item_id);
  215.                 }elseif($Csv->getFieldName() == 'product_item_value'){
  216.                     $value $Product->getValueData($product_item_id);
  217.                 }
  218.                 if(is_array($value))$value implode(','$value);
  219.                 $ExportCsvRow->setData($value);
  220.             }
  221.         }
  222.     }
  223.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  224.     {
  225.         $header $event->getArgument('header');
  226.         $key $event->getArgument('key');
  227.         $ProductItems $this->productItemRepository->getList();
  228.         foreach($ProductItems as $ProductItem){
  229.             if($key == $ProductItem->getName() . trans('productplus.csv.common.id')){
  230.                 $header['description'] = trans('productplus.admin.product.product_csv.product_plus.id_description');
  231.                 $header['required'] = false;
  232.             }elseif($key == $ProductItem->getName() && $ProductItem->getInputType() == ProductItem::IMAGE_TYPE){
  233.                 $header['description'] = trans('productplus.admin.product.product_csv.product_plus.image_description');
  234.                 $header['required'] = false;
  235.             }
  236.         }
  237.         $event->setArgument('header',$header);
  238.     }
  239.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  240.     {
  241.         $row $event->getArgument('row');
  242.         $data $event->getArgument('data');
  243.         $errors $event->getArgument('errors');
  244.         $ProductItems $this->productItemRepository->getList();
  245.         foreach($ProductItems as $ProductItem){
  246.             if(isset($row[$ProductItem->getName() . trans('productplus.csv.common.id')])){
  247.                 if($row[$ProductItem->getName() . trans('productplus.csv.common.id')] !== '' && preg_match("/[^0-9,]/"$row[$ProductItem->getName() . trans('productplus.csv.common.id')])){
  248.                     $message trans('productplus.admin.product.product_csv.not_correct', [
  249.                         '%line%' => $data->key() + 1,
  250.                         '%name%' => $ProductItem->getName() . trans('productplus.csv.common.id'),
  251.                     ]);
  252.                     $errors[] = $message;
  253.                 }
  254.             }
  255.         }
  256.         $event->setArgument('errors',$errors);
  257.     }
  258.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  259.     {
  260.         $row $event->getArgument('row');
  261.         $ProductClass $event->getArgument('ProductClass');
  262.         $Product $ProductClass->getProduct();
  263.         $ProductItems $this->productItemRepository->getList();
  264.         foreach($ProductItems as $ProductItem){
  265.             if(isset($row[$ProductItem->getName() . trans('productplus.csv.common.id')])
  266.              || isset($row[$ProductItem->getName()])){
  267.                 $ProductData $this->productDataRepository->findOneBy(['ProductItem' => $ProductItem'Product' => $Product]);
  268.                 if(!$ProductData){
  269.                     $ProductData = new ProductData();
  270.                     $ProductData->setProductItem($ProductItem);
  271.                     $ProductData->setProduct($Product);
  272.                 }
  273.                 if(isset($row[$ProductItem->getName() . trans('productplus.csv.common.id')])){
  274.                     $value $row[$ProductItem->getName() . trans('productplus.csv.common.id')];
  275.                     if (is_array($value)) {
  276.                         $value implode(','$value);
  277.                     }
  278.                 }
  279.                 if(isset($row[$ProductItem->getName()])){
  280.                     $value $row[$ProductItem->getName()];
  281.                 }
  282.                 if($ProductItem->getInputType() == ProductItem::IMAGE_TYPE) {
  283.                     foreach($ProductData->getDetails() as $removeDetail){
  284.                         $ProductData->removeDetail($removeDetail);
  285.                         $this->entityManager->remove($removeDetail);
  286.                     }
  287.                     $arrValue explode(',',$value);
  288.                     $sortNo 0;
  289.                     foreach($arrValue as $value){
  290.                         if(strlen($value) == 0)continue;
  291.                         $Detail = new ProductDataDetail();
  292.                         $Detail
  293.                             ->setValue($value)
  294.                             ->setProductData($ProductData)
  295.                             ->setSortNo(++$sortNo);
  296.                         $ProductData->addDetail($Detail);
  297.                         $this->entityManager->persist($Detail);
  298.                     }
  299.                     $this->entityManager->persist($ProductData);
  300.                     $Product->addProductData($ProductData);
  301.                     $this->entityManager->flush();
  302.                 }else{
  303.                     $ProductData $this->productDataRepository->regist($ProductData$ProductItem$value);
  304.                     $Product->addProductData($ProductData);
  305.                 }
  306.             }else{
  307.             }
  308.         }
  309.     }
  310. }