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

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\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Event\TemplateEvent;
  16. use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
  17. use Plugin\SalesRestrictions42\Service\SalesRestrictionsService;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class AdminProductEvent implements EventSubscriberInterface
  20. {
  21.     private $entityManager;
  22.     private $productCustomerRankRepository;
  23.     private $salesRestrictionsService;
  24.     public function __construct(
  25.             EntityManagerInterface $entityManager,
  26.             ProductCustomerRankRepository $productCustomerRankRepository,
  27.             SalesRestrictionsService $salesRestrictionsService
  28.             )
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->productCustomerRankRepository $productCustomerRankRepository;
  32.         $this->salesRestrictionsService $salesRestrictionsService;
  33.     }
  34.     /**
  35.      * @return array
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  41.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  42.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  43.         ];
  44.     }
  45.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  46.     {
  47.         $source $event->getSource();
  48.         if(preg_match("/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/",$source$result)){
  49.             $search $result[0];
  50.             $replace "{{ include('@SalesRestrictions42/admin/Product/ext_edit.twig') }}" $search;
  51.             $source str_replace($search$replace$source);
  52.         }
  53.         $event->setSource($source);
  54.     }
  55.     public function hookAdminProductCopyComplete(EventArgs $event)
  56.     {
  57.         $Product $event->getArgument('Product');
  58.         $CopyProduct $event->getArgument('CopyProduct');
  59.         $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  60.         if($plgProducts){
  61.             foreach($plgProducts as $plgProduct){
  62.                 $copyPlgProduct = new \Plugin\SalesRestrictions42\Entity\ProductCustomerRank();
  63.                 $copyPlgProduct->setProduct($CopyProduct);
  64.                 $copyPlgProduct->setCustomerRankId($plgProduct->getCustomerRankId());
  65.                 $this->entityManager->persist($copyPlgProduct);
  66.             }
  67.             $this->entityManager->flush();
  68.         }
  69.     }
  70.     public function hookAdminProductCsvExport(EventArgs $event)
  71.     {
  72.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  73.         if ($ExportCsvRow->isDataNull()) {
  74.             $ProductClass $event->getArgument('ProductClass');
  75.             $Product $ProductClass->getProduct();
  76.             $Csv $event->getArgument('Csv');
  77.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  78.             if($csvEntityName == 'Plugin\SalesRestrictions42\Entity\ProductCustomerRank'){
  79.                 $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  80.                 $ret = [];
  81.                 foreach($plgProducts as $plgProduct){
  82.                     if($Csv->getFieldName() == 'id'){
  83.                         $ret[] = $plgProduct->getCustomerRankId();
  84.                     }elseif($Csv->getFieldName() == 'name'){
  85.                         $rank_id $plgProduct->getCustomerRankId();
  86.                         if($rank_id == -1){
  87.                             $ret[] = trans('salesrestrictions.form.product.choice.guest');
  88.                         }else {
  89.                             if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank42')){
  90.                                 if($rank_id == 0){
  91.                                     $ret[] = trans('salesrestrictions.form.product.choice.empty');
  92.                                 }else{
  93.                                     $customerRankRepository $this->entityManager->getRepository('Plugin\CustomerRank42\Entity\CustomerRank');
  94.                                     $CustomerRanks $customerRankRepository->getList();
  95.                                     foreach($CustomerRanks as $CustomerRank){
  96.                                         if($CustomerRank->getId() == $rank_id)
  97.                                             $ret[] = $CustomerRank->getName();
  98.                                     }
  99.                                 }
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.                 $value implode(','$ret);
  105.                 $ExportCsvRow->setData($value);
  106.             }
  107.         }
  108.     }
  109. }