app/Plugin/FlashSale/FlashSaleEvent.php line 324

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Flash Sale plugin
  4.  *
  5.  * Copyright(c) ECCUBE VN LAB. All Rights Reserved.
  6.  *
  7.  * https://www.facebook.com/groups/eccube.vn
  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\FlashSale;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Cart;
  15. use Eccube\Entity\CustomerFavoriteProduct;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Event\TemplateEvent;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Plugin\FlashSale\Entity\FlashSale;
  20. use Plugin\FlashSale\Repository\FlashSaleRepository;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Eccube\Twig\Extension\CartServiceExtension;
  23. /**
  24.  * Class FlashSaleEvent
  25.  */
  26. class FlashSaleEvent implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var \Twig_Environment
  30.      */
  31.     protected $twig;
  32.     /**
  33.      * @var FlashSaleRepository
  34.      */
  35.     protected $flashSaleRepository;
  36.     /**
  37.      * @var BaseInfo
  38.      */
  39.     protected $baseInfo;
  40.     /**
  41.      * FlashSaleEvent constructor.
  42.      *
  43.      * @param \Twig_Environment $twig
  44.      * @param FlashSaleRepository $flashSaleRepository
  45.      * @param BaseInfoRepository $baseInfoRepository
  46.      *
  47.      * @throws \Exception
  48.      */
  49.     public function __construct(
  50.         \Twig_Environment $twig,
  51.         FlashSaleRepository $flashSaleRepository,
  52.         BaseInfoRepository $baseInfoRepository
  53.     ) {
  54.         $this->twig $twig;
  55.         $this->flashSaleRepository $flashSaleRepository;
  56.         $this->baseInfo $baseInfoRepository->get();
  57.     }
  58.     /**
  59.      * @return array
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             'Mypage/history.twig' => ['mypageHistory'0],
  65.             'Mypage/index.twig' => ['mypageIndex'0],
  66.             'Mypage/favorite.twig' => ['mypageFavorite'0],
  67.             'Block/header.twig' => ['onTemplateBlockHeader'0],
  68.             'Block/cart.twig' => [
  69.                 ['onTemplateBlockCart'0],
  70.                 ['onTemplateTotalPrice'100],
  71.             ],
  72.             'Cart/index.twig' => [
  73.                 ['onTemplateCartIndex'0],
  74.                 ['onTemplateTotalPrice'100],
  75.             ],
  76.             'Shopping/index.twig' => ['onTemplateShoppingIndex'0],
  77.             'Shopping/shipping_multiple.twig' => ['onTemplateShoppingIndex'0],
  78.             'Shopping/confirm.twig' => ['onTemplateShoppingIndex'0],
  79.         ];
  80.     }
  81.     /**
  82.      * @param TemplateEvent $event
  83.      */
  84.     public function mypageHistory(TemplateEvent $event)
  85.     {
  86.         $source $event->getSource();
  87.         $target '{{ orderItem.price_inc_tax|price }}';
  88.         $change "{% if orderItem.fs_price is defined and orderItem.fs_price is not null and orderItem.fs_price < orderItem.price_inc_tax %}<del>{{orderItem.price_inc_tax|price}}</del><span class='ec-color-red'>{{orderItem.fs_price|price}}</span>{% else %}{$target}{% endif %}";
  89.         $source str_replace($target$change$source);
  90.         $event->setSource($source);
  91.     }
  92.     /**
  93.      * @param TemplateEvent $event
  94.      */
  95.     public function mypageIndex(TemplateEvent $event)
  96.     {
  97.         $source $event->getSource();
  98.         $target '{{ OrderItem.price_inc_tax|price }}';
  99.         $change "{% if OrderItem.fs_price is defined and OrderItem.fs_price is not null and OrderItem.fs_price < OrderItem.price_inc_tax %}<del>{{OrderItem.price_inc_tax|price}}</del><span class='ec-color-red'>{{OrderItem.fs_price|price}}</span>{% else %}{$target}{% endif %}";
  100.         $source str_replace($target$change$source);
  101.         $target 'Order.MergedProductOrderItems';
  102.         $change 'Order.FsMergedProductOrderItems';
  103.         $source str_replace($target$change$source);
  104.         $event->setSource($source);
  105.     }
  106.     /**
  107.      * @param TemplateEvent $event
  108.      */
  109.     public function mypageFavorite(TemplateEvent $event)
  110.     {
  111.         $FlashSale $this->flashSaleRepository->getAvailableFlashSale();
  112.         if (!$event->hasParameter('pagination') || !$FlashSale instanceof FlashSale) {
  113.             return;
  114.         }
  115.         $source $event->getSource();
  116.         // check match price section
  117.         preg_match('/<p class="ec-favoriteRole__itemPrice">/u'$source$matchPREG_OFFSET_CAPTURE);
  118.         if (empty($match)) {
  119.             return;
  120.         }
  121.         $index current($match)[1];
  122.         $firstSection substr($source0$index);
  123.         $endSection substr($source$index);
  124.         // end of price section
  125.         preg_match('/<\/p>/u'$endSection$match2PREG_OFFSET_CAPTURE);
  126.         if (empty($match2)) {
  127.             return;
  128.         }
  129.         $index2 current($match2)[1];
  130.         $middleSection substr($endSection0$index2);
  131.         $endSection substr($endSection$index2);
  132.         $insert = <<<EOT
  133.         {% if fs_data is defined and fs_data[Product.id] is defined %}
  134. <p id="discount" class="ec-color-red ec-font-size-4">
  135.     {{ 'flash_sale.front.sale_up_to'|trans({'%percent%' : fs_data[Product.id] }) }}
  136. </p>
  137. {%endif%}
  138. EOT;
  139.         $newSource $firstSection.$middleSection.$insert.$endSection;
  140.         $event->setSource($newSource);
  141.         // calculate percent
  142.         $data = [];
  143.         $pagination $event->getParameter('pagination');
  144.         /** @var CustomerFavoriteProduct $ProductFavorite */
  145.         foreach ($pagination as $ProductFavorite) {
  146.             $Product $ProductFavorite->getProduct();
  147.             $tmp = [];
  148.             /** @var ProductClass $ProductClass */
  149.             foreach ($Product->getProductClasses() as $ProductClass) {
  150.                 $tmp[$ProductClass->getId()] = $ProductClass->getFlashSaleDiscountPercent();
  151.             }
  152.             if (count($tmp)) {
  153.                 $data[$Product->getId()] = max($tmp);
  154.             }
  155.         }
  156.         if (empty($data)) {
  157.             return;
  158.         }
  159.         $event->setParameter('fs_data'$data);
  160.     }
  161.     /**
  162.      * Make Block/cart.twig able to dispatch template event
  163.      *
  164.      * @param TemplateEvent $event
  165.      */
  166.     public function onTemplateBlockHeader(TemplateEvent $event)
  167.     {
  168.         $source $event->getSource();
  169.         $source str_replace(
  170.             '{{ include(\'Block/cart.twig\') }}',
  171.             '{{ include_dispatch(\'Block/cart.twig\') }}',
  172.             $source
  173.         );
  174.         $event->setSource($source);
  175.     }
  176.     /**
  177.      * Display price of flashsale on block cart template
  178.      *
  179.      * @param TemplateEvent $event
  180.      */
  181.     public function onTemplateBlockCart(TemplateEvent $event)
  182.     {
  183.         $FlashSale $this->flashSaleRepository->getAvailableFlashSale();
  184.         if (!$FlashSale instanceof FlashSale) {
  185.             return;
  186.         }
  187.         $source $event->getSource();
  188.         $source str_replace(
  189.             '{{ CartItem.price|price }}',
  190.             '{% if CartItem.getFlashSaleDiscount() %} <del>{{ CartItem.price|price }}</del> <span class="ec-color-red">{{ CartItem.getFlashSaleDiscountPrice()|price }} ({{ CartItem.getFlashSaleDiscountPercent() }}%)</span> {% else %} {{ CartItem.price|price }} {% endif %}',
  191.             $source
  192.         );
  193.         $source str_replace(
  194.             '{{ totalPrice|price }}',
  195.             '{% if totalDiscountPrice is defined %}<del>{{ totalPrice|price }}</del> <span>{{ (totalPrice - totalDiscountPrice)|price }}</span>{% else %} {{ totalPrice|price }} {% endif %}',
  196.             $source
  197.         );
  198.         $event->setSource($source);
  199.     }
  200.     /**
  201.      * Assign total discount price to template
  202.      *
  203.      * @param TemplateEvent $event
  204.      */
  205.     public function onTemplateTotalPrice(TemplateEvent $event)
  206.     {
  207.         if ($event->hasParameter('totalDiscountPrice')) {
  208.             return;
  209.         }
  210.         $totalDiscountPrice 0;
  211.         $Carts $this->twig->getExtension(CartServiceExtension::class)->get_all_carts();
  212.         foreach ($Carts as $Cart) {
  213.             $totalDiscountPrice += $Cart->getFlashSaleTotalDiscount();
  214.         }
  215.         if ($totalDiscountPrice) {
  216.             $event->setParameter('totalDiscountPrice'$totalDiscountPrice);
  217.         }
  218.     }
  219.     /**
  220.      * Display price of flashsale on cart index template
  221.      *
  222.      * @param TemplateEvent $event
  223.      */
  224.     public function onTemplateCartIndex(TemplateEvent $event)
  225.     {
  226.         $FlashSale $this->flashSaleRepository->getAvailableFlashSale();
  227.         if (!$FlashSale instanceof FlashSale) {
  228.             return;
  229.         }
  230.         // clone from CartController:index
  231.         $Carts $event->getParameter('Carts');
  232.         // TODO itemHolderから取得できるように
  233.         $least = [];
  234.         $isDeliveryFree = [];
  235.         /** @var Cart $Cart */
  236.         foreach ($Carts as $Cart) {
  237.             $isDeliveryFree[$Cart->getCartKey()] = false;
  238.             if ($this->baseInfo->getDeliveryFreeQuantity() && $this->baseInfo->getDeliveryFreeQuantity() <= $Cart->getQuantity()) {
  239.                 $isDeliveryFree[$Cart->getCartKey()] = true;
  240.             }
  241.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  242.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getFlashSaleTotalDiscountPrice()) {
  243.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  244.                 } else {
  245.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getFlashSaleTotalDiscountPrice();
  246.                 }
  247.             }
  248.         }
  249.         $parameters $event->getParameters();
  250.         $parameters['least'] = $least;
  251.         $parameters['is_delivery_free'] = $isDeliveryFree;
  252.         $event->setParameters($parameters);
  253.         $source $event->getSource();
  254.         $source str_replace(
  255.             '{{ CartItem.price|price }}',
  256.             '{% if CartItem.getFlashSaleDiscount() %} <del>{{ CartItem.price|price }}</del> <span class="ec-color-red">{{ CartItem.getFlashSaleDiscountPrice()|price }} ({{ CartItem.getFlashSaleDiscountPercent() }}%)</span> {% else %} {{ CartItem.price|price }} {% endif %}',
  257.             $source
  258.         );
  259.         $source str_replace(
  260.             '{{ CartItem.total_price|price }}',
  261.             '{% if CartItem.getFlashSaleTotalDiscount() %} <del>{{ CartItem.total_price|price }}</del> <span class="ec-color-red">{{ CartItem.getFlashSaleTotalDiscountPrice()|price }}</span> {% else %} {{ CartItem.total_price|price }} {% endif %}',
  262.             $source
  263.         );
  264.         $source str_replace(
  265.             '{{ Cart.totalPrice|price }}',
  266.             '{% if Cart.getFlashSaleTotalDiscount() %} <del>{{ Cart.totalPrice|price }}</del> <span class="ec-color-red">{{ Cart.getFlashSaleTotalDiscountPrice()|price }}</span> {% else %} {{ Cart.totalPrice|price }} {% endif %}',
  267.             $source
  268.         );
  269.         $replace '
  270.         {% if totalDiscountPrice is defined %}
  271.             {% set totalPriceWithFlashSaleDiscount = "<del>" ~ totalPrice|price ~ "</del> <span>" ~ (totalPrice - totalDiscountPrice)|price ~ "</span>" %}
  272.             {{ \'front.cart.total_price\'|trans({ \'%price%\': totalPriceWithFlashSaleDiscount })|raw }}
  273.         {% else %}
  274.             {{ \'front.cart.total_price\'|trans({ \'%price%\': totalPrice|price })|raw }}
  275.         {% endif %}
  276.         ';
  277.         $source str_replace(
  278.             '{{ \'front.cart.total_price\'|trans({ \'%price%\': totalPrice|price })|raw }}',
  279.             $replace,
  280.             $source
  281.         );
  282.         $event->setSource($source);
  283.     }
  284.     /**
  285.      * Display price of flashsale on shopping index template
  286.      *
  287.      * @param TemplateEvent $event
  288.      */
  289.     public function onTemplateShoppingIndex(TemplateEvent $event)
  290.     {
  291.         $FlashSale $this->flashSaleRepository->getAvailableFlashSale();
  292.         if (!$FlashSale instanceof FlashSale) {
  293.             return;
  294.         }
  295.         $source $event->getSource();
  296.         $source str_replace(
  297.             '{{ orderItem.priceIncTax|price }}',
  298.             '{% if orderItem.getFlashSaleDiscount() %} <del>{{ orderItem.priceIncTax|price }}</del><span class="ec-color-red">{{ orderItem.getFlashSaleDiscountPrice()|price }}({{ orderItem.getFlashSaleDiscountPercent() }}%)</span> {% else %} {{ orderItem.priceIncTax|price }} {% endif %}',
  299.             $source
  300.         );
  301.         $source str_replace(
  302.             '{{ orderItem.totalPrice|price }}',
  303.             '{% if orderItem.getFlashSaleTotalDiscount() %} <del>{{ orderItem.totalPrice|price }}</del><span class="ec-color-red">{{ orderItem.getFlashSaleTotalDiscountPrice()|price }}</span> {% else %} {{ orderItem.totalPrice|price }} {% endif %}',
  304.             $source
  305.         );
  306.         $event->setSource($source);
  307.     }
  308. }