app/Plugin/ShowPointAmount42/Event.php line 80

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of ShowPointAmount4
  4.  *
  5.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  6.  *
  7.  * https://umebius.com/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ShowPointAmount42;
  13. use Eccube\Entity\Cart;
  14. use Eccube\Entity\CartItem;
  15. use Eccube\Entity\Product;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Event\TemplateEvent;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\PluginRepository;
  20. use Plugin\ShowPointAmount42\Repository\ConfigRepository;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  24. class Event implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var BaseInfoRepository
  28.      */
  29.     private $baseInfoRepository;
  30.     /**
  31.      * @var ConfigRepository
  32.      */
  33.     private $configRepository;
  34.     /**
  35.      * @var PluginRepository
  36.      */
  37.     private $pluginRepository;
  38.     /**
  39.      * @var AuthorizationCheckerInterface
  40.      */
  41.     private $authorizationChecker;
  42.     /**
  43.      * @var TokenStorageInterface
  44.      */
  45.     private $tokenStorage;
  46.     /**
  47.      * @return array
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             'Product/detail.twig' => 'onProductTwig',
  53.             'Cart/index.twig' => 'onCartIndexTwig',
  54.         ];
  55.     }
  56.     public function __construct(
  57.         BaseInfoRepository $baseInfoRepository,
  58.         ConfigRepository $configRepository,
  59.         PluginRepository $pluginRepository,
  60.         AuthorizationCheckerInterface $authorizationChecker,
  61.         TokenStorageInterface $tokenStorage
  62.     ) {
  63.         $this->baseInfoRepository $baseInfoRepository;
  64.         $this->configRepository $configRepository;
  65.         $this->pluginRepository $pluginRepository;
  66.         $this->authorizationChecker $authorizationChecker;
  67.         $this->tokenStorage $tokenStorage;
  68.     }
  69.     public function onProductTwig(TemplateEvent $event)
  70.     {
  71.         $BaseInfo $this->baseInfoRepository->get();
  72.         if (!$BaseInfo->isOptionPoint()) {
  73.             return;
  74.         }
  75.         if (!$this->configRepository->get()->isOptionShowInProductDetail()) {
  76.             return;
  77.         }
  78.         $basicPointRate $BaseInfo->getBasicPointRate();
  79.         $Product $event->getParameter('Product');
  80.         $json $this->getClassCategoriesAsJson($Product);
  81.         $points = [];
  82.         /* @var $productClass ProductClass */
  83.         foreach ($Product->getProductClasses() as $ProductClass) {
  84.             if (!is_null($ProductClass->getPointRate())) {
  85.                 $rate $ProductClass->getPointRate();
  86.             } else {
  87.                 $rate $basicPointRate;
  88.                 $Plugin $this->pluginRepository->findOneBy([
  89.                     'code' => 'UMCustomerRank4',
  90.                     'enabled' => true,
  91.                 ]);
  92.                 if (!is_null($Plugin) && $this->authorizationChecker->isGranted('ROLE_USER')) {
  93.                     $Customer $this->tokenStorage->getToken()->getUser();
  94.                     $CustomerRank $Customer->getCustomerRank();
  95.                     if (!is_null($CustomerRank) && !is_null($CustomerRank->getPointRate())) {
  96.                         $rate $Customer->getCustomerRank()->getPointRate();
  97.                     }
  98.                 }
  99.             }
  100.             if ($rate !== null) {
  101.                 $points[] = round($ProductClass->getPrice02() * ($rate 100)) * 1;
  102.             }
  103.         }
  104.         $event->setParameter('point_min'min($points));
  105.         $event->setParameter('point_max'max($points));
  106.         $event->setParameter('point_json'$json);
  107.         $event->addSnippet('@ShowPointAmount42/product_detail_point.twig');
  108.         $event->addSnippet('@ShowPointAmount42/product_detail_point_js.twig');
  109.     }
  110.     public function onCartIndexTwig(TemplateEvent $event)
  111.     {
  112.         if (!$this->baseInfoRepository->get()->isOptionPoint()) {
  113.             return;
  114.         }
  115.         if (!$this->configRepository->get()->isOptionShowInCart()) {
  116.             return;
  117.         }
  118.         $Carts $event->getParameter('Carts');
  119.         if (empty($Carts)) {
  120.             return;
  121.         }
  122.         foreach ($Carts as $Cart) {
  123.             $points[] = $this->calculateAddPoint($Cart);
  124.         }
  125.         $event->setParameter('arrCartPoint'$points);
  126.         $event->addSnippet('@ShowPointAmount42/cart_index.twig');
  127.     }
  128.     /**
  129.      * Get the ClassCategories as JSON.
  130.      *
  131.      * @return string
  132.      */
  133.     public function getClassCategoriesAsJson(Product $Product)
  134.     {
  135.         $Product->_calc();
  136.         $class_categories = [
  137.             '__unselected' => [
  138.                 '__unselected' => [
  139.                     'name' => trans('common.select'),
  140.                     'product_class_id' => '',
  141.                 ],
  142.             ],
  143.         ];
  144.         $BaseInfo $this->baseInfoRepository->get();
  145.         $basicPointRate $BaseInfo->getBasicPointRate();
  146.         foreach ($Product->getProductClasses() as $ProductClass) {
  147.             /** @var ProductClass $ProductClass */
  148.             if (!$ProductClass->isVisible()) {
  149.                 continue;
  150.             }
  151.             if (!is_null($ProductClass->getPointRate())) {
  152.                 $rate $ProductClass->getPointRate();
  153.             } else {
  154.                 $rate $basicPointRate;
  155.             }
  156.             /* @var $ProductClass \Eccube\Entity\ProductClass */
  157.             $ClassCategory1 $ProductClass->getClassCategory1();
  158.             $ClassCategory2 $ProductClass->getClassCategory2();
  159.             if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  160.                 continue;
  161.             }
  162.             $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  163.             $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  164.             if ($rate === null) {
  165.                 $point '';
  166.             } else {
  167.                 $point round($ProductClass->getPrice02() * ($rate 100)) * 1;
  168.             }
  169.             $class_categories[$class_category_id1]['#'] = [
  170.                 'classcategory_id2' => '',
  171.                 'name' => trans('common.select'),
  172.                 'product_class_id' => '',
  173.             ];
  174.             $class_categories[$class_category_id1]['#'.$class_category_id2] = [
  175.                 'point' => $point,
  176.             ];
  177.         }
  178.         return json_encode($class_categories);
  179.     }
  180.     /**
  181.      * 付与ポイントを計算.
  182.      *
  183.      * @return int
  184.      */
  185.     private function calculateAddPoint(Cart $cart)
  186.     {
  187.         $BaseInfo $this->baseInfoRepository->get();
  188.         $basicPointRate $BaseInfo->getBasicPointRate();
  189.         // 明細ごとのポイントを集計
  190.         $totalPoint array_reduce($cart->getItems()->toArray(),
  191.             function ($carryCartItem $item) use ($basicPointRate) {
  192.                 $pointRate $item->isProduct() ? $item->getProductClass()->getPointRate() : null;
  193.                 if ($pointRate === null) {
  194.                     $pointRate $basicPointRate;
  195.                 }
  196.                 // ポイント = 単価 * ポイント付与率 * 数量
  197.                 $point round($item->getProductClass()->getPrice02() * ($pointRate 100)) * $item->getQuantity();
  198.                 return $carry $point;
  199.             }, 0);
  200.         return $totalPoint $totalPoint;
  201.     }
  202. }