app/Plugin/CustomerRank42/Event/AdminProductEvent.php line 65

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\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Event\TemplateEvent;
  16. use Eccube\Repository\ProductClassRepository;
  17. use Plugin\CustomerRank42\Entity\CustomerPrice;
  18. use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
  19. use Plugin\CustomerRank42\Repository\CustomerRankRepository;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class AdminProductEvent implements EventSubscriberInterface
  22. {
  23.     private $entityManager;
  24.     private $productClassRepository;
  25.     private $customerRankRepository;
  26.     private $customerPriceRepository;
  27.     /**
  28.      * CustomerRankController constructor.
  29.      * @param CustomerRankRepository $customerRankRepository
  30.      */
  31.     public function __construct(
  32.             EntityManagerInterface $entityManager,
  33.             ProductClassRepository $productClassRepository,
  34.             CustomerRankRepository $customerRankRepository,
  35.             CustomerPriceRepository $customerPriceRepository
  36.             )
  37.     {
  38.         $this->entityManager $entityManager;
  39.         $this->productClassRepository $productClassRepository;
  40.         $this->customerRankRepository $customerRankRepository;
  41.         $this->customerPriceRepository $customerPriceRepository;
  42.     }
  43.     /**
  44.      * @return array
  45.      */
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  50.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
  51.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  52.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  53.             '@admin/Product/product_class.twig' => 'onTemplateAdminProductClassEdit',
  54.         ];
  55.     }
  56.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  57.     {
  58.         $parameters $event->getParameters();
  59.         $CustomerRanks $this->customerRankRepository->getList();
  60.         $parameters['CustomerRanks'] = $CustomerRanks;
  61.         $event->setParameters($parameters);
  62.         $twig '@CustomerRank42/admin/Product/customer_price.twig';
  63.         $event->addSnippet($twig);
  64.         $js '@CustomerRank42/admin/Product/customer_price.js';
  65.         $event->addAsset($js);
  66.     }
  67.     public function hookAdminProductEditComplete(EventArgs $event)
  68.     {
  69.         $Product $event->getArgument('Product');
  70.         $form $event->getArgument('form');
  71.         $has_class $Product->hasProductClass();
  72.         if(!$has_class){
  73.             $CustomerRanks $this->customerRankRepository->getList();
  74.             $ProductClass $form['class']->getData();
  75.             foreach($CustomerRanks as $CustomerRank){
  76.                 if($form['class']->has('customer_price_'$CustomerRank->getId())){
  77.                     $CustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  78.                     if(!$CustomerPrice){
  79.                         $CustomerPrice =  new CustomerPrice();
  80.                         $CustomerPrice->setProductClass($ProductClass);
  81.                         $CustomerPrice->setCustomerRank($CustomerRank);
  82.                     }
  83.                     $CustomerPrice->setPrice($form['class']->get('customer_price_'$CustomerRank->getId())->getData());
  84.                     $this->entityManager->persist($CustomerPrice);
  85.                 }
  86.             }
  87.             $this->entityManager->flush();
  88.         }
  89.     }
  90.     public function hookAdminProductCopyComplete(EventArgs $event)
  91.     {
  92.         $Product $event->getArgument('Product');
  93.         $CopyProduct $event->getArgument('CopyProduct');
  94.         $orgProductClasses $Product->getProductClasses();
  95.         $CustomerRanks $this->customerRankRepository->getList();
  96.         foreach($CustomerRanks as $CustomerRank){
  97.             foreach ($orgProductClasses as $ProductClass) {
  98.                 $CopyProductClass $this->productClassRepository->findOneBy(['Product'=> $CopyProduct'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
  99.                 $orgCustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  100.                 if($CopyProductClass){
  101.                     $CustomerPrice = new CustomerPrice();
  102.                     $CustomerPrice->setProductClass($CopyProductClass);
  103.                     $CustomerPrice->setCustomerRank($CustomerRank);
  104.                     if($orgCustomerPrice){
  105.                         $CustomerPrice->setPrice($orgCustomerPrice->getPrice());
  106.                     }
  107.                     $this->entityManager->persist($CustomerPrice);
  108.                 }
  109.             }
  110.         }
  111.         $this->entityManager->flush();
  112.     }
  113.     public function hookAdminProductCsvExport(EventArgs $event)
  114.     {
  115.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  116.         if ($ExportCsvRow->isDataNull()) {
  117.             $csvService $event->getArgument('csvService');
  118.             $ProductClass $event->getArgument('ProductClass');
  119.             $Csv $event->getArgument('Csv');
  120.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  121.             if($csvEntityName == 'Plugin\CustomerRank42\Entity\CustomerPrice'){
  122.                 $customer_rank_id ltrim($Csv->getFieldName(), 'customerrank_price_');
  123.                 if(is_numeric($customer_rank_id)){
  124.                     $CustomerRank $this->customerRankRepository->find($customer_rank_id);
  125.                     if(!is_null($CustomerRank)){
  126.                         $CustomerPrice $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass'CustomerRank' => $CustomerRank]);
  127.                         if(!is_null($CustomerPrice)){
  128.                             $ExportCsvRow->setData($CustomerPrice->getPrice());
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.     public function onTemplateAdminProductClassEdit(TemplateEvent $event)
  136.     {
  137.         $parameters $event->getParameters();
  138.         $CustomerRanks $this->customerRankRepository->getList();
  139.         $parameters['CustomerRanks'] = $CustomerRanks;
  140.         $event->setParameters($parameters);
  141.         $twig '@CustomerRank42/admin/Product/customer_price_class.twig';
  142.         $event->addSnippet($twig);
  143.         $js '@CustomerRank42/admin/Product/customer_price_class.js';
  144.         $event->addAsset($js);
  145.     }
  146. }