src/Eccube/Controller/Admin/Product/ClassNameController.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller\Admin\Product;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\ClassName;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Admin\ClassNameType;
  18. use Eccube\Repository\ClassNameRepository;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. class ClassNameController extends AbstractController
  26. {
  27.     /**
  28.      * @var ClassNameRepository
  29.      */
  30.     protected $classNameRepository;
  31.     /**
  32.      * ClassNameController constructor.
  33.      *
  34.      * @param ClassNameRepository $classNameRepository
  35.      */
  36.     public function __construct(ClassNameRepository $classNameRepository)
  37.     {
  38.         $this->classNameRepository $classNameRepository;
  39.     }
  40.     /**
  41.      * @Route("/%eccube_admin_route%/product/class_name", name="admin_product_class_name", methods={"GET", "POST"})
  42.      * @Route("/%eccube_admin_route%/product/class_name/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_class_name_edit", methods={"GET", "POST"})
  43.      * @Template("@admin/Product/class_name.twig")
  44.      */
  45.     public function index(Request $request$id null)
  46.     {
  47.         if ($id) {
  48.             $TargetClassName $this->classNameRepository->find($id);
  49.             if (!$TargetClassName) {
  50.                 throw new NotFoundHttpException();
  51.             }
  52.         } else {
  53.             $TargetClassName = new \Eccube\Entity\ClassName();
  54.         }
  55.         $builder $this->formFactory
  56.             ->createBuilder(ClassNameType::class, $TargetClassName);
  57.         $event = new EventArgs(
  58.             [
  59.                 'builder' => $builder,
  60.                 'TargetClassName' => $TargetClassName,
  61.             ],
  62.             $request
  63.         );
  64.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_INITIALIZE);
  65.         $ClassNames $this->classNameRepository->getList();
  66.         /**
  67.          * 編集用フォーム
  68.          */
  69.         $forms = [];
  70.         foreach ($ClassNames as $ClassName) {
  71.             $id $ClassName->getId();
  72.             $forms[$id] = $this->formFactory->createNamed('class_name_'.$idClassNameType::class, $ClassName);
  73.         }
  74.         $form $builder->getForm();
  75.         if ($request->getMethod() === 'POST') {
  76.             $form->handleRequest($request);
  77.             if ($form->isSubmitted() && $form->isValid()) {
  78.                 log_info('商品規格登録開始', [$id]);
  79.                 $this->classNameRepository->save($TargetClassName);
  80.                 log_info('商品規格登録完了', [$id]);
  81.                 $event = new EventArgs(
  82.                     [
  83.                         'form' => $form,
  84.                         'TargetClassName' => $TargetClassName,
  85.                     ],
  86.                     $request
  87.                 );
  88.                 $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_COMPLETE);
  89.                 $this->addSuccess('admin.common.save_complete''admin');
  90.                 return $this->redirectToRoute('admin_product_class_name');
  91.             }
  92.             /*
  93.              * 編集処理
  94.              */
  95.             foreach ($forms as $editForm) {
  96.                 $editForm->handleRequest($request);
  97.                 if ($editForm->isSubmitted() && $editForm->isValid()) {
  98.                     $this->classNameRepository->save($editForm->getData());
  99.                     $this->addSuccess('admin.common.save_complete''admin');
  100.                     return $this->redirectToRoute('admin_product_class_name');
  101.                 }
  102.             }
  103.         }
  104.         $formViews = [];
  105.         foreach ($forms as $key => $value) {
  106.             $formViews[$key] = $value->createView();
  107.         }
  108.         return [
  109.             'form' => $form->createView(),
  110.             'ClassNames' => $ClassNames,
  111.             'TargetClassName' => $TargetClassName,
  112.             'forms' => $formViews,
  113.         ];
  114.     }
  115.     /**
  116.      * @Route("/%eccube_admin_route%/product/class_name/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_class_name_delete", methods={"DELETE"})
  117.      */
  118.     public function delete(Request $requestClassName $ClassName)
  119.     {
  120.         $this->isTokenValid();
  121.         log_info('商品規格削除開始', [$ClassName->getId()]);
  122.         try {
  123.             $this->classNameRepository->delete($ClassName);
  124.             $event = new EventArgs(['ClassName' => $ClassName], $request);
  125.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_NAME_DELETE_COMPLETE);
  126.             $this->addSuccess('admin.common.delete_complete''admin');
  127.             log_info('商品規格削除完了', [$ClassName->getId()]);
  128.         } catch (\Exception $e) {
  129.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $ClassName->getName()]);
  130.             $this->addError($message'admin');
  131.             log_error('商品規格削除エラー', [$ClassName->getId(), $e]);
  132.         }
  133.         return $this->redirectToRoute('admin_product_class_name');
  134.     }
  135.     /**
  136.      * @Route("/%eccube_admin_route%/product/class_name/sort_no/move", name="admin_product_class_name_sort_no_move", methods={"POST"})
  137.      */
  138.     public function moveSortNo(Request $request)
  139.     {
  140.         if (!$request->isXmlHttpRequest() && $this->isTokenValid()) {
  141.             throw new BadRequestHttpException();
  142.         }
  143.         if ($this->isTokenValid()) {
  144.             $sortNos $request->request->all();
  145.             foreach ($sortNos as $classNameId => $sortNo) {
  146.                 $ClassName $this->classNameRepository
  147.                     ->find($classNameId);
  148.                 $ClassName->setSortNo($sortNo);
  149.                 $this->entityManager->persist($ClassName);
  150.             }
  151.             $this->entityManager->flush();
  152.             return new Response();
  153.         }
  154.     }
  155. }