app/Plugin/CustomerPlus42/Event/ShoppingEvent.php line 80

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\Event\TemplateEvent;
  15. use Eccube\Form\Type\Shopping\CustomerAddressType;
  16. use Eccube\Service\OrderHelper;
  17. use Plugin\CustomerPlus42\Entity\CustomerAddressCustom;
  18. use Plugin\CustomerPlus42\Entity\CustomerCustom;
  19. use Plugin\CustomerPlus42\Entity\CustomerData;
  20. use Plugin\CustomerPlus42\Entity\CustomerDataDetail;
  21. use Plugin\CustomerPlus42\Entity\CustomerItem;
  22. use Plugin\CustomerPlus42\Event\AbstractEvent;
  23. use Plugin\CustomerPlus42\Service\CustomerPlusService;
  24. use Symfony\Component\DependencyInjection\ContainerInterface;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\Form\FormFactoryInterface;
  27. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  28. class ShoppingEvent extends AbstractEvent implements EventSubscriberInterface
  29. {
  30.     private $formFactory;
  31.     private $authorizationChecker;
  32.     private $customerPlusService;
  33.     public function __construct(
  34.             ContainerInterface $container,
  35.             FormFactoryInterface $formFactory,
  36.             AuthorizationCheckerInterface $authorizationChecker,
  37.             CustomerPlusService $customerPlusService
  38.             )
  39.     {
  40.         parent::__construct($container);
  41.         $this->formFactory $formFactory;
  42.         $this->authorizationChecker $authorizationChecker;
  43.         $this->customerPlusService $customerPlusService;
  44.     }
  45.     /**
  46.      * @return array
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             'Shopping/index.twig' => 'onTemplateShoppingIndex',
  52.             'Shopping/nonmember.twig' => 'onTemplateShoppingNonmember',
  53.             'Shopping/shipping_edit.twig' => 'insertDatePickerJS',
  54.             EccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE => 'hookFrontShoppingNonmemberComplete',
  55.             EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE => 'hookFrontShoppingShippingComplete',
  56.             EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE => 'hookFrontShoppingShippingEditInitialize',
  57.             EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE => 'hookFrontShoppingShippingEditComplete',
  58.         ];
  59.     }
  60.     public function onTemplateShoppingIndex(TemplateEvent $event)
  61.     {
  62.         $source $event->getSource();
  63.         if(preg_match('/\$\(\'#customer\'\)\.click\(function\(\)\s\{/',$source$result)){
  64.             $search $result[0];
  65.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/default/Shopping/insert_js.twig');
  66.             $replace $search $snippet;
  67.             $source str_replace($search$replace$source);
  68.         }
  69.         $event->setSource($source);
  70.     }
  71.     public function onTemplateShoppingNonmember(TemplateEvent $event)
  72.     {
  73.         $source $event->getSource();
  74.         $request $this->container->get('request_stack')->getCurrentRequest();
  75.         $session $request->getSession();
  76.         if($session->has(OrderHelper::SESSION_NON_MEMBER)){
  77.             if($session->get(OrderHelper::SESSION_NON_MEMBER)){
  78.                 if(preg_match('/url\(\'shopping_nonmember\'\)/',$source$result)){
  79.                     $search $result[0];
  80.                     $replace "url('shopping_customer_edit')";
  81.                     $source str_replace($search$replace$source);
  82.                 }
  83.                 if(preg_match('/url\(\'cart\'\)/',$source$result)){
  84.                     $search $result[0];
  85.                     $replace "url('shopping')";
  86.                     $source str_replace($search$replace$source);
  87.                 }
  88.                 if(preg_match('/\{\{\s\''.trans('common.next').'\'\|trans\s\}\}\<\/button\>/',$source$result)){
  89.                     $search $result[0];
  90.                     $replace "{{ '".trans('customerplus.common.edit')."'|trans }}</button>";
  91.                     $source str_replace($search$replace$source);
  92.                 }
  93.             }
  94.         }
  95.         $event->setSource($source);
  96.         $this->insertDatePickerJS($event);
  97.     }
  98.     public function hookFrontShoppingNonmemberComplete(EventArgs $event)
  99.     {
  100.         $form $event->getArgument('form');
  101.         $request $this->container->get('request_stack')->getCurrentRequest();
  102.         $session $request->getSession();
  103.         $Customer $session->get(OrderHelper::SESSION_NON_MEMBER);
  104.         $CustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('customer');
  105.         if($Customer instanceof \Eccube\Entity\Customer){
  106.             foreach($CustomerItems as $CustomerItem){
  107.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  108.                     $plgCustomer = new CustomerCustom();
  109.                     $plgCustomer->setCustomerItemId($CustomerItem->getId());
  110.                     $plgCustomer->setCustomer($Customer);
  111.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  112.                     $value $this->customerPlusService->convRegistData($value$CustomerItem);
  113.                     $CustomerData = new CustomerData();
  114.                     if($CustomerItem->getInputType() == CustomerItem::CHECKBOX_TYPE){
  115.                         $arrValue explode(','$value);
  116.                     }else{
  117.                         $arrValue = [$value];
  118.                     }
  119.                     foreach($arrValue as $value){
  120.                         $detail = new CustomerDataDetail();
  121.                         $detail->setCustomerData($CustomerData);
  122.                         $disp_value $value;
  123.                         if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
  124.                             if(!is_null($value)){
  125.                                 $value = new \DateTime($value);
  126.                             }
  127.                             $detail->setDateValue($value);
  128.                         }
  129.                         if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
  130.                             $detail->setNumValue(intval($value));
  131.                             foreach($CustomerItem->getOptions() as $Option){
  132.                                 if($value == $Option->getId())$disp_value $Option->getText();
  133.                             }
  134.                         }
  135.                         $detail->setValue($disp_value);
  136.                         $CustomerData->setCustomerItem($CustomerItem);
  137.                         $CustomerData->addDetail($detail);
  138.                     }
  139.                     $plgCustomer->setCustomerData($CustomerData);
  140.                     $Customer->addCustomerCustom($plgCustomer);
  141.                 }
  142.             }
  143.             $session->set(OrderHelper::SESSION_NON_MEMBER$Customer);
  144.         }else{
  145.             $data $session->get(OrderHelper::SESSION_NON_MEMBER);
  146.             foreach($CustomerItems as $CustomerItem){
  147.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  148.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  149.                     $data['customerplus_'.$CustomerItem->getId()] = $this->customerPlusService->convRegistData($value$CustomerItem);
  150.                 }
  151.             }
  152.             $session->set(OrderHelper::SESSION_NON_MEMBER$data);
  153.         }
  154.     }
  155.     public function hookFrontShoppingShippingComplete(EventArgs $event)
  156.     {
  157.         $Shipping $event->getArgument('Shipping');
  158.         $Order $event->getArgument('Order');
  159.         $request $this->container->get('request_stack')->getCurrentRequest();
  160.         $builder $this->formFactory->createBuilder(CustomerAddressType::class, null, [
  161.             'customer' => $Order->getCustomer(),
  162.             'shipping' => $Shipping,
  163.         ]);
  164.         $form $builder->getForm();
  165.         $form->handleRequest($request);
  166.         if ($form->isSubmitted() && $form->isValid()) {
  167.             $CustomerAddress $form['addresses']->getData();
  168.             $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  169.         }
  170.     }
  171.     public function hookFrontShoppingShippingEditInitialize(EventArgs $event)
  172.     {
  173.         if(!$this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
  174.             $builder $event->getArgument('builder');
  175.             $Shipping $event->getArgument('Shipping');
  176.             $CustomerItems $this->customerPlusService->getCustomerPlusForm('shipping_multiple_edit');
  177.             $plgShippings $Shipping->getShippingCustoms();
  178.             if(!is_null($plgShippings) && count($plgShippings) > 0){
  179.                 foreach($plgShippings as $plgShipping){
  180.                     $CustomerItem $plgShipping->getCustomerData()->getCustomerItem();
  181.                     if(in_array($CustomerItem,$CustomerItems)){
  182.                         $customer_item_id $CustomerItem->getId();
  183.                         $value $plgShipping->getValue();
  184.                         if($value){
  185.                             $value $this->customerPlusService->convSetData($value$customer_item_id'customerplus_Shipping_customerplus_');
  186.                             $builder->get('customerplus_'.$customer_item_id)->setData($value);
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.     public function hookFrontShoppingShippingEditComplete(EventArgs $event)
  194.     {
  195.         $form $event->getArgument('form');
  196.         $Shipping $event->getArgument('Shipping');
  197.         $CustomerAddress $event->getArgument('CustomerAddress');
  198.         if($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')){
  199.             $entityManager $this->container->get('doctrine.orm.entity_manager');
  200.             $customerDataRepository $entityManager->getRepository(CustomerData::class);
  201.             $CustomerItems $this->customerPlusService->getCustomerPlusForm('shipping_edit');
  202.             foreach($CustomerItems as $CustomerItem){
  203.                 if($form->has('customerplus_'.$CustomerItem->getId())){
  204.                     $plgCustomerAddress = new CustomerAddressCustom();
  205.                     $plgCustomerAddress->setCustomerItemId($CustomerItem->getId());
  206.                     $plgCustomerAddress->setCustomerAddress($CustomerAddress);
  207.                     $value $form->get('customerplus_'.$CustomerItem->getId())->getData();
  208.                     $value $this->customerPlusService->convRegistData($value$CustomerItem);
  209.                     $CustomerData = new CustomerData();
  210.                     $CustomerData $customerDataRepository->regist($CustomerData$CustomerItem$value);
  211.                     $plgCustomerAddress->setCustomerData($CustomerData);
  212.                     $CustomerAddress->addCustomerAddressCustom($plgCustomerAddress);
  213.                 }
  214.             }
  215.         }
  216.         $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  217.     }
  218. }