app/Plugin/ExpressLink42/Controller/Admin/ConfigController.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ExpressLink42
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\ExpressLink42\Controller\Admin;
  12. use Eccube\Controller\AbstractController;
  13. use Plugin\ExpressLink42\Entity\ExpressLinkConfig;
  14. use Plugin\ExpressLink42\Form\Type\Admin\ConfigType;
  15. use Plugin\ExpressLink42\Repository\CodRepository;
  16. use Plugin\ExpressLink42\Repository\ConfigRepository;
  17. use Plugin\ExpressLink42\Repository\DelivtypeRepository;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class ConfigController extends AbstractController
  22. {
  23.     private $configRepository;
  24.     private $codRepository;
  25.     private $delivtypeRepository;
  26.     public function __construct(
  27.             ConfigRepository $configRepository,
  28.             CodRepository $codRepository,
  29.             DelivtypeRepository $delivtypeRepository
  30.             )
  31.     {
  32.         $this->configRepository $configRepository;
  33.         $this->codRepository $codRepository;
  34.         $this->delivtypeRepository $delivtypeRepository;
  35.     }
  36.     /**
  37.      * @Route("/%eccube_admin_route%/setting/expresslink/config", name="admin_setting_expresslink_config")
  38.      * @Template("@ExpressLink42/admin/Setting/config.twig")
  39.      */
  40.     public function index(Request $request)
  41.     {
  42.         $form $this->formFactory
  43.             ->createBuilder(ConfigType::class)
  44.             ->getForm();
  45.         $Cods $this->codRepository->findAll();
  46.         $Delivtypes $this->delivtypeRepository->findAll();
  47.         $Configs $this->configRepository->findAll();
  48.         $payments = [];
  49.         foreach($Cods as $Cod){
  50.             if(!is_null($Cod->getPayment())){
  51.                 $payments[] = $Cod->getPayment();
  52.             }
  53.         }
  54.         $form['cod']->setData($payments);
  55.         foreach($Delivtypes as $Delivtype){
  56.             $targetDelivery $Delivtype->getDelivery();
  57.             if(!is_null($targetDelivery)){
  58.                 $arrDeliveries[$Delivtype->getName()][] = $targetDelivery;
  59.             }
  60.         }
  61.         if(isset($arrDeliveries)){
  62.             foreach($arrDeliveries as $name => $deliveries){
  63.                 $form[$name]->setData($deliveries);
  64.             }
  65.         }
  66.         foreach($Configs as $config){
  67.             if(!isset($form[$config->getName()]) || is_null($config->getValue()) || is_array($config->getValue()))continue;
  68.             $form[$config->getName()]->setData($config->getValue());
  69.         }
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted()) {
  72.             if ($form->isValid()) {
  73.                 //設定内容を一度クリア
  74.                 foreach($Cods as $Cod){
  75.                     $this->entityManager->remove($Cod);
  76.                 }
  77.                 foreach($Delivtypes as $Delivtype){
  78.                     $this->entityManager->remove($Delivtype);
  79.                 }
  80.                 foreach($Configs as $config){
  81.                     $this->entityManager->remove($config);
  82.                 }
  83.                 $this->entityManager->flush();
  84.                 //設定登録
  85.                 $Values $form->getData();
  86.                 foreach($Values as $name => $value){
  87.                     if($name == 'cod'){
  88.                         $Payments $Values[$name];
  89.                         foreach($Payments as $Payment){
  90.                             $payment_id $Payment->getId();
  91.                             if(is_null($payment_id))continue;
  92.                             $Cod = new \Plugin\ExpressLink42\Entity\Cod();
  93.                             $Cod->setPayment($Payment);
  94.                             $this->entityManager->persist($Cod);
  95.                         }
  96.                     }else{
  97.                         $arrName explode('_',$name);
  98.                         if($arrName[1] == 'delivtype'){
  99.                             $Deliveries $Values[$name];
  100.                             foreach($Deliveries as $Delivery){
  101.                                 $delivery_id $Delivery->getId();
  102.                                 if(is_null($delivery_id))continue;
  103.                                 $Delivtype = new \Plugin\ExpressLink42\Entity\Delivtype();
  104.                                 $Delivtype->setDelivery($Delivery);
  105.                                 $Delivtype->setName($name);
  106.                                 $this->entityManager->persist($Delivtype);
  107.                             }
  108.                         }else{
  109.                             $Config = new ExpressLinkConfig();
  110.                             $Config->setName($name);
  111.                             $Config->setValue($value);
  112.                             $this->entityManager->persist($Config);
  113.                         }
  114.                     }
  115.                 }
  116.                 $this->entityManager->flush();
  117.                 $this->addSuccess('admin.setting.expresslink.save.complete''admin');
  118.             }
  119.         }
  120.         $Delivery = new \Eccube\Entity\Delivery();
  121.         return [
  122.             'form' => $form->createView(),
  123.             'existsDeliveryDate' => method_exists($Delivery'getDeliveryDates')
  124.         ];
  125.     }
  126. }