app/Plugin/DeliveryFreeProduct/DeliveryFreeProductEvent.php line 181

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 $freeProductRepoEntityManagerInterface $emUtilService $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 (count($value) > 0) {
  119.             $plgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
  120.             $plgProduct->setProduct($Product);
  121.             $this->em->persist($plgProduct);
  122.             $this->em->flush($plgProduct);
  123.         }
  124.     }
  125.     public function hookAdminProductCopyComplete(EventArgs $event)
  126.     {
  127.         $Product $event->getArgument('Product');
  128.         $CopyProduct $event->getArgument('CopyProduct');
  129.         $plgProduct $this->freeProductRepo->findOneBy(['Product' => $Product]);
  130.         if($plgProduct instanceof \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct){
  131.             $copyPlgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
  132.             $copyPlgProduct->setProduct($CopyProduct);
  133.             $this->em->persist($copyPlgProduct);
  134.             $this->em->flush($copyPlgProduct);
  135.         }
  136.     }
  137.     public function hookAdminProductCsvExport(EventArgs $event)
  138.     {
  139.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  140.         if ($ExportCsvRow->isDataNull()) {
  141.             $ProductClass $event->getArgument('ProductClass');
  142.             $Product $ProductClass->getProduct();
  143.             $Csv $event->getArgument('Csv');
  144.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  145.             if ($csvEntityName == 'Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct') {
  146.                 $plgProduct $this->freeProductRepo->findOneBy(['Product' => $Product]);
  147.                 if (!is_null($plgProduct)) {
  148.                     $value 1;
  149.                 } else {
  150.                     $value 0;
  151.                 }
  152.                 $ExportCsvRow->setData($value);
  153.             }
  154.         }
  155.     }
  156.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  157.     {
  158.         $header $event->getArgument('header');
  159.         $key $event->getArgument('key');
  160.         if($key == trans('delivery_free_product.product.csv.title')){
  161.             $header['description'] = trans('delivery_free_product.admin.product.product_csv.delivery_free_flag_description');
  162.             $header['required'] = false;
  163.         }
  164.         $event->setArgument('header',$header);
  165.     }
  166.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  167.     {
  168.         $row $event->getArgument('row');
  169.         $data $event->getArgument('data');
  170.         $errors $event->getArgument('errors');
  171.         if(isset($row[trans('delivery_free_product.product.csv.title')])){
  172.             if($row[trans('delivery_free_product.product.csv.title')] !== '' && $row[trans('delivery_free_product.product.csv.title')] != && $row[trans('delivery_free_product.product.csv.title')] != 1){
  173.                 $message trans('delivery_free_product.product.product_csv.not_correct', [
  174.                     '%line%' => $data->key() + 1,
  175.                     '%name%' => trans('delivery_free_product.product.csv.title'),
  176.                 ]);
  177.                 $errors[] = $message;
  178.             }
  179.         }
  180.         $event->setArgument('errors',$errors);
  181.     }
  182.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  183.     {
  184.         $row $event->getArgument('row');
  185.         $data $event->getArgument('data');
  186.         $ProductClass $event->getArgument('ProductClass');
  187.         if(isset($row[trans('delivery_free_product.product.csv.title')])){
  188.             $Product $ProductClass->getProduct();
  189.             $plgProduct $this->freeProductRepo->findOneBy(['Product' => $Product]);
  190.             if(!is_null($plgProduct)){
  191.                 $this->em->remove($plgProduct);
  192.                 $this->em->flush($plgProduct);
  193.             }
  194.             if($row[trans('delivery_free_product.product.csv.title')] == 1){
  195.                 $plgProduct = new DeliveryFreeProduct();
  196.                 $plgProduct->setProduct($Product);
  197.                 $this->em->persist($plgProduct);
  198.             }
  199.         }
  200.     }
  201.     public function hookDeliveryCoolDeliveryFree(EventArgs $event)
  202.     {
  203.         $isFree $event->getArgument('isFree');
  204.         $Shipping $event->getArgument('Shipping');
  205.         $range $this->deliveryFreeService->getConfig(DeliveryFreeConfig::RANGE_NAME);
  206.         $condition $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
  207.         if($range == DeliveryFreeConfig::BY_SHIPPING){
  208.             $count 0;
  209.             $total 0;
  210.             foreach($Shipping->getProductOrderItems() as $orderItem){
  211.                 $total++;
  212.                 $plgProducts $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
  213.                 if($plgProducts)$count++;
  214.             }
  215.             if($condition == DeliveryFreeConfig::MUST_ALL){
  216.                 if($total == $count)$isFree true;
  217.             }else{
  218.                 if($count 0)$isFree true;
  219.             }
  220.         }elseif($range == DeliveryFreeConfig::BY_ALL){
  221.             $Order $Shipping->getOrder();
  222.             $count 0;
  223.             $total 0;
  224.             /** @var OrderItem $orderDetail */
  225.             foreach($Order->getProductOrderItems() as $orderItem){
  226.                 $total++;
  227.                 $plgProducts $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
  228.                 if($plgProducts)$count++;
  229.             }
  230.             if($condition == DeliveryFreeConfig::MUST_ALL){
  231.                 if($total == $count)$isFree true;
  232.             }else{
  233.                 if($count 0)$isFree true;
  234.             }
  235.         }
  236.         $event->setArgument('isFree',$isFree);
  237.     }
  238. }