app/Plugin/SalesRestrictions42/Event/ProductEvent.php line 83

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 Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Service\CartService;
  16. use Plugin\SalesRestrictions42\Entity\SalesRestrictionsConfig;
  17. use Plugin\SalesRestrictions42\Repository\ConfigRepository;
  18. use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
  19. use Plugin\SalesRestrictions42\Service\SalesRestrictionsService;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  24. class ProductEvent implements EventSubscriberInterface
  25. {
  26.     private $authorizationChecker;
  27.     private $tokenStorage;
  28.     private $cartService;
  29.     private $configRepository;
  30.     private $productCustomerRankRepository;
  31.     private $salesRestrictionsService;
  32.     public function __construct(
  33.             AuthorizationCheckerInterface $authorizationChecker,
  34.             TokenStorageInterface $tokenStorage,
  35.             CartService $cartService,
  36.             ConfigRepository $configRepository,
  37.             ProductCustomerRankRepository $productCustomerRankRepository,
  38.             SalesRestrictionsService $salesRestrictionsService
  39.             )
  40.     {
  41.         $this->authorizationChecker $authorizationChecker;
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->cartService $cartService;
  44.         $this->configRepository $configRepository;
  45.         $this->productCustomerRankRepository $productCustomerRankRepository;
  46.         $this->salesRestrictionsService $salesRestrictionsService;
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             'Product/list.twig' => 'onTemplateProductList',
  55.             'Product/detail.twig' => 'onTemplateProductDetail',
  56.             EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'hookProductDetailInitialize',
  57.             EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE => 'hookProductCartAddComplete',
  58.         ];
  59.     }
  60.     public function onTemplateProductList(TemplateEvent $event)
  61.     {
  62.         $parameters $event->getParameters();
  63.         $Products $parameters['pagination'];
  64.         $hiddenIds = [];
  65.         foreach($Products as $Product){
  66.             if($this->checkAccess($Product))continue;
  67.             $hiddenIds[] = $Product->getId();
  68.         }
  69.         $parameters['hiddenIds'] = json_encode($hiddenIds);
  70.         $event->setParameters($parameters);
  71.         $event->addSnippet('@SalesRestrictions42/default/Product/restrictions_js.twig');
  72.     }
  73.     public function onTemplateProductDetail(TemplateEvent $event)
  74.     {
  75.         $parameters $event->getParameters();
  76.         $Product $parameters['Product'];
  77.         if($this->checkAccess($Product))return;
  78.         $source $event->getSource();
  79.         if(preg_match('/<form.*id="form1".*>/',$source$result)){
  80.             $search $result[0];
  81.             $start_idx strpos($source,$search);
  82.             $sub_source substr($source,$start_idx);
  83.             if(preg_match('/<\/form>/',$sub_source$result)){
  84.                 $search $result[0];
  85.                 $end_idx strpos($sub_source,$search);
  86.                 $target substr($sub_source,0,$end_idx+strlen($search));
  87.                 $replace "{{ include('Product/sales_restrictions_cart.twig') }}";
  88.                 $source str_replace($target,$replace,$source);
  89.             }
  90.         }
  91.         $event->setSource($source);
  92.     }
  93.     public function hookProductDetailInitialize(EventArgs $event)
  94.     {
  95.         $Product $event->getArgument('Product');
  96.         $Condition $this->configRepository->findOneBy(['name' => SalesRestrictionsConfig::DEFAULT_NAME]);
  97.         if(!$Condition)return;
  98.         if($Condition->getValue() == SalesRestrictionsConfig::DISPLAY)return;
  99.         if($this->checkAccess($Product))return;
  100.         throw new NotFoundHttpException();
  101.     }
  102.     public function hookProductCartAddComplete(EventArgs $event)
  103.     {
  104.         $Carts $this->cartService->getCarts();
  105.         foreach ($Carts as $Cart){
  106.             foreach ($Cart->getCartItems() as $CartItem) {
  107.                 $ProductClass $CartItem->getProductClass();
  108.                 if ($ProductClass) {
  109.                     $Product $ProductClass->getProduct();
  110.                     if(!$this->checkAccess($Product))$this->cartService->removeProduct($ProductClass);
  111.                 }
  112.             }
  113.         }
  114.         $this->cartService->save();
  115.     }
  116.     private function checkAccess($Product)
  117.     {
  118.         $plgProducts $this->productCustomerRankRepository->getRankIdsforProduct($Product);
  119.         if(!$plgProducts)return true;
  120.         if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank42')){
  121.             if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  122.                 if(count($plgProducts) == && in_array(-1$plgProducts))return true;
  123.                 $Customer $this->tokenStorage->getToken()->getUser();
  124.                 $CustomerRank $Customer->getCustomerRank();
  125.                 if($CustomerRank){
  126.                     $customer_rank_id $CustomerRank->getId();
  127.                 }else{
  128.                     $customer_rank_id 0;
  129.                 }
  130.                 if(!in_array($customer_rank_id$plgProducts))return true;
  131.             }
  132.         }else{
  133.             if ($this->authorizationChecker->isGranted('ROLE_USER') || !in_array(-1$plgProducts))return true;
  134.         }
  135.         return false;
  136.     }
  137. }