<?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\Service\OrderHelper;
use Plugin\CustomerPlus42\Entity\CustomerAddressCustom;
use Plugin\CustomerPlus42\Entity\CustomerData;
use Plugin\CustomerPlus42\Entity\CustomerDataDetail;
use Plugin\CustomerPlus42\Entity\CustomerItem;
use Plugin\CustomerPlus42\Event\AbstractEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ShippingMultipleEvent extends AbstractEvent implements EventSubscriberInterface
{
private $session;
private $authorizationChecker;
private $orderHelper;
public function __construct(
ContainerInterface $container,
SessionInterface $session,
AuthorizationCheckerInterface $authorizationChecker,
OrderHelper $orderHelper
)
{
parent::__construct($container);
$this->session = $session;
$this->authorizationChecker = $authorizationChecker;
$this->orderHelper = $orderHelper;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/shipping_multiple_edit.twig' => 'insertDatePickerJS',
EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE => 'hookFrontShoppingShippingMultipleInitialize',
EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE => ['hookFrontShoppingShippingMultipleComplete',-10],
];
}
public function hookFrontShoppingShippingMultipleInitialize(EventArgs $event)
{
//ゲスト購入時の場合は削除前のShipping情報のカスタム要素を保持
$Order = $event->getArgument('Order');
$Shippings = $Order->getShippings();
$ShippingCustoms = [];
$ShippingNames = [];
$CustomerDatas = [];
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$customerItemRepository = $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
foreach($Shippings as $Shipping){
$CustomerDatas = [];
foreach($Shipping->getShippingCustoms() as $ShippingCustom){
$CustomerItem = $customerItemRepository->find($ShippingCustom->getCustomerItemId());
foreach($ShippingCustom->getCustomerData()->getDetails() as $Detail){
$CustomerDatas[$ShippingCustom->getCustomerItemId()][] = ($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE) ? $Detail->getNumValue() : $Detail->getValue();
}
}
$ShippingCustoms[$Shipping->getId()] = $CustomerDatas;
$ShippingNames[$Shipping->getId()] = $Shipping->getShippingMultipleDefaultName();
}
$this->session->set('eccube.front.shopping.nonmember.shipping', serialize($ShippingNames));
$this->session->set('eccube.front.shopping.nonmember.shippingCustom', serialize($ShippingCustoms));
}
public function hookFrontShoppingShippingMultipleComplete(EventArgs $event)
{
$form = $event->getArgument('form');
$Order = $event->getArgument('Order');
$data = $form['shipping_multiple'];
foreach ($data as $multiples) {
foreach ($multiples as $items) {
foreach ($items as $item) {
$CustomerAddress = $item['customer_address']->getData();
$customerAddressName = $CustomerAddress->getShippingMultipleDefaultName();
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
foreach($Order->getShippings() as $Shipping){
$shippingName = $Shipping->getShippingMultipleDefaultName();
if($customerAddressName == $shippingName){
if($customerAddressName == $Order->getShippingMultipleDefaultName()){
foreach($Order->getOrderCustoms() as $OrderCustom){
$CustomerAddressCustom = new CustomerAddressCustom();
$CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
->setCustomerItemId($OrderCustom->getCustomerItemId())
->setCustomerAddress($CustomerAddress);
$CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
}
}
$this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
}
}
}else{
//ゲスト購入の場合は削除前に保持されていたShipping情報からデータを復元
if ($NonMember = $this->orderHelper->getNonMember()) {
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$customerItemRepository = $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
if ($MemShippings = $this->session->get('eccube.front.shopping.nonmember.shipping')) {
$MemShippingNames = unserialize($MemShippings);
$ShippingCustoms = $this->session->get('eccube.front.shopping.nonmember.shippingCustom');
$ShippingCustoms = unserialize($ShippingCustoms);
$flg = false;
foreach($Order->getShippings() as $Shipping){
$shippingName = $Shipping->getShippingMultipleDefaultName();
if($customerAddressName == $shippingName){
foreach($MemShippingNames as $memShippingId => $memShippingName){
if($customerAddressName == $memShippingName){
foreach($ShippingCustoms as $shipping_id => $ShippingCustom){
if($shipping_id == $memShippingId){
foreach($ShippingCustom as $customer_item_id => $Details){
$CustomerData = new CustomerData();
$CustomerItem = $customerItemRepository->find($customer_item_id);
$CustomerData->setCustomerItem($CustomerItem);
foreach($Details 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->addDetail($detail);
}
$CustomerAddressCustom = new CustomerAddressCustom();
$CustomerAddressCustom->setCustomerData($CustomerData)
->setCustomerItemId($customer_item_id)
->setCustomerAddress($CustomerAddress);
$CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
}
}
}
$flg = true;
break;
}
}
$this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
}
}
if(!$flg && $customerAddressName == $Order->getShippingMultipleDefaultName()){
foreach($Order->getOrderCustoms() as $OrderCustom){
$CustomerAddressCustom = new CustomerAddressCustom();
$CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
->setCustomerItemId($OrderCustom->getCustomerItemId())
->setCustomerAddress($CustomerAddress);
$CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
}
$this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
}
}
}
}
}
}
}
}
}