app/Plugin/CustomerPlus42/Event/ShippingMultipleEvent.php line 83

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerPlus4
  4. *
  5. * Copyright (C) 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\CustomerPlus42\Event;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Service\OrderHelper;
  15. use Plugin\CustomerPlus42\Entity\CustomerAddressCustom;
  16. use Plugin\CustomerPlus42\Entity\CustomerData;
  17. use Plugin\CustomerPlus42\Entity\CustomerDataDetail;
  18. use Plugin\CustomerPlus42\Entity\CustomerItem;
  19. use Plugin\CustomerPlus42\Event\AbstractEvent;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  24. class ShippingMultipleEvent extends AbstractEvent implements EventSubscriberInterface
  25. {
  26.     private $session;
  27.     private $authorizationChecker;
  28.     private $orderHelper;
  29.     public function __construct(
  30.             ContainerInterface $container,
  31.             SessionInterface $session,
  32.             AuthorizationCheckerInterface $authorizationChecker,
  33.             OrderHelper $orderHelper
  34.             )
  35.     {
  36.         parent::__construct($container);
  37.         $this->session $session;
  38.         $this->authorizationChecker $authorizationChecker;
  39.         $this->orderHelper $orderHelper;
  40.     }
  41.     /**
  42.      * @return array
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             'Shopping/shipping_multiple_edit.twig' => 'insertDatePickerJS',
  48.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE => 'hookFrontShoppingShippingMultipleInitialize',
  49.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE => ['hookFrontShoppingShippingMultipleComplete',-10],
  50.         ];
  51.     }
  52.     public function hookFrontShoppingShippingMultipleInitialize(EventArgs $event)
  53.     {
  54.         //ゲスト購入時の場合は削除前のShipping情報のカスタム要素を保持
  55.         $Order $event->getArgument('Order');
  56.         $Shippings $Order->getShippings();
  57.         $ShippingCustoms = [];
  58.         $ShippingNames = [];
  59.         $CustomerDatas = [];
  60.         $entityManager $this->container->get('doctrine.orm.entity_manager');
  61.         $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
  62.         foreach($Shippings as $Shipping){
  63.             $CustomerDatas = [];
  64.             foreach($Shipping->getShippingCustoms() as $ShippingCustom){
  65.                 $CustomerItem $customerItemRepository->find($ShippingCustom->getCustomerItemId());
  66.                 foreach($ShippingCustom->getCustomerData()->getDetails() as $Detail){
  67.                     $CustomerDatas[$ShippingCustom->getCustomerItemId()][] = ($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE) ? $Detail->getNumValue() : $Detail->getValue();
  68.                 }
  69.             }
  70.             $ShippingCustoms[$Shipping->getId()] = $CustomerDatas;
  71.             $ShippingNames[$Shipping->getId()] = $Shipping->getShippingMultipleDefaultName();
  72.         }
  73.         $this->session->set('eccube.front.shopping.nonmember.shipping'serialize($ShippingNames));
  74.         $this->session->set('eccube.front.shopping.nonmember.shippingCustom'serialize($ShippingCustoms));
  75.     }
  76.     public function hookFrontShoppingShippingMultipleComplete(EventArgs $event)
  77.     {
  78.         $form $event->getArgument('form');
  79.         $Order $event->getArgument('Order');
  80.         $data $form['shipping_multiple'];
  81.         foreach ($data as $multiples) {
  82.             foreach ($multiples as $items) {
  83.                 foreach ($items as $item) {
  84.                     $CustomerAddress $item['customer_address']->getData();
  85.                     $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  86.                     if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  87.                         foreach($Order->getShippings() as $Shipping){
  88.                             $shippingName $Shipping->getShippingMultipleDefaultName();
  89.                             if($customerAddressName == $shippingName){
  90.                                 if($customerAddressName == $Order->getShippingMultipleDefaultName()){
  91.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  92.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  93.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  94.                                                           ->setCustomerItemId($OrderCustom->getCustomerItemId())
  95.                                                           ->setCustomerAddress($CustomerAddress);
  96.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  97.                                     }
  98.                                 }
  99.                                 $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  100.                             }
  101.                         }
  102.                     }else{
  103.                         //ゲスト購入の場合は削除前に保持されていたShipping情報からデータを復元
  104.                         if ($NonMember $this->orderHelper->getNonMember()) {
  105.                             $entityManager $this->container->get('doctrine.orm.entity_manager');
  106.                             $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
  107.                             if ($MemShippings $this->session->get('eccube.front.shopping.nonmember.shipping')) {
  108.                                 $MemShippingNames unserialize($MemShippings);
  109.                                 $ShippingCustoms $this->session->get('eccube.front.shopping.nonmember.shippingCustom');
  110.                                 $ShippingCustoms unserialize($ShippingCustoms);
  111.                                 $flg false;
  112.                                 foreach($Order->getShippings() as $Shipping){
  113.                                     $shippingName $Shipping->getShippingMultipleDefaultName();
  114.                                     if($customerAddressName == $shippingName){
  115.                                         foreach($MemShippingNames as $memShippingId => $memShippingName){
  116.                                             if($customerAddressName == $memShippingName){
  117.                                                 foreach($ShippingCustoms as $shipping_id => $ShippingCustom){
  118.                                                     if($shipping_id == $memShippingId){
  119.                                                         foreach($ShippingCustom as $customer_item_id => $Details){
  120.                                                             $CustomerData = new CustomerData();
  121.                                                             $CustomerItem $customerItemRepository->find($customer_item_id);
  122.                                                             $CustomerData->setCustomerItem($CustomerItem);
  123.                                                             foreach($Details as $value){
  124.                                                                 $detail = new CustomerDataDetail();
  125.                                                                 $detail->setCustomerData($CustomerData);
  126.                                                                 $disp_value $value;
  127.                                                                 if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
  128.                                                                     if(!is_null($value)){
  129.                                                                         $value = new \DateTime($value);
  130.                                                                     }
  131.                                                                     $detail->setDateValue($value);
  132.                                                                 }
  133.                                                                 if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
  134.                                                                     $detail->setNumValue(intval($value));
  135.                                                                     foreach($CustomerItem->getOptions() as $Option){
  136.                                                                         if($value == $Option->getId())$disp_value $Option->getText();
  137.                                                                     }
  138.                                                                 }
  139.                                                                 $detail->setValue($disp_value);
  140.                                                                 $CustomerData->addDetail($detail);
  141.                                                             }
  142.                                                             $CustomerAddressCustom = new CustomerAddressCustom();
  143.                                                             $CustomerAddressCustom->setCustomerData($CustomerData)
  144.                                                                     ->setCustomerItemId($customer_item_id)
  145.                                                                     ->setCustomerAddress($CustomerAddress);
  146.                                                             $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  147.                                                         }
  148.                                                     }
  149.                                                 }
  150.                                                 $flg true;
  151.                                                 break;
  152.                                             }
  153.                                         }
  154.                                         $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  155.                                     }
  156.                                 }
  157.                                 if(!$flg && $customerAddressName == $Order->getShippingMultipleDefaultName()){
  158.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  159.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  160.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  161.                                                 ->setCustomerItemId($OrderCustom->getCustomerItemId())
  162.                                                 ->setCustomerAddress($CustomerAddress);
  163.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  164.                                     }
  165.                                     $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  166.                                 }
  167.                             }
  168.                         }
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.     }
  174. }