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

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