<?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 Eccube\Form\Type\Shopping\CustomerAddressType;
use Eccube\Service\OrderHelper;
use Plugin\CustomerPlus42\Entity\CustomerAddressCustom;
use Plugin\CustomerPlus42\Entity\CustomerCustom;
use Plugin\CustomerPlus42\Entity\CustomerData;
use Plugin\CustomerPlus42\Entity\CustomerDataDetail;
use Plugin\CustomerPlus42\Entity\CustomerItem;
use Plugin\CustomerPlus42\Event\AbstractEvent;
use Plugin\CustomerPlus42\Service\CustomerPlusService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ShoppingEvent extends AbstractEvent implements EventSubscriberInterface
{
private $formFactory;
private $authorizationChecker;
private $customerPlusService;
public function __construct(
ContainerInterface $container,
FormFactoryInterface $formFactory,
AuthorizationCheckerInterface $authorizationChecker,
CustomerPlusService $customerPlusService
)
{
parent::__construct($container);
$this->formFactory = $formFactory;
$this->authorizationChecker = $authorizationChecker;
$this->customerPlusService = $customerPlusService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'onTemplateShoppingIndex',
'Shopping/nonmember.twig' => 'onTemplateShoppingNonmember',
'Shopping/shipping_edit.twig' => 'insertDatePickerJS',
EccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE => 'hookFrontShoppingNonmemberComplete',
EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE => 'hookFrontShoppingShippingComplete',
EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE => 'hookFrontShoppingShippingEditInitialize',
EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE => 'hookFrontShoppingShippingEditComplete',
];
}
public function onTemplateShoppingIndex(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match('/\$\(\'#customer\'\)\.click\(function\(\)\s\{/',$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->container->getParameter('plugin_realdir'). '/CustomerPlus42/Resource/template/default/Shopping/insert_js.twig');
$replace = $search . $snippet;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
public function onTemplateShoppingNonmember(TemplateEvent $event)
{
$source = $event->getSource();
$request = $this->container->get('request_stack')->getCurrentRequest();
$session = $request->getSession();
if($session->has(OrderHelper::SESSION_NON_MEMBER)){
if($session->get(OrderHelper::SESSION_NON_MEMBER)){
if(preg_match('/url\(\'shopping_nonmember\'\)/',$source, $result)){
$search = $result[0];
$replace = "url('shopping_customer_edit')";
$source = str_replace($search, $replace, $source);
}
if(preg_match('/url\(\'cart\'\)/',$source, $result)){
$search = $result[0];
$replace = "url('shopping')";
$source = str_replace($search, $replace, $source);
}
if(preg_match('/\{\{\s\''.trans('common.next').'\'\|trans\s\}\}\<\/button\>/',$source, $result)){
$search = $result[0];
$replace = "{{ '".trans('customerplus.common.edit')."'|trans }}</button>";
$source = str_replace($search, $replace, $source);
}
}
}
$event->setSource($source);
$this->insertDatePickerJS($event);
}
public function hookFrontShoppingNonmemberComplete(EventArgs $event)
{
$form = $event->getArgument('form');
$request = $this->container->get('request_stack')->getCurrentRequest();
$session = $request->getSession();
$Customer = $session->get(OrderHelper::SESSION_NON_MEMBER);
$CustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('customer');
if($Customer instanceof \Eccube\Entity\Customer){
foreach($CustomerItems as $CustomerItem){
if($form->has('customerplus_'.$CustomerItem->getId())){
$plgCustomer = new CustomerCustom();
$plgCustomer->setCustomerItemId($CustomerItem->getId());
$plgCustomer->setCustomer($Customer);
$value = $form->get('customerplus_'.$CustomerItem->getId())->getData();
$value = $this->customerPlusService->convRegistData($value, $CustomerItem);
$CustomerData = new CustomerData();
if($CustomerItem->getInputType() == CustomerItem::CHECKBOX_TYPE){
$arrValue = explode(',', $value);
}else{
$arrValue = [$value];
}
foreach($arrValue as $value){
$detail = new CustomerDataDetail();
$detail->setCustomerData($CustomerData);
$disp_value = $value;
if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
if(!is_null($value)){
$value = new \DateTime($value);
}
$detail->setDateValue($value);
}
if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
$detail->setNumValue(intval($value));
foreach($CustomerItem->getOptions() as $Option){
if($value == $Option->getId())$disp_value = $Option->getText();
}
}
$detail->setValue($disp_value);
$CustomerData->setCustomerItem($CustomerItem);
$CustomerData->addDetail($detail);
}
$plgCustomer->setCustomerData($CustomerData);
$Customer->addCustomerCustom($plgCustomer);
}
}
$session->set(OrderHelper::SESSION_NON_MEMBER, $Customer);
}else{
$data = $session->get(OrderHelper::SESSION_NON_MEMBER);
foreach($CustomerItems as $CustomerItem){
if($form->has('customerplus_'.$CustomerItem->getId())){
$value = $form->get('customerplus_'.$CustomerItem->getId())->getData();
$data['customerplus_'.$CustomerItem->getId()] = $this->customerPlusService->convRegistData($value, $CustomerItem);
}
}
$session->set(OrderHelper::SESSION_NON_MEMBER, $data);
}
}
public function hookFrontShoppingShippingComplete(EventArgs $event)
{
$Shipping = $event->getArgument('Shipping');
$Order = $event->getArgument('Order');
$request = $this->container->get('request_stack')->getCurrentRequest();
$builder = $this->formFactory->createBuilder(CustomerAddressType::class, null, [
'customer' => $Order->getCustomer(),
'shipping' => $Shipping,
]);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$CustomerAddress = $form['addresses']->getData();
$this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
}
}
public function hookFrontShoppingShippingEditInitialize(EventArgs $event)
{
if(!$this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')){
$builder = $event->getArgument('builder');
$Shipping = $event->getArgument('Shipping');
$CustomerItems = $this->customerPlusService->getCustomerPlusForm('shipping_multiple_edit');
$plgShippings = $Shipping->getShippingCustoms();
if(!is_null($plgShippings) && count($plgShippings) > 0){
foreach($plgShippings as $plgShipping){
$CustomerItem = $plgShipping->getCustomerData()->getCustomerItem();
if(in_array($CustomerItem,$CustomerItems)){
$customer_item_id = $CustomerItem->getId();
$value = $plgShipping->getValue();
if($value){
$value = $this->customerPlusService->convSetData($value, $customer_item_id, 'customerplus_Shipping_customerplus_');
$builder->get('customerplus_'.$customer_item_id)->setData($value);
}
}
}
}
}
}
public function hookFrontShoppingShippingEditComplete(EventArgs $event)
{
$form = $event->getArgument('form');
$Shipping = $event->getArgument('Shipping');
$CustomerAddress = $event->getArgument('CustomerAddress');
if($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')){
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$customerDataRepository = $entityManager->getRepository(CustomerData::class);
$CustomerItems = $this->customerPlusService->getCustomerPlusForm('shipping_edit');
foreach($CustomerItems as $CustomerItem){
if($form->has('customerplus_'.$CustomerItem->getId())){
$plgCustomerAddress = new CustomerAddressCustom();
$plgCustomerAddress->setCustomerItemId($CustomerItem->getId());
$plgCustomerAddress->setCustomerAddress($CustomerAddress);
$value = $form->get('customerplus_'.$CustomerItem->getId())->getData();
$value = $this->customerPlusService->convRegistData($value, $CustomerItem);
$CustomerData = new CustomerData();
$CustomerData = $customerDataRepository->regist($CustomerData, $CustomerItem, $value);
$plgCustomerAddress->setCustomerData($CustomerData);
$CustomerAddress->addCustomerAddressCustom($plgCustomerAddress);
}
}
}
$this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
}
}