app/Plugin/CustomerPlus4/CustomerPlusEvent.php line 578

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\CustomerPlus4;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\RenderEvent;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\Shopping\CustomerAddressType;
  17. use Eccube\Service\OrderHelper;
  18. use Plugin\CustomerPlus4\Entity\CustomerItem;
  19. use Plugin\CustomerPlus4\Entity\CustomerData;
  20. use Plugin\CustomerPlus4\Entity\CustomerDataDetail;
  21. use Plugin\CustomerPlus4\Entity\CustomerCustom;
  22. use Plugin\CustomerPlus4\Entity\CustomerAddressCustom;
  23. use Plugin\CustomerPlus4\Entity\ShippingCustom;
  24. use Plugin\CustomerPlus4\Service\CustomerPlusService;
  25. use Symfony\Component\DependencyInjection\ContainerInterface;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\Form\FormFactoryInterface;
  28. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  29. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  30. class CustomerPlusEvent implements EventSubscriberInterface
  31. {
  32.     private $container;
  33.     private $formFactory;
  34.     private $customerPlusService;
  35.     private $authorizationChecker;
  36.     private $session;
  37.     private $orderHelper;
  38.     public function __construct(
  39.             ContainerInterface $container,
  40.             FormFactoryInterface $formFactory,
  41.             OrderHelper $orderHelper,
  42.             SessionInterface $session,
  43.             AuthorizationCheckerInterface $authorizationChecker,
  44.             CustomerPlusService $customerPlusService
  45.             )
  46.     {
  47.         $this->container $container;
  48.         $this->formFactory $formFactory;
  49.         $this->orderHelper $orderHelper;
  50.         $this->session $session;
  51.         $this->authorizationChecker $authorizationChecker;
  52.         $this->customerPlusService $customerPlusService;
  53.     }
  54.     /**
  55.      * @return array
  56.      */
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return [
  60.             '@admin/Customer/edit.twig' => 'onTemplateAdminCustomerEdit',
  61.             '@admin/Customer/delivery_edit.twig' => 'onTemplateAdminCustomerDeliveryEdit',
  62.             '@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
  63.             '@admin/Order/search_customer.twig' => 'onTemplateAdminOrderSearchCustomer',
  64.             '@admin/Order/shipping.twig' => 'onTemplateAdminOrderShipping',
  65.             EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE => 'hookAdminOrderEditSearchCustomerByIdComplete',
  66.             'Shopping/index.twig' => 'onTemplateShoppingIndex',
  67.             'Shopping/nonmember.twig' => 'onTemplateShoppingNonmember',
  68.             EccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE => 'hookFrontShoppingNonmemberComplete',
  69.             EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE => 'hookFrontShoppingShippingComplete',
  70.             EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE => 'hookFrontShoppingShippingEditInitialize',
  71.             EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE => 'hookFrontShoppingShippingEditComplete',
  72.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE => 'hookFrontShoppingShippingMultipleInitialize',
  73.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE => ['hookFrontShoppingShippingMultipleComplete',-10],
  74.             EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE => 'hookFrontContactIndexComplete',
  75.             EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT => 'hookAdminCustomerCsvExport',
  76.             EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => 'hookAdminOrderCsvExport',
  77.             'Entry/index.twig' => 'insertDatePickerJS',
  78.             'Mypage/change.twig' => 'insertDatePickerJS',
  79.             'Mypage/delivery_edit.twig' => 'insertDatePickerJS',
  80.             'Shopping/shipping_edit.twig' => 'insertDatePickerJS',
  81.             'Shopping/shipping_multiple_edit.twig' => 'insertDatePickerJS',
  82.             'Contact/index.twig' => 'insertDatePickerJS',
  83.         ];
  84.     }
  85.     public function onTemplateAdminCustomerEdit(TemplateEvent $event)
  86.     {
  87.         $parameters $event->getParameters();
  88.         $source $event->getSource();
  89.         $CustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('customer');
  90.         $AdminCustomerItems $this->customerPlusService->getCustomerPlusForm('admin_customer');
  91.         foreach($CustomerItems as $key => $CustomerItem){
  92.             foreach($AdminCustomerItems as $AdminCustomerItem){
  93.                 if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($CustomerItems[$key]);
  94.             }
  95.         }
  96.         if(preg_match('/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/',$source$result)){
  97.             $search $result[0];
  98.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Customer/ext_edit.twig');
  99.             $replace $snippet $search;
  100.             $source str_replace($search$replace$source);
  101.         }
  102.         $event->setSource($source);
  103.         $parameters['CustomerItems'] = $CustomerItems;
  104.         $event->setParameters($parameters);
  105.         $this->insertDatePickerJSAdmin($event);
  106.     }
  107.     public function onTemplateAdminCustomerDeliveryEdit(TemplateEvent $event)
  108.     {
  109.         $parameters $event->getParameters();
  110.         $source $event->getSource();
  111.         $CustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('shipping');
  112.         $AdminCustomerItems $this->customerPlusService->getCustomerPlusForm('admin_delivery');
  113.         foreach($CustomerItems as $key => $CustomerItem){
  114.             foreach($AdminCustomerItems as $AdminCustomerItem){
  115.                 if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($CustomerItems[$key]);
  116.             }
  117.         }
  118.         if(preg_match('/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/',$source$result)){
  119.             $search $result[0];
  120.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Customer/ext_edit.twig');
  121.             $replace $snippet $search;
  122.             $source str_replace($search$replace$source);
  123.         }
  124.         $event->setSource($source);
  125.         $parameters['CustomerItems'] = $CustomerItems;
  126.         $event->setParameters($parameters);
  127.         $this->insertDatePickerJSAdmin($event);
  128.     }
  129.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  130.     {
  131.         $parameters $event->getParameters();
  132.         $source $event->getSource();
  133.         $OrderCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('order');
  134.         $AdminCustomerItems $this->customerPlusService->getCustomerPlusForm('admin_order');
  135.         foreach($OrderCustomerItems as $key => $CustomerItem){
  136.             foreach($AdminCustomerItems as $AdminCustomerItem){
  137.                 if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($OrderCustomerItems[$key]);
  138.             }
  139.         }
  140.         $ShippingCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
  141.         if(preg_match('/\{\{\sform\_errors\(form\.message\)\s\}\}[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>/',$source$result)){
  142.             $search $result[0];
  143.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Order/ext_order_edit.twig');
  144.             $replace $search $snippet;
  145.             $source str_replace($search$replace$source);
  146.         }
  147.         if(preg_match('/\{\{\sform\_errors\(form\.Shipping\.company_name\)\s\}\}[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>/',$source$result)){
  148.             $search $result[0];
  149.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Order/ext_shipping_edit.twig');
  150.             $replace $search $snippet;
  151.             $source str_replace($search$replace$source);
  152.         }
  153.         $event->setSource($source);
  154.         $parameters['OrderCustomerItems'] = $OrderCustomerItems;
  155.         $parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
  156.         $event->setParameters($parameters);
  157.         $twig '@CustomerPlus4/admin/Order/copy_customer_js.twig';
  158.         $event->addSnippet($twig);
  159.         $this->insertDatePickerJSAdmin($event);
  160.     }
  161.     public function onTemplateAdminOrderSearchCustomer(TemplateEvent $event)
  162.     {
  163.         $parameters $event->getParameters();
  164.         $CustomerCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
  165.         $source $event->getSource();
  166.         if(preg_match('/val\(data\[\'company_name\'\]\)\;/',$source$result)){
  167.             $search $result[0];
  168.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Order/insert_customer_js.twig');
  169.             $replace $search $snippet;
  170.             $source str_replace($search$replace$source);
  171.         }
  172.         $event->setSource($source);
  173.         $parameters['CustomerCustomerItems'] = $CustomerCustomerItems;
  174.         $event->setParameters($parameters);
  175.     }
  176.     public function hookAdminOrderEditSearchCustomerByIdComplete(EventArgs $event)
  177.     {
  178.         $data $event->getArgument('data');
  179.         $Customer $event->getArgument('Customer');
  180.         $CustomerCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
  181.         foreach($CustomerCustomerItems as $CustomerItem){
  182.             $data['customerplus_'.$CustomerItem->getId()] = '';
  183.             $value $Customer->getCustomData($CustomerItem->getId());
  184.             $data['customerplus_'.$CustomerItem->getId()] = $value;
  185.         }
  186.         $event->setArgument('data',$data);
  187.     }
  188.     public function onTemplateAdminOrderShipping(TemplateEvent $event)
  189.     {
  190.         $parameters $event->getParameters();
  191.         $source $event->getSource();
  192.         $ShippingCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
  193.         if(preg_match('/\{\{\sform\_errors\(shippingForm\.company\_name\)\s\}\}[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>[\n|\r\n|\r]\s*\<\/div\>/',$source$result)){
  194.             $search $result[0];
  195.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/admin/Order/ext_shipping.twig');
  196.             $replace $search $snippet;
  197.             $source str_replace($search$replace$source);
  198.         }
  199.         $event->setSource($source);
  200.         $parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
  201.         $event->setParameters($parameters);
  202.         $this->insertDatePickerJSAdmin($event);
  203.     }
  204.     public function onTemplateShoppingIndex(TemplateEvent $event)
  205.     {
  206.         $source $event->getSource();
  207.         if(preg_match('/\$\(\'#customer\'\)\.click\(function\(\)\s\{/',$source$result)){
  208.             $search $result[0];
  209.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus4/Resource/template/default/Shopping/insert_js.twig');
  210.             $replace $search $snippet;
  211.             $source str_replace($search$replace$source);
  212.         }
  213.         $event->setSource($source);
  214.     }
  215.     public function onTemplateShoppingNonmember(TemplateEvent $event)
  216.     {
  217.         $source $event->getSource();
  218.         $request $this->container->get('request_stack')->getCurrentRequest();
  219.         $session $request->getSession();
  220.         if($session->has(OrderHelper::SESSION_NON_MEMBER)){
  221.             if($session->get(OrderHelper::SESSION_NON_MEMBER)){
  222.                 if(preg_match('/url\(\'shopping_nonmember\'\)/',$source$result)){
  223.                     $search $result[0];
  224.                     $replace "url('shopping_customer_edit')";
  225.                     $source str_replace($search$replace$source);
  226.                 }
  227.                 if(preg_match('/url\(\'cart\'\)/',$source$result)){
  228.                     $search $result[0];
  229.                     $replace "url('shopping')";
  230.                     $source str_replace($search$replace$source);
  231.                 }
  232.                 if(preg_match('/\{\{\s\''.trans('common.next').'\'\|trans\s\}\}\<\/button\>/',$source$result)){
  233.                     $search $result[0];
  234.                     $replace "{{ '".trans('customerplus.common.edit')."'|trans }}</button>";
  235.                     $source str_replace($search$replace$source);
  236.                 }
  237.             }
  238.         }
  239.         $event->setSource($source);
  240.         $this->insertDatePickerJS($event);
  241.     }
  242.     public function hookFrontShoppingNonmemberComplete(EventArgs $event)
  243.     {
  244.         $form $event->getArgument('form');
  245.         $request $this->container->get('request_stack')->getCurrentRequest();
  246.         $session $request->getSession();
  247.         $Customer $session->get(OrderHelper::SESSION_NON_MEMBER);
  248.         $CustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('customer');
  249.         if($Customer instanceof \Eccube\Entity\Customer){
  250.             foreach($CustomerItems as $CustomerItem){
  251.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  252.                     $plgCustomer = new CustomerCustom();
  253.                     $plgCustomer->setCustomerItemId($CustomerItem->getId());
  254.                     $plgCustomer->setCustomer($Customer);
  255.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  256.                     $value $this->customerPlusService->convRegistData($value$CustomerItem);
  257.                     $CustomerData = new CustomerData();
  258.                     if($CustomerItem->getInputType() == CustomerItem::CHECKBOX_TYPE){
  259.                         $arrValue explode(','$value);
  260.                     }else{
  261.                         $arrValue = [$value];
  262.                     }
  263.                     foreach($arrValue as $value){
  264.                         $detail = new CustomerDataDetail();
  265.                         $detail->setCustomerData($CustomerData);
  266.                         $disp_value $value;
  267.                         if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
  268.                             if(!is_null($value)){
  269.                                 $value = new \DateTime($value);
  270.                             }
  271.                             $detail->setDateValue($value);
  272.                         }
  273.                         if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
  274.                             $detail->setNumValue(intval($value));
  275.                             foreach($CustomerItem->getOptions() as $Option){
  276.                                 if($value == $Option->getId())$disp_value $Option->getText();
  277.                             }
  278.                         }
  279.                         $detail->setValue($disp_value);
  280.                         $CustomerData->setCustomerItem($CustomerItem);
  281.                         $CustomerData->addDetail($detail);
  282.                     }
  283.                     $plgCustomer->setCustomerData($CustomerData);
  284.                     $Customer->addCustomerCustom($plgCustomer);
  285.                 }
  286.             }
  287.             $session->set(OrderHelper::SESSION_NON_MEMBER$Customer);
  288.         }else{
  289.             $data $session->get(OrderHelper::SESSION_NON_MEMBER);
  290.             foreach($CustomerItems as $CustomerItem){
  291.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  292.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  293.                     $data['customerplus_'.$CustomerItem->getId()] = $this->customerPlusService->convRegistData($value$CustomerItem);
  294.                 }
  295.             }
  296.             $session->set(OrderHelper::SESSION_NON_MEMBER$data);
  297.         }
  298.     }
  299.     public function hookFrontShoppingShippingComplete(EventArgs $event)
  300.     {
  301.         $Shipping $event->getArgument('Shipping');
  302.         $Order $event->getArgument('Order');
  303.         $request $this->container->get('request_stack')->getCurrentRequest();
  304.         $builder $this->formFactory->createBuilder(CustomerAddressType::class, null, [
  305.             'customer' => $Order->getCustomer(),
  306.             'shipping' => $Shipping,
  307.         ]);
  308.         $form $builder->getForm();
  309.         $form->handleRequest($request);
  310.         if ($form->isSubmitted() && $form->isValid()) {
  311.             $CustomerAddress $form['addresses']->getData();
  312.             $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  313.         }
  314.     }
  315.     public function hookFrontShoppingShippingEditInitialize(EventArgs $event)
  316.     {
  317.         if(!$this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
  318.             $builder $event->getArgument('builder');
  319.             $Shipping $event->getArgument('Shipping');
  320.             $entityManager $this->container->get('doctrine.orm.entity_manager');
  321.             $CustomerItems $this->customerPlusService->getCustomerPlusForm('shipping_multiple_edit');
  322.             $plgShippings $Shipping->getShippingCustoms();
  323.             if(!is_null($plgShippings) && count($plgShippings) > 0){
  324.                 foreach($plgShippings as $plgShipping){
  325.                     $CustomerItem $plgShipping->getCustomerData()->getCustomerItem();
  326.                     if(in_array($CustomerItem,$CustomerItems)){
  327.                         $customer_item_id $CustomerItem->getId();
  328.                         $value $plgShipping->getValue();
  329.                         if($value){
  330.                             $value $this->customerPlusService->convSetData($value$customer_item_id'customerplus_Shipping_customerplus_');
  331.                             $builder->get('customerplus_'.$customer_item_id)->setData($value);
  332.                         }
  333.                     }
  334.                 }
  335.             }
  336.         }
  337.     }
  338.     public function hookFrontShoppingShippingEditComplete(EventArgs $event)
  339.     {
  340.         $form $event->getArgument('form');
  341.         $Shipping $event->getArgument('Shipping');
  342.         $CustomerAddress $event->getArgument('CustomerAddress');
  343.         if($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')){
  344.             $entityManager $this->container->get('doctrine.orm.entity_manager');
  345.             $customerDataRepository $entityManager->getRepository(CustomerData::class);
  346.             $CustomerItems $this->customerPlusService->getCustomerPlusForm('shipping_edit');
  347.             foreach($CustomerItems as $CustomerItem){
  348.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  349.                     $plgCustomerAddress = new CustomerAddressCustom();
  350.                     $plgCustomerAddress->setCustomerItemId($CustomerItem->getId());
  351.                     $plgCustomerAddress->setCustomerAddress($CustomerAddress);
  352.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  353.                     $value $this->customerPlusService->convRegistData($value$CustomerItem);
  354.                     $CustomerData = new CustomerData();
  355.                     $CustomerData $customerDataRepository->regist($CustomerData$CustomerItem$value);
  356.                     $plgCustomerAddress->setCustomerData($CustomerData);
  357.                     $CustomerAddress->addCustomerAddressCustom($plgCustomerAddress);
  358.                 }
  359.             }
  360.         }
  361.         $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  362.     }
  363.     public function hookFrontContactIndexComplete(EventArgs $event)
  364.     {
  365.         $form $event->getArgument('form');
  366.         $data $event->getArgument('data');
  367.         $CustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('contact');
  368.         foreach($CustomerItems as $CustomerItem){
  369.             if($form->has('customerplus_'.$CustomerItem->getId())){
  370.                 $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  371.                 $input_type $CustomerItem->getInputType();
  372.                 if ($input_type == CustomerItem::DATE_TYPE) {
  373.                     if($value instanceof \DateTime){
  374.                         $value $value->format('Y-m-d');
  375.                     }
  376.                 } elseif($input_type >= CustomerItem::SELECT_TYPE) {
  377.                     $Options $CustomerItem->getOptions();
  378.                     if(!is_array($value))$value = [$value];
  379.                     $ret = [];
  380.                     foreach($value as $val){
  381.                         foreach($Options as $Option){
  382.                             if($Option->getId() == $val){
  383.                                 $ret[] = $Option->getText();
  384.                                 continue;
  385.                             }
  386.                         }
  387.                     }
  388.                     $value implode(','$ret);
  389.                 }
  390.                 $data['customerplus_'.$CustomerItem->getId()] = $value;
  391.             }
  392.         }
  393.         $event->setArgument('data',$data);
  394.     }
  395.     public function hookFrontShoppingShippingMultipleInitialize(EventArgs $event)
  396.     {
  397.         //ゲスト購入時の場合は削除前のShipping情報のカスタム要素を保持
  398.         $Order $event->getArgument('Order');
  399.         $Shippings $Order->getShippings();
  400.         $ShippingCustoms = [];
  401.         $ShippingNames = [];
  402.         $entityManager $this->container->get('doctrine.orm.entity_manager');
  403.         $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus4\Entity\CustomerItem');
  404.         foreach($Shippings as $Shipping){
  405.             foreach($Shipping->getShippingCustoms() as $ShippingCustom){
  406.                 $CustomerItem $customerItemRepository->find($ShippingCustom->getCustomerItemId());
  407.                 foreach($ShippingCustom->getCustomerData()->getDetails() as $Detail){
  408.                     $CustomerDatas[$ShippingCustom->getCustomerItemId()][] = ($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE) ? $Detail->getNumValue() : $Detail->getValue();
  409.                 }
  410.             }
  411.             $ShippingCustoms[$Shipping->getId()] = $CustomerDatas;
  412.             $ShippingNames[$Shipping->getId()] = $Shipping->getShippingMultipleDefaultName();
  413.         }
  414.         $this->session->set('eccube.front.shopping.nonmember.shipping'serialize($ShippingNames));
  415.         $this->session->set('eccube.front.shopping.nonmember.shippingCustom'serialize($ShippingCustoms));
  416.     }
  417.     public function hookFrontShoppingShippingMultipleComplete(EventArgs $event)
  418.     {
  419.         $form $event->getArgument('form');
  420.         $Order $event->getArgument('Order');
  421.         $data $form['shipping_multiple'];
  422.         foreach ($data as $multiples) {
  423.             foreach ($multiples as $items) {
  424.                 foreach ($items as $item) {
  425.                     $CustomerAddress $item['customer_address']->getData();
  426.                     $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  427.                     if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  428.                         foreach($Order->getShippings() as $Shipping){
  429.                             $shippingName $Shipping->getShippingMultipleDefaultName();
  430.                             if($customerAddressName == $shippingName){
  431.                                 if($customerAddressName == $Order->getShippingMultipleDefaultName()){
  432.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  433.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  434.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  435.                                                           ->setCustomerItemId($OrderCustom->getCustomerItemId())
  436.                                                           ->setCustomerAddress($CustomerAddress);
  437.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  438.                                     }
  439.                                 }
  440.                                 $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  441.                             }
  442.                         }
  443.                     }else{
  444.                         //ゲスト購入の場合は削除前に保持されていたShipping情報からデータを復元
  445.                         if ($NonMember $this->orderHelper->getNonMember()) {
  446.                             $entityManager $this->container->get('doctrine.orm.entity_manager');
  447.                             $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus4\Entity\CustomerItem');
  448.                             if ($MemShippings $this->session->get('eccube.front.shopping.nonmember.shipping')) {
  449.                                 $MemShippingNames unserialize($MemShippings);
  450.                                 $ShippingCustoms $this->session->get('eccube.front.shopping.nonmember.shippingCustom');
  451.                                 $ShippingCustoms unserialize($ShippingCustoms);
  452.                                 $flg false;
  453.                                 foreach($Order->getShippings() as $Shipping){
  454.                                     $shippingName $Shipping->getShippingMultipleDefaultName();
  455.                                     if($customerAddressName == $shippingName){
  456.                                         foreach($MemShippingNames as $memShippingId => $memShippingName){
  457.                                             if($customerAddressName == $memShippingName){
  458.                                                 foreach($ShippingCustoms as $shipping_id => $ShippingCustom){
  459.                                                     if($shipping_id == $memShippingId){
  460.                                                         foreach($ShippingCustom as $customer_item_id => $Details){
  461.                                                             $CustomerData = new CustomerData();
  462.                                                             $CustomerItem $customerItemRepository->find($customer_item_id);
  463.                                                             $CustomerData->setCustomerItem($CustomerItem);
  464.                                                             foreach($Details as $value){
  465.                                                                 $detail = new CustomerDataDetail();
  466.                                                                 $detail->setCustomerData($CustomerData);
  467.                                                                 $disp_value $value;
  468.                                                                 if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
  469.                                                                     if(!is_null($value)){
  470.                                                                         $value = new \DateTime($value);
  471.                                                                     }
  472.                                                                     $detail->setDateValue($value);
  473.                                                                 }
  474.                                                                 if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
  475.                                                                     $detail->setNumValue(intval($value));
  476.                                                                     foreach($CustomerItem->getOptions() as $Option){
  477.                                                                         if($value == $Option->getId())$disp_value $Option->getText();
  478.                                                                     }
  479.                                                                 }
  480.                                                                 $detail->setValue($disp_value);
  481.                                                                 $CustomerData->addDetail($detail);
  482.                                                             }
  483.                                                             $CustomerAddressCustom = new CustomerAddressCustom();
  484.                                                             $CustomerAddressCustom->setCustomerData($CustomerData)
  485.                                                                     ->setCustomerItemId($customer_item_id)
  486.                                                                     ->setCustomerAddress($CustomerAddress);
  487.                                                             $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  488.                                                         }
  489.                                                     }
  490.                                                 }
  491.                                                 $flg true;
  492.                                                 break;
  493.                                             }
  494.                                         }
  495.                                         $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  496.                                     }
  497.                                 }
  498.                                 if(!$flg && $customerAddressName == $Order->getShippingMultipleDefaultName()){
  499.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  500.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  501.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  502.                                                 ->setCustomerItemId($OrderCustom->getCustomerItemId())
  503.                                                 ->setCustomerAddress($CustomerAddress);
  504.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  505.                                     }
  506.                                     $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  507.                                 }
  508.                             }
  509.                         }
  510.                     }
  511.                 }
  512.             }
  513.         }
  514.     }
  515.     public function hookAdminCustomerCsvExport(EventArgs $event)
  516.     {
  517.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  518.         if ($ExportCsvRow->isDataNull()) {
  519.             $csvService $event->getArgument('csvService');
  520.             $Customer $event->getArgument('Customer');
  521.             $Csv $event->getArgument('Csv');
  522.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  523.             if($csvEntityName == 'Plugin\CustomerPlus4\Entity\CustomerCustom'){
  524.                 $data $Customer->getViewData($Csv->getFieldName());
  525.                 $ExportCsvRow->setData($data);
  526.             }
  527.         }
  528.     }
  529.     public function hookAdminOrderCsvExport(EventArgs $event)
  530.     {
  531.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  532.         if ($ExportCsvRow->isDataNull()) {
  533.             $entityManager $this->container->get('doctrine.orm.entity_manager');
  534.             $shippingReposiory $entityManager->getRepository(ShippingCustom::class);
  535.             $csvService $event->getArgument('csvService');
  536.             $OrderItem $event->getArgument('OrderItem');
  537.             $Order $OrderItem->getOrder();
  538.             $Shipping $OrderItem->getShipping();
  539.             $Csv $event->getArgument('Csv');
  540.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  541.             $data null;
  542.             if($csvEntityName == 'Plugin\CustomerPlus4\Entity\OrderCustom'){
  543.                 $data $Order->getViewData($Csv->getFieldName());
  544.             }elseif($csvEntityName == 'Plugin\CustomerPlus4\Entity\ShippingCustom'){
  545.                 $plgShipping $shippingReposiory->findOneBy(['Shipping' => $Shipping'customer_item_id' =>  $Csv->getFieldName()]);
  546.                 if($plgShipping){
  547.                     $data $plgShipping->getViewValue();
  548.                 }
  549.             }
  550.             $ExportCsvRow->setData($data);
  551.         }
  552.     }
  553.     public function insertDatePickerJS(TemplateEvent $event)
  554.     {
  555.         $twig '@CustomerPlus4/default/datepicker_js.twig';
  556.         $event->addAsset($twig);
  557.     }
  558.     private function insertDatePickerJSAdmin(TemplateEvent $event)
  559.     {
  560.         $twig '@CustomerPlus4/admin/datepicker_js.twig';
  561.         $event->addAsset($twig);
  562.     }
  563.     private function setShippingFromCustomerAddress($Shipping$CustomerAddress)
  564.     {
  565.         $entityManager $this->container->get('doctrine.orm.entity_manager');
  566.         $ShippingCustoms $Shipping->getShippingCustoms();
  567.         if(!is_null($ShippingCustoms) && count($ShippingCustoms) > 0){
  568.             foreach($ShippingCustoms as $ShippingCustom){
  569.                 $Shipping->removeShippingCustom($ShippingCustom);
  570.                 $entityManager->remove($ShippingCustom);
  571.             }
  572.         }
  573.         $entityManager->flush();
  574.         $CustomerAddressCustoms $CustomerAddress->getCustomerAddressCustoms();
  575.         if(!is_null($CustomerAddressCustoms)){
  576.             foreach($CustomerAddressCustoms as $CustomerAddressCustom){
  577.                 $CustomerItem $entityManager->getRepository(CustomerItem::class)->find($CustomerAddressCustom->getCustomerItemId());
  578.                 $CustomerData = new CustomerData();
  579.                 $CustomerData->setCustomerItem($CustomerItem);
  580.                 foreach($CustomerAddressCustom->getCustomerData()->getDetails() as $Detail){
  581.                     $shippingDetail = new CustomerDataDetail();
  582.                     $shippingDetail->setCustomerData($CustomerData)
  583.                                 ->setValue($Detail->getValue())
  584.                                 ->setDateValue($Detail->getDateValue())
  585.                                 ->setNumValue($Detail->getNumValue());
  586.                     $CustomerData->addDetail($shippingDetail);
  587.                 }
  588.                 $ShippingCustom = new ShippingCustom();
  589.                 $ShippingCustom->setCustomerData($CustomerData)
  590.                             ->setCustomerItemId($CustomerAddressCustom->getCustomerItemId())
  591.                             ->setShipping($Shipping);
  592.                 $Shipping->addShippingCustom($ShippingCustom);
  593.                 $entityManager->persist($CustomerData);
  594.                 $entityManager->persist($ShippingCustom);
  595.             }
  596.         }
  597.         $entityManager->flush();
  598.     }
  599. }