app/Plugin/KokokaraSelect/EventSubscriber/KernelEventsSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2020 SYSTEM_KD
  4.  * Date: 2020/05/17
  5.  */
  6. namespace Plugin\KokokaraSelect\EventSubscriber;
  7. use Eccube\Entity\Product;
  8. use Eccube\Repository\ProductRepository;
  9. use Eccube\Service\CartService;
  10. use Plugin\KokokaraSelect\Controller\KokokaraSelectController;
  11. use Plugin\KokokaraSelect\Entity\KsCartSelectItem;
  12. use Plugin\KokokaraSelect\Entity\KsCartSelectItemGroup;
  13. use Plugin\KokokaraSelect\Service\ConfigHelperTrait;
  14. use Plugin\KokokaraSelect\Service\KsCartHelper;
  15. use Plugin\KokokaraSelect\Service\KsService;
  16. use Plugin\KokokaraSelect\Service\VersionHelperTrait;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Session\Session;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. use Symfony\Component\Routing\RouterInterface;
  26. class KernelEventsSubscriber implements EventSubscriberInterface
  27. {
  28.     use ConfigHelperTrait;
  29.     use VersionHelperTrait;
  30.     protected $router;
  31.     /** @var Session */
  32.     protected $session;
  33.     /** @var ProductRepository */
  34.     protected $productRepository;
  35.     /** @var CartService */
  36.     protected $cartService;
  37.     /** @var KsCartHelper */
  38.     protected $ksCartHelper;
  39.     /** @var KsService */
  40.     protected $ksService;
  41.     /** @var ContainerInterface */
  42.     protected $container;
  43.     public function __construct(
  44.         RouterInterface $router,
  45.         SessionInterface $session,
  46.         ProductRepository $productRepository,
  47.         CartService $cartService,
  48.         KsCartHelper $ksCartHelper,
  49.         KsService $ksService,
  50.         ContainerInterface $container
  51.     )
  52.     {
  53.         $this->router $router;
  54.         $this->session $session;
  55.         $this->productRepository $productRepository;
  56.         $this->cartService $cartService;
  57.         $this->ksCartHelper $ksCartHelper;
  58.         $this->ksService $ksService;
  59.         $this->container $container;
  60.     }
  61.     /**
  62.      * コントローラ制御
  63.      *
  64.      * @param FilterControllerEvent $event
  65.      */
  66.     public function onControllerBefore(FilterControllerEvent $event)
  67.     {
  68.         $request $event->getRequest();
  69.         $route $request->attributes->get('_route');
  70.         if ($route === 'admin_product_product_class') {
  71.             // 選択商品が設定された商品かチェック
  72.             $id $request->get('id');
  73.             /** @var Product $product */
  74.             $product $this->productRepository->find($id);
  75.             if ($product) {
  76.                 $ksProduct $product->getKsProduct();
  77.                 if ($ksProduct) {
  78.                     // 規格設定は不可
  79.                     $message trans('kokokara_select.admin.setting.ks_product.valid');
  80.                     $this->session->getFlashBag()->add('eccube.admin.warning'$message);
  81.                     $event->setController(function () use ($id) {
  82.                         $url $this->router->generate('admin_product_product_edit', ['id' => $id]);
  83.                         return new RedirectResponse($url);
  84.                     });
  85.                 }
  86.             }
  87.         } else if ($route === "product_detail") {
  88.             // 商品詳細
  89.             $id $request->get('id');
  90.             /** @var Product $product */
  91.             $product $this->productRepository->find($id);
  92.             if ($product) {
  93.                 $ksProductOption $product->getKsProductOption();
  94.                 if ($ksProductOption && $ksProductOption->isSelectOnly()) {
  95.                     // 選択専用商品の場合
  96.                     throw new NotFoundHttpException();
  97.                 }
  98.                 if ($this->ksService->isKsProduct($producttrue)) {
  99.                     // 専用画面へ遷移
  100.                     $event->setController(function () use ($id) {
  101.                         $url $this->router->generate('kokokara_select_list', ['id' => $id]);
  102.                         return new RedirectResponse($url);
  103.                     });
  104.                 }
  105.             }
  106.         } else if ($route === 'cart_buystep') {
  107.             $cart $this->cartService->getCart();
  108.             foreach ($cart->getCartItems() as $cartItem) {
  109.                 $valid true;
  110.                 $product $cartItem->getProductClass()->getProduct();
  111.                 // 選択商品のみチェック
  112.                 if (!$this->ksService->isKsProduct($product)) {
  113.                     continue;
  114.                 }
  115.                 if ($cartItem->getKsCartSelectItemGroups()->count() == 0) {
  116.                     $valid false;
  117.                 } else {
  118.                     /** @var KsCartSelectItemGroup $ksCartSelectItemGroup */
  119.                     foreach ($cartItem->getKsCartSelectItemGroups() as $ksCartSelectItemGroup) {
  120.                         if (!$this->ksCartHelper->validKsCartSelectItemGroupProduct($product$ksCartSelectItemGroup)) {
  121.                             // 選択可能商品チェック
  122.                             $valid false;
  123.                             break;
  124.                         } else if (!$this->ksCartHelper->validKsCartSelectItemGroupQuantity($product$ksCartSelectItemGroup)) {
  125.                             // 数量チェック
  126.                             $valid false;
  127.                             break;
  128.                         }
  129.                         /** @var KsCartSelectItem $ksCartSelectItem */
  130.                         foreach ($ksCartSelectItemGroup->getKsCartSelectItems() as $ksCartSelectItem) {
  131.                             $result $this->ksCartHelper->checkKsCartSelectItemStockSingle($ksCartSelectItem);
  132.                             if (!$result['result']) {
  133.                                 $valid false;
  134.                                 break;
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.                 if (!$valid) {
  140.                     // カート画面再表示
  141.                     $event->setController(function () {
  142.                         $url $this->router->generate('cart');
  143.                         return new RedirectResponse($url);
  144.                     });
  145.                 }
  146.             }
  147.         } else if ($route === 'shopping_shipping_multiple') {
  148.             // EC-CUBE 4.0.3 以降のみ固定選択商品の複数配送許可
  149.             if ($this->isMultipleVersion()) {
  150.                 $checkOption KsService::IS_KS_ORDER_NORMAL;
  151.             } else {
  152.                 // 4.0.2以下
  153.                 $checkOption KsService::IS_KS_ORDER_DEFAULT;
  154.             }
  155.             // 選択商品が選ばれている場合は利用不可
  156.             $preOrderId $this->cartService->getPreOrderId();
  157.             if ($this->ksService->isKsOrder($preOrderId$checkOption)) {
  158.                 $message trans('kokokara_select.shopping.multi.cut', ['%kokokara_select%' => $this->getKokokaraSelectName()]);
  159.                 $this->session->getFlashBag()->add('eccube.front.warning'$message);
  160.                 $event->setController(function () {
  161.                     $url $this->router->generate('shopping');
  162.                     return new RedirectResponse($url);
  163.                 });
  164.             }
  165.         } else if ($route === 'product_add_cart') {
  166.             // カート投入
  167.             // 選択商品が設定された商品かチェック
  168.             $id $request->get('id');
  169.             /** @var Product $product */
  170.             $product $this->productRepository->find($id);
  171.             if ($product) {
  172.                 if ($this->ksService->isKsProduct($product)
  173.                     && $product->getKsProduct()->isDirectSelect()) {
  174.                     /** @var KokokaraSelectController $addCartController */
  175.                     $addCartController $this->container->get(KokokaraSelectController::class);
  176.                     $addCartController->setContainer($this->container);
  177.                     // 不足リクエスト生成
  178.                     $event->getRequest()->request
  179.                         ->set('ksCartKey'"");
  180.                     $event->getRequest()
  181.                         ->request
  182.                         ->set('selectItems'$this->ksCartHelper->createDirectSelectFormData($product));
  183.                     $event->setController([$addCartController'addCart']);
  184.                 }
  185.             }
  186.         }
  187.     }
  188.     public static function getSubscribedEvents()
  189.     {
  190.         return [
  191.             KernelEvents::CONTROLLER => ["onControllerBefore"],
  192.         ];
  193.     }
  194. }