app/Plugin/DeliveryFreeProduct/DeliveryFreeProductEvent.php line 237

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : DeliveryFreeProduct
  4. *
  5. * Copyright (C) 2016 BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\DeliveryFreeProduct;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Entity\Cart;
  14. use Eccube\Entity\CartItem;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\TemplateEvent;
  17. use Eccube\Exception\CsvImportException;
  18. use Eccube\Event\EventArgs;
  19. use Eccube\Util\EntityUtil;
  20. use Plugin\DeliveryFreeProduct\Entity\DeliveryFreeConfig;
  21. use Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct;
  22. use Plugin\DeliveryFreeProduct\Repository\ProductRepository;
  23. use Plugin\DeliveryFreeProduct\Service\UtilService;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. class DeliveryFreeProductEvent implements EventSubscriberInterface
  26. {
  27.     /** @var ProductRepository */
  28.     private $freeProductRepo;
  29.     /** @var EntityManagerInterface */
  30.     private $em;
  31.     /** @var UtilService */
  32.     private $deliveryFreeService;
  33.     /**
  34.      * DeliveryFreeProductEvent constructor.
  35.      * @param ProductRepository $freeProductRepo
  36.      * @param EntityManagerInterface $em
  37.      * @param UtilService $deliveryFreeService
  38.      */
  39.     public function __construct(ProductRepository $freeProductRepo, EntityManagerInterface $em, UtilService $deliveryFreeService)
  40.     {
  41.         $this->freeProductRepo = $freeProductRepo;
  42.         $this->em = $em;
  43.         $this->deliveryFreeService = $deliveryFreeService;
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             // admin
  49.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
  50.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  51.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  52.             // front
  53.             'Cart/index.twig' => 'onRenderCart',
  54.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  55.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  56.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  57.             'deliverycool.delivery_free.condition' => 'hookDeliveryCoolDeliveryFree',
  58.         ];
  59.     }
  60.     public function onRenderCart(TemplateEvent $event)
  61.     {
  62.         $parameters = $event->getParameters();
  63.         /** @var Cart[] $Carts */
  64.         $Carts = $parameters['Carts'];
  65.         $arrDeliveryFree = [];
  66.         $originalDeliveryFree = $parameters['is_delivery_free'];
  67.         $condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
  68.         $needAddTemplate = false;
  69.         $isCartFree = [];
  70.         foreach ($Carts as $key => $Cart) {
  71.             // default
  72.             $arrDeliveryFree[$Cart->getCartKey()] = $originalDeliveryFree[$Cart->getCartKey()];
  73.             if(!$originalDeliveryFree[$Cart->getCartKey()]) {
  74.                 $count = 0;
  75.                 $total = 0;
  76.                 /** @var CartItem $cart_item */
  77.                 foreach ($Cart->getCartItems() as $cart_item) {
  78.                     if (!$cart_item->isProduct()) {
  79.                         continue;
  80.                     }
  81.                     $total++;
  82.                     $plgProducts = $this->freeProductRepo->findBy(['Product' => $cart_item->getProductClass()->getProduct()]);
  83.                     if($plgProducts)$count++;
  84.                 }
  85.                 $deliv_free_flg = false;
  86.                 if ($condition == DeliveryFreeConfig::MUST_ALL) {
  87.                     if ($total == $count) {
  88.                         $deliv_free_flg = true;
  89.                         $needAddTemplate = true;
  90.                     }
  91.                 } else {
  92.                     if ($count > 0) {
  93.                         $deliv_free_flg = true;
  94.                         $needAddTemplate = true;
  95.                     }
  96.                 }
  97.                 $arrDeliveryFree[$Cart->getCartKey()] = $deliv_free_flg;
  98.                 $isCartFree[$key] = $deliv_free_flg;
  99.             }
  100.         }
  101.         $parameters['is_delivery_free'] = $arrDeliveryFree;
  102.         $parameters['is_cart_free'] = $isCartFree;
  103.         $event->setParameters($parameters);
  104.         if ($needAddTemplate) {
  105.             $event->addSnippet("@DeliveryFreeProduct/default/Cart/delivery_free_message.twig");
  106.         }
  107.     }
  108.     public function hookAdminProductEditComplete(EventArgs $event)
  109.     {
  110.         $Product = $event->getArgument('Product');
  111.         $form = $event->getArgument('form');
  112.         $value = $form->get('plg_deliveryfreeproduct')->getData();
  113.         $plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
  114.         // if (EntityUtil::isNotEmpty($plgProduct)) {
  115.         //     $this->em->remove($plgProduct);
  116.         //     $this->em->flush();
  117.         // }
  118.         if ($plgProduct !== null) {
  119.             $this->em->remove($plgProduct);
  120.             $this->em->flush();
  121.         }
  122.         if (count($value) > 0) {
  123.             $plgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
  124.             $plgProduct->setProduct($Product);
  125.             $this->em->persist($plgProduct);
  126.             $this->em->flush($plgProduct);
  127.         }
  128.     }
  129.     public function hookAdminProductCopyComplete(EventArgs $event)
  130.     {
  131.         $Product = $event->getArgument('Product');
  132.         $CopyProduct = $event->getArgument('CopyProduct');
  133.         $plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
  134.         if($plgProduct instanceof \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct){
  135.             $copyPlgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
  136.             $copyPlgProduct->setProduct($CopyProduct);
  137.             $this->em->persist($copyPlgProduct);
  138.             $this->em->flush($copyPlgProduct);
  139.         }
  140.     }
  141.     public function hookAdminProductCsvExport(EventArgs $event)
  142.     {
  143.         $ExportCsvRow = $event->getArgument('ExportCsvRow');
  144.         if ($ExportCsvRow->isDataNull()) {
  145.             $ProductClass = $event->getArgument('ProductClass');
  146.             $Product = $ProductClass->getProduct();
  147.             $Csv = $event->getArgument('Csv');
  148.             $csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
  149.             if ($csvEntityName == 'Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct') {
  150.                 $plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
  151.                 if (!is_null($plgProduct)) {
  152.                     $value = 1;
  153.                 } else {
  154.                     $value = 0;
  155.                 }
  156.                 $ExportCsvRow->setData($value);
  157.             }
  158.         }
  159.     }
  160.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  161.     {
  162.         $header = $event->getArgument('header');
  163.         $key = $event->getArgument('key');
  164.         if($key == trans('delivery_free_product.product.csv.title')){
  165.             $header['description'] = trans('delivery_free_product.admin.product.product_csv.delivery_free_flag_description');
  166.             $header['required'] = false;
  167.         }
  168.         $event->setArgument('header',$header);
  169.     }
  170.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  171.     {
  172.         $row = $event->getArgument('row');
  173.         $data = $event->getArgument('data');
  174.         $errors = $event->getArgument('errors');
  175.         if(isset($row[trans('delivery_free_product.product.csv.title')])){
  176.             if($row[trans('delivery_free_product.product.csv.title')] !== '' && $row[trans('delivery_free_product.product.csv.title')] != 0 && $row[trans('delivery_free_product.product.csv.title')] != 1){
  177.                 $message = trans('delivery_free_product.product.product_csv.not_correct', [
  178.                     '%line%' => $data->key() + 1,
  179.                     '%name%' => trans('delivery_free_product.product.csv.title'),
  180.                 ]);
  181.                 $errors[] = $message;
  182.             }
  183.         }
  184.         $event->setArgument('errors',$errors);
  185.     }
  186.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  187.     {
  188.         $row = $event->getArgument('row');
  189.         $data = $event->getArgument('data');
  190.         $ProductClass = $event->getArgument('ProductClass');
  191.         if(isset($row[trans('delivery_free_product.product.csv.title')])){
  192.             $Product = $ProductClass->getProduct();
  193.             $plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
  194.             if(!is_null($plgProduct)){
  195.                 $this->em->remove($plgProduct);
  196.                 $this->em->flush($plgProduct);
  197.             }
  198.             if($row[trans('delivery_free_product.product.csv.title')] == 1){
  199.                 $plgProduct = new DeliveryFreeProduct();
  200.                 $plgProduct->setProduct($Product);
  201.                 $this->em->persist($plgProduct);
  202.             }
  203.         }
  204.     }
  205.     public function hookDeliveryCoolDeliveryFree(EventArgs $event)
  206.     {
  207.         $isFree = $event->getArgument('isFree');
  208.         $Shipping = $event->getArgument('Shipping');
  209.         $range = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::RANGE_NAME);
  210.         $condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
  211.         if($range == DeliveryFreeConfig::BY_SHIPPING){
  212.             $count = 0;
  213.             $total = 0;
  214.             foreach($Shipping->getProductOrderItems() as $orderItem){
  215.                 $total++;
  216.                 $plgProducts = $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
  217.                 if($plgProducts)$count++;
  218.             }
  219.             if($condition == DeliveryFreeConfig::MUST_ALL){
  220.                 if($total == $count)$isFree = true;
  221.             }else{
  222.                 if($count > 0)$isFree = true;
  223.             }
  224.         }elseif($range == DeliveryFreeConfig::BY_ALL){
  225.             $Order = $Shipping->getOrder();
  226.             $count = 0;
  227.             $total = 0;
  228.             /** @var OrderItem $orderDetail */
  229.             foreach($Order->getProductOrderItems() as $orderItem){
  230.                 $total++;
  231.                 $plgProducts = $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
  232.                 if($plgProducts)$count++;
  233.             }
  234.             if($condition == DeliveryFreeConfig::MUST_ALL){
  235.                 if($total == $count)$isFree = true;
  236.             }else{
  237.                 if($count > 0)$isFree = true;
  238.             }
  239.         }
  240.         $event->setArgument('isFree',$isFree);
  241.     }
  242. }