<?php
/*
* Plugin Name : CustomerPlus4
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerPlus42\Event;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\CustomerPlus42\Entity\ShippingCustom;
use Plugin\CustomerPlus42\Event\AbstractEvent;
use Plugin\CustomerPlus42\Service\CustomerPlusService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminOrderEvent extends AbstractEvent implements EventSubscriberInterface
{
private $customerPlusService;
public function __construct(
ContainerInterface $container,
CustomerPlusService $customerPlusService
)
{
parent::__construct($container);
$this->customerPlusService = $customerPlusService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
'@admin/Order/search_customer.twig' => 'onTemplateAdminOrderSearchCustomer',
'@admin/Order/shipping.twig' => 'onTemplateAdminOrderShipping',
EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_BY_ID_COMPLETE => 'hookAdminOrderEditSearchCustomerByIdComplete',
EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => 'hookAdminOrderCsvExport',
];
}
public function onTemplateAdminOrderEdit(TemplateEvent $event)
{
$parameters = $event->getParameters();
$source = $event->getSource();
$OrderCustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('order');
$AdminCustomerItems = $this->customerPlusService->getCustomerPlusForm('admin_order');
foreach($OrderCustomerItems as $key => $CustomerItem){
foreach($AdminCustomerItems as $AdminCustomerItem){
if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($OrderCustomerItems[$key]);
}
}
$ShippingCustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
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)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_order_edit.twig');
$replace = $search . $snippet;
$source = str_replace($search, $replace, $source);
}
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)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_shipping_edit.twig');
$replace = $search . $snippet;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters['OrderCustomerItems'] = $OrderCustomerItems;
$parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
$event->setParameters($parameters);
$twig = '@CustomerPlus42/admin/Order/copy_customer_js.twig';
$event->addSnippet($twig);
$this->insertDatePickerJSAdmin($event);
}
public function onTemplateAdminOrderSearchCustomer(TemplateEvent $event)
{
$parameters = $event->getParameters();
$CustomerCustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
$source = $event->getSource();
if(preg_match('/val\(data\[\'company_name\'\]\)\;/',$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/insert_customer_js.twig');
$replace = $search . $snippet;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters['CustomerCustomerItems'] = $CustomerCustomerItems;
$event->setParameters($parameters);
}
public function hookAdminOrderEditSearchCustomerByIdComplete(EventArgs $event)
{
$data = $event->getArgument('data');
$Customer = $event->getArgument('Customer');
$CustomerCustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('admin_customer');
foreach($CustomerCustomerItems as $CustomerItem){
$data['customerplus_'.$CustomerItem->getId()] = '';
$value = $Customer->getCustomData($CustomerItem->getId());
$data['customerplus_'.$CustomerItem->getId()] = $value;
}
$event->setArgument('data',$data);
}
public function onTemplateAdminOrderShipping(TemplateEvent $event)
{
$parameters = $event->getParameters();
$source = $event->getSource();
$ShippingCustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('admin_delivery');
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)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Order/ext_shipping.twig');
$replace = $search . $snippet;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters['ShippingCustomerItems'] = $ShippingCustomerItems;
$event->setParameters($parameters);
$this->insertDatePickerJSAdmin($event);
}
public function hookAdminOrderCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$shippingReposiory = $entityManager->getRepository(ShippingCustom::class);
$OrderItem = $event->getArgument('OrderItem');
$Order = $OrderItem->getOrder();
$Shipping = $OrderItem->getShipping();
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
$data = null;
if($csvEntityName == 'Plugin\CustomerPlus42\Entity\OrderCustom'){
$data = $Order->getViewData($Csv->getFieldName());
}elseif($csvEntityName == 'Plugin\CustomerPlus42\Entity\ShippingCustom'){
$plgShipping = $shippingReposiory->findOneBy(['Shipping' => $Shipping, 'customer_item_id' => $Csv->getFieldName()]);
if($plgShipping){
$data = $plgShipping->getViewValue();
}
}
$ExportCsvRow->setData($data);
}
}
}