app/Plugin/CustomerPlus42/Event/AdminOrderEvent.php line 90

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 Plugin\CustomerPlus42\Entity\ShippingCustom;
  16. use Plugin\CustomerPlus42\Event\AbstractEvent;
  17. use Plugin\CustomerPlus42\Service\CustomerPlusService;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class AdminOrderEvent extends AbstractEvent implements EventSubscriberInterface
  21. {
  22.     private $customerPlusService;
  23.     public function __construct(
  24.             ContainerInterface $container,
  25.             CustomerPlusService $customerPlusService
  26.             )
  27.     {
  28.         parent::__construct($container);
  29.         $this->customerPlusService $customerPlusService;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             '@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
  38.             '@admin/Order/search_customer.twig' => 'onTemplateAdminOrderSearchCustomer',
  39.             '@admin/Order/shipping.twig' => 'onTemplateAdminOrderShipping',
  40.             EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE => 'hookAdminOrderEditSearchCustomerByIdComplete',
  41.             EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => 'hookAdminOrderCsvExport',
  42.         ];
  43.     }
  44.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  45.     {
  46.         $parameters $event->getParameters();
  47.         $source $event->getSource();
  48.         $OrderCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('order');
  49.         $AdminCustomerItems $this->customerPlusService->getCustomerPlusForm('admin_order');
  50.         foreach($OrderCustomerItems as $key => $CustomerItem){
  51.             foreach($AdminCustomerItems as $AdminCustomerItem){
  52.                 if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($OrderCustomerItems[$key]);
  53.             }
  54.         }
  55.         $ShippingCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
  56.         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)){
  57.             $search $result[0];
  58.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_order_edit.twig');
  59.             $replace $search $snippet;
  60.             $source str_replace($search$replace$source);
  61.         }
  62.         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)){
  63.             $search $result[0];
  64.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_shipping_edit.twig');
  65.             $replace $search $snippet;
  66.             $source str_replace($search$replace$source);
  67.         }
  68.         $event->setSource($source);
  69.         $parameters['OrderCustomerItems'] = $OrderCustomerItems;
  70.         $parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
  71.         $event->setParameters($parameters);
  72.         $twig '@CustomerPlus42/admin/Order/copy_customer_js.twig';
  73.         $event->addSnippet($twig);
  74.         $this->insertDatePickerJSAdmin($event);
  75.     }
  76.     public function onTemplateAdminOrderSearchCustomer(TemplateEvent $event)
  77.     {
  78.         $parameters $event->getParameters();
  79.         $CustomerCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
  80.         $source $event->getSource();
  81.         if(preg_match('/val\(data\[\'company_name\'\]\)\;/',$source$result)){
  82.             $search $result[0];
  83.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/insert_customer_js.twig');
  84.             $replace $search $snippet;
  85.             $source str_replace($search$replace$source);
  86.         }
  87.         $event->setSource($source);
  88.         $parameters['CustomerCustomerItems'] = $CustomerCustomerItems;
  89.         $event->setParameters($parameters);
  90.     }
  91.     public function hookAdminOrderEditSearchCustomerByIdComplete(EventArgs $event)
  92.     {
  93.         $data $event->getArgument('data');
  94.         $Customer $event->getArgument('Customer');
  95.         $CustomerCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
  96.         foreach($CustomerCustomerItems as $CustomerItem){
  97.             $data['customerplus_'.$CustomerItem->getId()] = '';
  98.             $value $Customer->getCustomData($CustomerItem->getId());
  99.             $data['customerplus_'.$CustomerItem->getId()] = $value;
  100.         }
  101.         $event->setArgument('data',$data);
  102.     }
  103.     public function onTemplateAdminOrderShipping(TemplateEvent $event)
  104.     {
  105.         $parameters $event->getParameters();
  106.         $source $event->getSource();
  107.         $ShippingCustomerItems $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
  108.         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)){
  109.             $search $result[0];
  110.             $snippet file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_shipping.twig');
  111.             $replace $search $snippet;
  112.             $source str_replace($search$replace$source);
  113.         }
  114.         $event->setSource($source);
  115.         $parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
  116.         $event->setParameters($parameters);
  117.         $this->insertDatePickerJSAdmin($event);
  118.     }
  119.     public function hookAdminOrderCsvExport(EventArgs $event)
  120.     {
  121.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  122.         if ($ExportCsvRow->isDataNull()) {
  123.             $entityManager $this->container->get('doctrine.orm.entity_manager');
  124.             $shippingReposiory $entityManager->getRepository(ShippingCustom::class);
  125.             $OrderItem $event->getArgument('OrderItem');
  126.             $Order $OrderItem->getOrder();
  127.             $Shipping $OrderItem->getShipping();
  128.             $Csv $event->getArgument('Csv');
  129.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  130.             $data null;
  131.             if($csvEntityName == 'Plugin\CustomerPlus42\Entity\OrderCustom'){
  132.                 $data $Order->getViewData($Csv->getFieldName());
  133.             }elseif($csvEntityName == 'Plugin\CustomerPlus42\Entity\ShippingCustom'){
  134.                 $plgShipping $shippingReposiory->findOneBy(['Shipping' => $Shipping'customer_item_id' =>  $Csv->getFieldName()]);
  135.                 if($plgShipping){
  136.                     $data $plgShipping->getViewValue();
  137.                 }
  138.             }
  139.             $ExportCsvRow->setData($data);
  140.         }
  141.     }
  142. }