<?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\Event\AbstractEvent;
use Plugin\CustomerPlus42\Service\CustomerPlusService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminCustomerEvent 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/Customer/edit.twig' => 'onTemplateAdminCustomerEdit',
'@admin/Customer/delivery_edit.twig' => 'onTemplateAdminCustomerDeliveryEdit',
EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT => 'hookAdminCustomerCsvExport',
];
}
public function onTemplateAdminCustomerEdit(TemplateEvent $event)
{
$parameters = $event->getParameters();
$source = $event->getSource();
$CustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('customer');
$AdminCustomerItems = $this->customerPlusService->getCustomerPlusForm('admin_customer');
foreach($CustomerItems as $key => $CustomerItem){
foreach($AdminCustomerItems as $AdminCustomerItem){
if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($CustomerItems[$key]);
}
}
if(preg_match('/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/',$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Customer/ext_edit.twig');
$replace = $snippet . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters['CustomerItems'] = $CustomerItems;
$event->setParameters($parameters);
$this->insertDatePickerJSAdmin($event);
}
public function onTemplateAdminCustomerDeliveryEdit(TemplateEvent $event)
{
$parameters = $event->getParameters();
$source = $event->getSource();
$CustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('shipping');
$AdminCustomerItems = $this->customerPlusService->getCustomerPlusForm('admin_delivery');
foreach($CustomerItems as $key => $CustomerItem){
foreach($AdminCustomerItems as $AdminCustomerItem){
if($CustomerItem->getId() == $AdminCustomerItem->getId())unset($CustomerItems[$key]);
}
}
if(preg_match('/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/',$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/admin/Customer/ext_edit.twig');
$replace = $snippet . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters['CustomerItems'] = $CustomerItems;
$event->setParameters($parameters);
$this->insertDatePickerJSAdmin($event);
}
public function hookAdminCustomerCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$Customer = $event->getArgument('Customer');
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if($csvEntityName == 'Plugin\CustomerPlus42\Entity\CustomerCustom'){
$data = $Customer->getViewData($Csv->getFieldName());
$ExportCsvRow->setData($data);
}
}
}
}