<?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 Plugin\CustomerPlus42\Entity\CustomerItem;
use Plugin\CustomerPlus42\Event\AbstractEvent;
use Plugin\CustomerPlus42\Service\CustomerPlusService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContactEvent extends AbstractEvent implements EventSubscriberInterface
{
private $customerPlusService;
public function __construct(
CustomerPlusService $customerPlusService
)
{
$this->customerPlusService = $customerPlusService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Contact/index.twig' => 'insertDatePickerJS',
EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE => 'hookFrontContactIndexComplete',
];
}
public function hookFrontContactIndexComplete(EventArgs $event)
{
$form = $event->getArgument('form');
$data = $event->getArgument('data');
$CustomerItems = $this->customerPlusService->getEnabledCustomerPlusForm('contact');
foreach($CustomerItems as $CustomerItem){
if($form->has('customerplus_'.$CustomerItem->getId())){
$value = $form->get('customerplus_'.$CustomerItem->getId())->getData();
$input_type = $CustomerItem->getInputType();
if ($input_type == CustomerItem::DATE_TYPE) {
if($value instanceof \DateTime){
$value = $value->format('Y-m-d');
}
} elseif($input_type >= CustomerItem::SELECT_TYPE) {
$Options = $CustomerItem->getOptions();
if(!is_array($value))$value = [$value];
$ret = [];
foreach($value as $val){
foreach($Options as $Option){
if($Option->getId() == $val){
$ret[] = $Option->getText();
continue;
}
}
}
$value = implode(',', $ret);
}
$data['customerplus_'.$CustomerItem->getId()] = $value;
}
}
$event->setArgument('data',$data);
}
}