<?php
/*
* Plugin Name : SalesRestrictions4
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\SalesRestrictions42\Event;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Service\CartService;
use Plugin\SalesRestrictions42\Entity\SalesRestrictionsConfig;
use Plugin\SalesRestrictions42\Repository\ConfigRepository;
use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
use Plugin\SalesRestrictions42\Service\SalesRestrictionsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ProductEvent implements EventSubscriberInterface
{
private $authorizationChecker;
private $tokenStorage;
private $cartService;
private $configRepository;
private $productCustomerRankRepository;
private $salesRestrictionsService;
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage,
CartService $cartService,
ConfigRepository $configRepository,
ProductCustomerRankRepository $productCustomerRankRepository,
SalesRestrictionsService $salesRestrictionsService
)
{
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
$this->cartService = $cartService;
$this->configRepository = $configRepository;
$this->productCustomerRankRepository = $productCustomerRankRepository;
$this->salesRestrictionsService = $salesRestrictionsService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onTemplateProductList',
'Product/detail.twig' => 'onTemplateProductDetail',
EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'hookProductDetailInitialize',
EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE => 'hookProductCartAddComplete',
];
}
public function onTemplateProductList(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Products = $parameters['pagination'];
$hiddenIds = [];
foreach($Products as $Product){
if($this->checkAccess($Product))continue;
$hiddenIds[] = $Product->getId();
}
$parameters['hiddenIds'] = json_encode($hiddenIds);
$event->setParameters($parameters);
$event->addSnippet('@SalesRestrictions42/default/Product/restrictions_js.twig');
}
public function onTemplateProductDetail(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Product = $parameters['Product'];
if($this->checkAccess($Product))return;
$source = $event->getSource();
if(preg_match('/<form.*id="form1".*>/',$source, $result)){
$search = $result[0];
$start_idx = strpos($source,$search);
$sub_source = substr($source,$start_idx);
if(preg_match('/<\/form>/',$sub_source, $result)){
$search = $result[0];
$end_idx = strpos($sub_source,$search);
$target = substr($sub_source,0,$end_idx+strlen($search));
$replace = "{{ include('Product/sales_restrictions_cart.twig') }}";
$source = str_replace($target,$replace,$source);
}
}
$event->setSource($source);
}
public function hookProductDetailInitialize(EventArgs $event)
{
$Product = $event->getArgument('Product');
$Condition = $this->configRepository->findOneBy(['name' => SalesRestrictionsConfig::DEFAULT_NAME]);
if(!$Condition)return;
if($Condition->getValue() == SalesRestrictionsConfig::DISPLAY)return;
if($this->checkAccess($Product))return;
throw new NotFoundHttpException();
}
public function hookProductCartAddComplete(EventArgs $event)
{
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart){
foreach ($Cart->getCartItems() as $CartItem) {
$ProductClass = $CartItem->getProductClass();
if ($ProductClass) {
$Product = $ProductClass->getProduct();
if(!$this->checkAccess($Product))$this->cartService->removeProduct($ProductClass);
}
}
}
$this->cartService->save();
}
private function checkAccess($Product)
{
$plgProducts = $this->productCustomerRankRepository->getRankIdsforProduct($Product);
if(!$plgProducts)return true;
if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank42')){
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
if(count($plgProducts) == 1 && in_array(-1, $plgProducts))return true;
$Customer = $this->tokenStorage->getToken()->getUser();
$CustomerRank = $Customer->getCustomerRank();
if($CustomerRank){
$customer_rank_id = $CustomerRank->getId();
}else{
$customer_rank_id = 0;
}
if(!in_array($customer_rank_id, $plgProducts))return true;
}
}else{
if ($this->authorizationChecker->isGranted('ROLE_USER') || !in_array(-1, $plgProducts))return true;
}
return false;
}
}