app/Plugin/JoolenDisplayPoints4/Event.php line 190

Open in your IDE?
  1. <?php
  2. /*
  3.  * Plugin Name: JoolenDisplayPoints4
  4.  *
  5.  * Copyright(c) joolen inc. All Rights Reserved.
  6.  *
  7.  * https://www.joolen.co.jp/
  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\JoolenDisplayPoints4;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\Cart;
  16. use Eccube\Entity\CustomerFavoriteProduct;
  17. use Eccube\Entity\Plugin;
  18. use Eccube\Entity\Product;
  19. use Eccube\Entity\ProductClass;
  20. use Eccube\Event\TemplateEvent;
  21. use Eccube\Repository\BaseInfoRepository;
  22. use Eccube\Repository\PluginRepository;
  23. use Knp\Component\Pager\Pagination\SlidingPagination;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. class Event implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var BaseInfo
  29.      */
  30.     protected $BaseInfo;
  31.     /**
  32.      * @var Plugin
  33.      */
  34.     private $FixedPointPlugin;
  35.     /**
  36.      * @var Plugin
  37.      */
  38.     private $NoPointPlugin;
  39.     /**
  40.      * @var string
  41.      */
  42.     protected $app_template_default_path;
  43.     /**
  44.      * AddProductFixedPointProcessor constructor.
  45.      *
  46.      * @throws \Exception
  47.      */
  48.     public function __construct(
  49.         EccubeConfig $eccubeConfig,
  50.         BaseInfoRepository $baseInfoRepository,
  51.         PluginRepository $pluginRepository
  52.     ) {
  53.         $this->BaseInfo $baseInfoRepository->get();
  54.         // 商品追加ポイント設定プラグインの情報を取得
  55.         $this->FixedPointPlugin $pluginRepository->findOneBy([
  56.             'code' => 'JoolenFixedPointsForProduct4',
  57.             'enabled' => true,
  58.         ]);
  59.         // ポイント対象外商品設定プラグインの情報を取得
  60.         $this->NoPointPlugin $pluginRepository->findOneBy([
  61.             'code' => 'JoolenNoPointsForProduct4',
  62.             'enabled' => true,
  63.         ]);
  64.         $this->app_template_default_path $eccubeConfig['eccube_theme_front_dir'].'/JoolenDisplayPoints4/Resource/template/default';
  65.     }
  66.     /**
  67.      * @return array
  68.      */
  69.     public static function getSubscribedEvents()
  70.     {
  71.         return [
  72.             // --------------
  73.             // Front
  74.             // --------------
  75.             'Product/list.twig' => 'onRenderProductList',
  76.             'Product/detail.twig' => 'onRenderProductDetail',
  77.             'Cart/index.twig' => 'onRenderCartIndex',
  78.             'Mypage/favorite.twig' => 'onRenderFavorite',
  79.         ];
  80.     }
  81.     /**
  82.      * [フロント]商品一覧画面の描画
  83.      */
  84.     public function onRenderProductList(TemplateEvent $event)
  85.     {
  86.         $datum = [];
  87.         /** @var SlidingPagination $Pagination */
  88.         $Pagination $event->getParameter('pagination');
  89.         foreach ($Pagination->getItems() as $Product) {
  90.             $data $this->getClassCategoriesAsJson($Product);
  91.             // MEMO: ポイントの最小と最大を求める
  92.             $pointRange $this->calcPointRange($Product);
  93.             foreach ($pointRange as $key => $point) {
  94.                 $data[$key] = $point;
  95.             }
  96.             // ポイント対象外商品設定プラグインが有効な場合、有効/無効のフラグを情報に追加する
  97.             $isNoPointFlag false;
  98.             if ($this->NoPointPlugin) {
  99.                 $isNoPointFlag $Product->isNoPointFlag();
  100.             }
  101.             $data['is_no_point_flag'] = $isNoPointFlag;
  102.             $datum[$Product->getId()] = $data;
  103.         }
  104.         $twig $this->getTwigPath('Product/list_js.twig');
  105.         $event->addAsset($twig);
  106.         $event->setParameter('display_point_json'json_encode($datum));
  107.     }
  108.     /**
  109.      * [フロント]商品詳細画面の描画
  110.      */
  111.     public function onRenderProductDetail(TemplateEvent $event)
  112.     {
  113.         $datum = [];
  114.         /** @var Product $Product */
  115.         $Product $event->getParameter('Product');
  116.         $data $this->getClassCategoriesAsJson($Product);
  117.         // MEMO: ポイントの最小と最大を求める
  118.         $pointRange $this->calcPointRange($Product);
  119.         foreach ($pointRange as $key => $point) {
  120.             $data[$key] = $point;
  121.             // ポイント対象外商品設定プラグインが有効な場合、有効/無効のフラグを情報に追加する
  122.             $isNoPointFlag false;
  123.             if ($this->NoPointPlugin) {
  124.                 $isNoPointFlag $Product->isNoPointFlag();
  125.             }
  126.             $data['is_no_point_flag'] = $isNoPointFlag;
  127.         }
  128.         $datum[$Product->getId()] = $data;
  129.         $twig $this->getTwigPath('Product/detail_js.twig');
  130.         $event->addAsset($twig);
  131.         $event->setParameter('display_point_json'json_encode($datum));
  132.     }
  133.     /**
  134.      * [フロント]カート画面の描画
  135.      */
  136.     public function onRenderCartIndex(TemplateEvent $event)
  137.     {
  138.         $Carts $event->getParameter('Carts');
  139.         $datum = [];
  140.         /** @var Cart $Cart */
  141.         foreach ($Carts as $Cart) {
  142.             foreach ($Cart->getCartItems() as $CartItem) {
  143.                 $ProductClass $CartItem->getProductClass();
  144.                 // カート内の商品のポイントを合算する
  145.                 $point $this->calcPoint($ProductClass$CartItem->getQuantity());
  146.                 // ポイント対象外商品設定プラグインが有効な場合、有効/無効のフラグを情報に追加する
  147.                 $isNoPointFlag false;
  148.                 if ($this->NoPointPlugin) {
  149.                     $isNoPointFlag $ProductClass->getProduct()->isNoPointFlag();
  150.                 }
  151.                 $datum[$CartItem->getId()] = [
  152.                     'point' => $point,
  153.                     'is_no_point_flag' => $isNoPointFlag,
  154.                 ];
  155.             }
  156.         }
  157.         $event->setParameter('cart_points'$datum);
  158.     }
  159.     /**
  160.      * [フロント]Mypage/お気に入り一覧画面の描画
  161.      */
  162.     public function onRenderFavorite(TemplateEvent $event)
  163.     {
  164.         /** @var SlidingPagination $Pagination */
  165.         $Pagination $event->getParameter('pagination');
  166.         $favoritePointRanges = [];
  167.         /** @var CustomerFavoriteProduct $CustomerFavoriteProduct */
  168.         foreach ($Pagination->getItems() as $CustomerFavoriteProduct) {
  169.             $Product $CustomerFavoriteProduct->getProduct();
  170.             $favoritePointRanges[$Product->getId()] = $this->calcPointRange($CustomerFavoriteProduct->getProduct());
  171.             // ポイント対象外商品設定プラグインが有効な場合、有効/無効のフラグを情報に追加する
  172.             $isNoPointFlag false;
  173.             if ($this->NoPointPlugin) {
  174.                 $isNoPointFlag $Product->isNoPointFlag();
  175.             }
  176.             $favoritePointRanges[$Product->getId()]['is_no_point_flag'] = $isNoPointFlag;
  177.         }
  178.         $event->setParameter('favorite_points'$favoritePointRanges);
  179.     }
  180.     /**
  181.      * Get the ClassCategories as JSON.
  182.      *
  183.      * @return \array[][]|string
  184.      */
  185.     public function getClassCategoriesAsJson(Product $Product)
  186.     {
  187.         $Product->_calc();
  188.         $class_categories = [
  189.             '__unselected' => [
  190.                 '__unselected' => [
  191.                     'name' => trans('common.select'),
  192.                     'product_class_id' => '',
  193.                 ],
  194.             ],
  195.         ];
  196.         foreach ($Product->getProductClasses() as $ProductClass) {
  197.             /** @var ProductClass $ProductClass */
  198.             if (!$ProductClass->isVisible()) {
  199.                 continue;
  200.             }
  201.             /* @var $ProductClass \Eccube\Entity\ProductClass */
  202.             $ClassCategory1 $ProductClass->getClassCategory1();
  203.             $ClassCategory2 $ProductClass->getClassCategory2();
  204.             if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  205.                 continue;
  206.             }
  207.             $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  208.             $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  209.             $class_categories[$class_category_id1]['#'] = [
  210.                 'classcategory_id2' => '',
  211.                 'name' => trans('common.select'),
  212.                 'product_class_id' => '',
  213.             ];
  214.             $class_categories[$class_category_id1]['#'.$class_category_id2] = [
  215.                 'point' => $this->calcPoint($ProductClass),
  216.             ];
  217.         }
  218.         return $class_categories;
  219.     }
  220.     /**
  221.      * 商品のポイントの最大と最小を計算して返します
  222.      *
  223.      * @return array|int[]
  224.      */
  225.     private function calcPointRange(Product $Product)
  226.     {
  227.         $points = [];
  228.         foreach ($Product->getProductClasses() as $ProductClass) {
  229.             $points[] = $this->calcPoint($ProductClass);
  230.         }
  231.         return ['min_point' => min($points), 'max_point' => max($points)];
  232.     }
  233.     /**
  234.      * 規格ごとのポイントを計算して返します
  235.      *
  236.      * @return float|int
  237.      */
  238.     private function calcPoint(ProductClass $ProductClass$quantity 1)
  239.     {
  240.         if (!$this->BaseInfo->isOptionPoint()) {
  241.             return 0;
  242.         }
  243.         $Product $ProductClass->getProduct();
  244.         // ポイント対象外商品設定プラグインにより付与が無効の場合は0を返す
  245.         if ($this->NoPointPlugin) {
  246.             if ($Product->isNoPointFlag()) {
  247.                 return 0;
  248.             }
  249.         }
  250.         $point 0;
  251.         $pointRate $this->BaseInfo->getBasicPointRate();
  252.         // MEMO: 基本のポイント計算
  253.         // ポイント = 単価 * ポイント付与率 * 数量
  254.         $point += round($ProductClass->getPrice02() * ($pointRate 100)) * $quantity;
  255.         // MEMO: 商品追加ポイントプラグインで設定されているポイントを加算
  256.         if ($this->FixedPointPlugin) {
  257.             $point += $Product->getFixedPoint() * $quantity;
  258.         }
  259.         return $point;
  260.     }
  261.     /**
  262.      * 使用するTwigのパスを取得する
  263.      * app/template配下にある場合はそっちを優先
  264.      *
  265.      * @param $path
  266.      * @return string
  267.      */
  268.     private function getTwigPath($path)
  269.     {
  270.         // MEMO: ページ管理で編集したテンプレートの保存先のパス
  271.         $app_template_default_path $this->app_template_default_path.'/'.$path;
  272.         // MEMO: ページ管理で編集したテンプレートがある場合 優先してそっちを使う
  273.         if (file_exists($app_template_default_path)) {
  274.             return $app_template_default_path;
  275.         }
  276.         return '@JoolenFriendPoint4/default/'.$path;
  277.     }
  278. }