app/Plugin/SalesRestrictions4/SalesRestrictionsEvent.php line 70

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\SalesRestrictions4;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Service\CartService;
  16. use Plugin\SalesRestrictions4\Repository\ProductCustomerRankRepository;
  17. use Plugin\SalesRestrictions4\Service\SalesRestrictionsService;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. class SalesRestrictionsEvent implements EventSubscriberInterface
  23. {
  24.     private $entityManager;
  25.     private $authorizationChecker;
  26.     private $tokenStorage;
  27.     private $cartService;
  28.     private $productCustomerRankRepository;
  29.     private $salesRestrictionsService;
  30.     public function __construct(
  31.             EntityManagerInterface $entityManager,
  32.             AuthorizationCheckerInterface $authorizationChecker,
  33.             TokenStorageInterface $tokenStorage,
  34.             CartService $cartService,
  35.             ProductCustomerRankRepository $productCustomerRankRepository,
  36.             SalesRestrictionsService $salesRestrictionsService
  37.             )
  38.     {
  39.         $this->entityManager $entityManager;
  40.         $this->authorizationChecker $authorizationChecker;
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->cartService $cartService;
  43.         $this->productCustomerRankRepository $productCustomerRankRepository;
  44.         $this->salesRestrictionsService $salesRestrictionsService;
  45.     }
  46.     /**
  47.      * @return array
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  53.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  54.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  55.             'Product/list.twig' => 'onTemplateProductList',
  56.             'Product/detail.twig' => 'onTemplateProductDetail',
  57.             EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE => 'hookProductCartAddComplete',
  58.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  59.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  60.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  61.         ];
  62.     }
  63.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  64.     {
  65.         $source $event->getSource();
  66.         if(preg_match("/\{\%\sfor\sf\sin\sform\sif\sf\.vars\.eccube\_form\_options\.auto\_render\s\%\}/",$source$result)){
  67.             $search $result[0];
  68.             $replace "{{ include('@SalesRestrictions4/admin/Product/ext_edit.twig') }}" $search;
  69.             $source str_replace($search$replace$source);
  70.         }
  71.         $event->setSource($source);
  72.     }
  73.     public function hookAdminProductCopyComplete(EventArgs $event)
  74.     {
  75.         $Product $event->getArgument('Product');
  76.         $CopyProduct $event->getArgument('CopyProduct');
  77.         $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  78.         if($plgProducts){
  79.             foreach($plgProducts as $plgProduct){
  80.                 $copyPlgProduct = new \Plugin\SalesRestrictions4\Entity\ProductCustomerRank();
  81.                 $copyPlgProduct->setProduct($CopyProduct);
  82.                 $copyPlgProduct->setCustomerRankId($plgProduct->getCustomerRankId());
  83.                 $this->entityManager->persist($copyPlgProduct);
  84.             }
  85.             $this->entityManager->flush();
  86.         }
  87.     }
  88.     public function hookProductCartAddComplete(EventArgs $event)
  89.     {
  90.         $Carts $this->cartService->getCarts();
  91.         foreach ($Carts as $Cart){
  92.             foreach ($Cart->getCartItems() as $CartItem) {
  93.                 $ProductClass $CartItem->getProductClass();
  94.                 if ($ProductClass) {
  95.                     $flg true;
  96.                     $Product $ProductClass->getProduct();
  97.                     $plgProducts $this->productCustomerRankRepository->getRankIdsforProduct($Product);
  98.                     if (count($plgProducts) > && !$this->authorizationChecker->isGranted('ROLE_USER')) {
  99.                         $flg false;
  100.                     }
  101.                     if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank')){
  102.                         if($this->authorizationChecker->isGranted('ROLE_USER')){
  103.                             $Customer $this->tokenStorage->getToken()->getUser();
  104.                             $CustomerRank $Customer->getCustomerRank();
  105.                             if($CustomerRank){
  106.                                 $customer_rank_id $CustomerRank->getId();
  107.                             }else{
  108.                                 $customer_rank_id 0;
  109.                             }
  110.                             if(in_array($customer_rank_id$plgProducts)){
  111.                                 $flg false;
  112.                             }
  113.                         }
  114.                     }
  115.                     if(!$flg){
  116.                         $this->cartService->removeProduct($ProductClass);
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         $this->cartService->save();
  122.     }
  123.     public function onTemplateProductList(TemplateEvent $event)
  124.     {
  125.         $parameters $event->getParameters();
  126.         $Products $parameters['pagination'];
  127.         $hiddenIds = [];
  128.         foreach($Products as $Product){
  129.             $plgProducts $this->productCustomerRankRepository->getRankIdsforProduct($Product);
  130.             if(!$plgProducts)continue;
  131.             if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank')){
  132.                 if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  133.                     if(count($plgProducts) == && in_array(-1$plgProducts))continue;
  134.                     $Customer $this->tokenStorage->getToken()->getUser();
  135.                     $CustomerRank $Customer->getCustomerRank();
  136.                     if($CustomerRank){
  137.                         $customer_rank_id $CustomerRank->getId();
  138.                     }else{
  139.                         $customer_rank_id 0;
  140.                     }
  141.                     if(!in_array($customer_rank_id$plgProducts))continue;
  142.                 }
  143.             }else{
  144.                 if ($this->authorizationChecker->isGranted('ROLE_USER') || !in_array(-1$plgProducts))continue;
  145.             }
  146.             $hiddenIds[] = $Product->getId();
  147.         }
  148.         $parameters['hiddenIds'] = json_encode($hiddenIds);
  149.         $event->setParameters($parameters);
  150.         $event->addSnippet('@SalesRestrictions4/default/Product/restrictions_js.twig');
  151.     }
  152.     public function onTemplateProductDetail(TemplateEvent $event)
  153.     {
  154.         $parameters $event->getParameters();
  155.         $Product $parameters['Product'];
  156.         $plgProducts $this->productCustomerRankRepository->getRankIdsforProduct($Product);
  157.         if(!$plgProducts)return;
  158.         if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank')){
  159.             if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  160.                 if(count($plgProducts) == && in_array(-1$plgProducts))return;
  161.                 $Customer $this->tokenStorage->getToken()->getUser();
  162.                 $CustomerRank $Customer->getCustomerRank();
  163.                 if($CustomerRank){
  164.                     $customer_rank_id $CustomerRank->getId();
  165.                 }else{
  166.                     $customer_rank_id 0;
  167.                 }
  168.                 if(!in_array($customer_rank_id$plgProducts))return;
  169.             }
  170.         }else{
  171.             if ($this->authorizationChecker->isGranted('ROLE_USER') || !in_array(-1$plgProducts))return;
  172.         }
  173.         $source $event->getSource();
  174.         if(preg_match('/<form.*id="form1".*>/',$source$result)){
  175.             $search $result[0];
  176.             $start_idx strpos($source,$search);
  177.             $sub_source substr($source,$start_idx);
  178.             if(preg_match('/<\/form>/',$sub_source$result)){
  179.                 $search $result[0];
  180.                 $end_idx strpos($sub_source,$search);
  181.                 $target substr($sub_source,0,$end_idx+strlen($search));
  182.                 $replace "{{ include('Product/sales_restrictions_cart.twig') }}";
  183.                 $source str_replace($target,$replace,$source);
  184.             }
  185.         }
  186.         $event->setSource($source);
  187.     }
  188.     public function hookAdminProductCsvExport(EventArgs $event)
  189.     {
  190.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  191.         if ($ExportCsvRow->isDataNull()) {
  192.             $csvService $event->getArgument('csvService');
  193.             $ProductClass $event->getArgument('ProductClass');
  194.             $Product $ProductClass->getProduct();
  195.             $Csv $event->getArgument('Csv');
  196.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  197.             if($csvEntityName == 'Plugin\SalesRestrictions4\Entity\ProductCustomerRank'){
  198.                 $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  199.                 $ret = [];
  200.                 foreach($plgProducts as $plgProduct){
  201.                     if($Csv->getFieldName() == 'id'){
  202.                         $ret[] = $plgProduct->getCustomerRankId();
  203.                     }elseif($Csv->getFieldName() == 'name'){
  204.                         $rank_id $plgProduct->getCustomerRankId();
  205.                         if($rank_id == -1){
  206.                             $ret[] = trans('salesrestrictions.form.product.choice.guest');
  207.                         }else {
  208.                             if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank')){
  209.                                 if($rank_id == 0){
  210.                                     $ret[] = trans('salesrestrictions.form.product.choice.empty');
  211.                                 }else{
  212.                                     $customerRankRepository $this->entityManager->getRepository('Plugin\CustomerRank\Entity\CustomerRank');
  213.                                     $CustomerRanks $customerRankRepository->getList();
  214.                                     foreach($CustomerRanks as $CustomerRank){
  215.                                         if($CustomerRank->getId() == $rank_id)
  216.                                             $ret[] = $CustomerRank->getName();
  217.                                     }
  218.                                 }
  219.                             }
  220.                         }
  221.                     }
  222.                 }
  223.                 $value implode(','$ret);
  224.                 $ExportCsvRow->setData($value);
  225.             }
  226.         }
  227.     }
  228.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  229.     {
  230.         $header $event->getArgument('header');
  231.         $key $event->getArgument('key');
  232.         if($key == trans('salesrestrictions.csv.common.id')){
  233.             $header['description'] = trans('salesrestrictions.csv.product.sales_restrictions.description');
  234.             $header['required'] = false;
  235.         }
  236.         $event->setArgument('header',$header);
  237.     }
  238.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  239.     {
  240.         $row $event->getArgument('row');
  241.         $data $event->getArgument('data');
  242.         $errors $event->getArgument('errors');
  243.         if(isset($row[trans('salesrestrictions.csv.common.id')])){
  244.             if($row[trans('salesrestrictions.csv.common.id')] !== ''){
  245.                 $sales_restrictions explode(','$row[trans('salesrestrictions.csv.common.id')]);
  246.                 foreach($sales_restrictions as $value){
  247.                     if($value != '' && !is_numeric($value)){
  248.                         $message trans('salesrestrictions.csv.product.sales_restrictions.error', [
  249.                             '%line%' => $data->key() + 1,
  250.                             '%name%' => trans('salesrestrictions.csv.common.id'),
  251.                         ]);
  252.                         $errors[] = $message;
  253.                         break;
  254.                     }
  255.                 }
  256.             }
  257.         }
  258.         $event->setArgument('errors',$errors);
  259.     }
  260.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  261.     {
  262.         $row $event->getArgument('row');
  263.         $data $event->getArgument('data');
  264.         $ProductClass $event->getArgument('ProductClass');
  265.         if(isset($row[trans('salesrestrictions.csv.common.id')])){
  266.             $Product $ProductClass->getProduct();
  267.             $plgProducts $this->productCustomerRankRepository->findBy(['Product' => $Product]);
  268.             foreach($plgProducts as $plgProduct){
  269.                 $this->entityManager->remove($plgProduct);
  270.             }
  271.             $this->entityManager->flush();
  272.             $sales_restrictions explode(','$row[trans('salesrestrictions.csv.common.id')]);
  273.             if(count($sales_restrictions) > 0){
  274.                 foreach($sales_restrictions as $value){
  275.                     if(is_numeric($value)){
  276.                         $plgProduct = new \Plugin\SalesRestrictions4\Entity\ProductCustomerRank();
  277.                         $plgProduct->setProduct($Product);
  278.                         $plgProduct->setCustomerRankId($value);
  279.                         $this->entityManager->persist($plgProduct);
  280.                     }
  281.                 }
  282.                 $this->entityManager->flush();
  283.             }
  284.         }
  285.     }
  286. }