app/Plugin/ProductOption42/Event/AdminOrderEvent.php line 68

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  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\ProductOption42\Event;
  12. use Eccube\Common\EccubeConfig;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Event\TemplateEvent;
  16. use Plugin\ProductOption42\Service\ProductOptionService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class AdminOrderEvent implements EventSubscriberInterface
  19. {
  20.     private $eccubeConfig;
  21.     private $productOptionService;
  22.     public function __construct(
  23.         EccubeConfig $eccubeConfig,
  24.         ProductOptionService $productOptionService
  25.             )
  26.     {
  27.         $this->eccubeConfig $eccubeConfig;
  28.         $this->productOptionService $productOptionService;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             '@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
  37.             '@admin/Order/order_item_prototype.twig' => 'onTemplateAdminOrderItemPrototype',
  38.             '@admin/Order/shipping.twig' => 'onTemplateAdminOrderEdit',
  39.             '@admin/Order/search_product.twig' => 'onTemplateAdminOrderSearchProduct',
  40.             EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => 'hookAdminOrderCsvExport',
  41.         ];
  42.     }
  43.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  44.     {
  45.         $source $event->getSource();
  46.         if(preg_match("/\{\{\sOrderItem\.product\_name\s\}\}/",$source$result)){
  47.             $search $result[0];
  48.             $replace $search "{{ include('@ProductOption42/admin/Order/orderitem_option.twig') }}";
  49.             $source str_replace($search$replace$source);
  50.         }
  51.         if(preg_match("/\{\{\s*form\_widget\(orderItemForm\.product\_name,\s*\{\s*'type'\:\s*'hidden'\s*\}\)\s*\}\}/",$source$result)){
  52.             $search $result[0];
  53.             $replace $search "{{ form_widget(orderItemForm.option_serial, { 'type': 'hidden' }) }}";
  54.             $source str_replace($search$replace$source);
  55.         }
  56.         $event->setSource($source);
  57.     }
  58.     public function onTemplateAdminOrderItemPrototype(TemplateEvent $event)
  59.     {
  60.         $source $event->getSource();
  61.         if(preg_match("/\{\{\s*form\_widget\(orderItemForm\.tax\_type\)\s*\}\}/",$source$result)){
  62.             $search $result[0];
  63.             $replace $search "{{ form_widget(orderItemForm.option_serial) }}";
  64.             $source str_replace($search$replace$source);
  65.         }
  66.         $event->setSource($source);
  67.     }
  68.     public function onTemplateAdminOrderSearchProduct(TemplateEvent $event)
  69.     {
  70.         $source $event->getSource();
  71.         if(preg_match("/\<\/script\>/",$source$result)){
  72.             $search $result[0];
  73.             $replace $search "{{ include('@ProductOption42/admin/Order/search_product_js.twig') }}";
  74.             $source str_replace($search$replace$source);
  75.         }
  76.         if(preg_match("/fnAddOrderItem/",$source$result)){
  77.             $search $result[0];
  78.             $replace "fnAddOrderItemOption";
  79.             $source str_replace($search$replace$source);
  80.         }
  81.         if(preg_match("/\{\{\s\'admin\.product\.product\_class\_\_short\'\|trans\s\}\}/",$source$result)){
  82.             $search $result[0];
  83.             $replace $search "/{{ 'productoption.admin.common.option'|trans }}";
  84.             $source str_replace($search$replace$source);
  85.         }
  86.         if(preg_match("/\<\/td\>[\n|\r\n|\r]\s*\<td class=\"align\-middle pr\-3 text\-end\"\>/",$source$result)){
  87.             $search $result[0];
  88.             $snippet file_get_contents($this->eccubeConfig->get('plugin_realdir'). '/ProductOption42/Resource/template/admin/Order/search_product_option.twig');
  89.             $replace $snippet $search;
  90.             $source str_replace($search$replace$source);
  91.         }elseif(preg_match("/\<\/td\>[\n|\r\n|\r]\s*\{\%\sif\sProduct\.stock_find\s\%\}/",$source$result)){
  92.             $search $result[0];
  93.             $snippet file_get_contents($this->eccubeConfig->get('plugin_realdir'). '/ProductOption42/Resource/template/admin/Order/search_product_option.twig');
  94.             $replace $snippet $search;
  95.             $source str_replace($search$replace$source);
  96.         }
  97.         $event->setSource($source);
  98.         $parameters $event->getParameters();
  99.         $Products $parameters['Products'];
  100.         $optionParameters $this->productOptionService->getOptionParameters($Products);
  101.         $parameters array_merge($parameters$optionParameters);
  102.         $event->setParameters($parameters);
  103.     }
  104.     public function hookAdminOrderCsvExport(EventArgs $event)
  105.     {
  106.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  107.         if ($ExportCsvRow->isDataNull()) {
  108.             $OrderItem $event->getArgument('OrderItem');
  109.             $Csv $event->getArgument('Csv');
  110.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  111.             if($csvEntityName == 'Plugin\ProductOption42\Entity\OrderItemOption'){
  112.                 $data null;
  113.                 $arrData = [];
  114.                 $OrderItemOptions $OrderItem->getOrderItemOptions();
  115.                 if(!is_null($Csv->getReferenceFieldName())){
  116.                     $optionId $Csv->getReferenceFieldName();
  117.                     foreach($OrderItemOptions as $OrderItemOption){
  118.                         if($optionId == $OrderItemOption->getOptionId()){
  119.                             foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
  120.                                 $arrData[] = $OrderItemOptionCategory->getValue();
  121.                             }
  122.                         }
  123.                     }
  124.                     if(count($arrData) > 0)$data implode(',',$arrData);
  125.                 }else{
  126.                     foreach($OrderItemOptions as $OrderItemOption){
  127.                         $text $OrderItemOption->getLabel(). ':';
  128.                         foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
  129.                             $text .= $OrderItemOptionCategory->getValue() .',';
  130.                         }
  131.                         $arrData[] = rtrim($text,',');
  132.                     }
  133.                     if(count($arrData) > 0)$data implode(',',$arrData);
  134.                 }
  135.                 $ExportCsvRow->setData($data);
  136.             }
  137.         }
  138.     }
  139. }