<?php
/*
* Plugin Name : ExpressLink42
*
* 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\ExpressLink42\Controller\Admin;
use Eccube\Controller\AbstractController;
use Plugin\ExpressLink42\Entity\ExpressLinkConfig;
use Plugin\ExpressLink42\Form\Type\Admin\ConfigType;
use Plugin\ExpressLink42\Repository\CodRepository;
use Plugin\ExpressLink42\Repository\ConfigRepository;
use Plugin\ExpressLink42\Repository\DelivtypeRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ConfigController extends AbstractController
{
private $configRepository;
private $codRepository;
private $delivtypeRepository;
public function __construct(
ConfigRepository $configRepository,
CodRepository $codRepository,
DelivtypeRepository $delivtypeRepository
)
{
$this->configRepository = $configRepository;
$this->codRepository = $codRepository;
$this->delivtypeRepository = $delivtypeRepository;
}
/**
* @Route("/%eccube_admin_route%/setting/expresslink/config", name="admin_setting_expresslink_config")
* @Template("@ExpressLink42/admin/Setting/config.twig")
*/
public function index(Request $request)
{
$form = $this->formFactory
->createBuilder(ConfigType::class)
->getForm();
$Cods = $this->codRepository->findAll();
$Delivtypes = $this->delivtypeRepository->findAll();
$Configs = $this->configRepository->findAll();
$payments = [];
foreach($Cods as $Cod){
if(!is_null($Cod->getPayment())){
$payments[] = $Cod->getPayment();
}
}
$form['cod']->setData($payments);
foreach($Delivtypes as $Delivtype){
$targetDelivery = $Delivtype->getDelivery();
if(!is_null($targetDelivery)){
$arrDeliveries[$Delivtype->getName()][] = $targetDelivery;
}
}
if(isset($arrDeliveries)){
foreach($arrDeliveries as $name => $deliveries){
$form[$name]->setData($deliveries);
}
}
foreach($Configs as $config){
if(!isset($form[$config->getName()]) || is_null($config->getValue()) || is_array($config->getValue()))continue;
$form[$config->getName()]->setData($config->getValue());
}
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
//設定内容を一度クリア
foreach($Cods as $Cod){
$this->entityManager->remove($Cod);
}
foreach($Delivtypes as $Delivtype){
$this->entityManager->remove($Delivtype);
}
foreach($Configs as $config){
$this->entityManager->remove($config);
}
$this->entityManager->flush();
//設定登録
$Values = $form->getData();
foreach($Values as $name => $value){
if($name == 'cod'){
$Payments = $Values[$name];
foreach($Payments as $Payment){
$payment_id = $Payment->getId();
if(is_null($payment_id))continue;
$Cod = new \Plugin\ExpressLink42\Entity\Cod();
$Cod->setPayment($Payment);
$this->entityManager->persist($Cod);
}
}else{
$arrName = explode('_',$name);
if($arrName[1] == 'delivtype'){
$Deliveries = $Values[$name];
foreach($Deliveries as $Delivery){
$delivery_id = $Delivery->getId();
if(is_null($delivery_id))continue;
$Delivtype = new \Plugin\ExpressLink42\Entity\Delivtype();
$Delivtype->setDelivery($Delivery);
$Delivtype->setName($name);
$this->entityManager->persist($Delivtype);
}
}else{
$Config = new ExpressLinkConfig();
$Config->setName($name);
$Config->setValue($value);
$this->entityManager->persist($Config);
}
}
}
$this->entityManager->flush();
$this->addSuccess('admin.setting.expresslink.save.complete', 'admin');
}
}
$Delivery = new \Eccube\Entity\Delivery();
return [
'form' => $form->createView(),
'existsDeliveryDate' => method_exists($Delivery, 'getDeliveryDates')
];
}
}