src/Eccube/Controller/Admin/Product/ClassCategoryController.php line 69

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\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\Admin\ClassCategoryType;
  17. use Eccube\Repository\ClassCategoryRepository;
  18. use Eccube\Repository\ClassNameRepository;
  19. use Eccube\Repository\ProductClassRepository;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. class ClassCategoryController extends AbstractController
  27. {
  28.     /**
  29.      * @var ProductClassRepository
  30.      */
  31.     protected $productClassRepository;
  32.     /**
  33.      * @var ClassCategoryRepository
  34.      */
  35.     protected $classCategoryRepository;
  36.     /**
  37.      * @var ClassNameRepository
  38.      */
  39.     protected $classNameRepository;
  40.     /**
  41.      * ClassCategoryController constructor.
  42.      *
  43.      * @param ProductClassRepository $productClassRepository
  44.      * @param ClassCategoryRepository $classCategoryRepository
  45.      * @param ClassNameRepository $classNameRepository
  46.      */
  47.     public function __construct(
  48.         ProductClassRepository $productClassRepository,
  49.         ClassCategoryRepository $classCategoryRepository,
  50.         ClassNameRepository $classNameRepository
  51.     ) {
  52.         $this->productClassRepository $productClassRepository;
  53.         $this->classCategoryRepository $classCategoryRepository;
  54.         $this->classNameRepository $classNameRepository;
  55.     }
  56.     /**
  57.      * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}", requirements={"class_name_id" = "\d+"}, name="admin_product_class_category", methods={"GET", "POST"})
  58.      * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/edit", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_edit", methods={"GET", "POST"})
  59.      * @Template("@admin/Product/class_category.twig")
  60.      */
  61.     public function index(Request $request$class_name_id$id null)
  62.     {
  63.         $ClassName $this->classNameRepository->find($class_name_id);
  64.         if (!$ClassName) {
  65.             throw new NotFoundHttpException();
  66.         }
  67.         if ($id) {
  68.             $TargetClassCategory $this->classCategoryRepository->find($id);
  69.             if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
  70.                 throw new NotFoundHttpException();
  71.             }
  72.         } else {
  73.             $TargetClassCategory = new \Eccube\Entity\ClassCategory();
  74.             $TargetClassCategory->setClassName($ClassName);
  75.         }
  76.         $builder $this->formFactory
  77.             ->createBuilder(ClassCategoryType::class, $TargetClassCategory);
  78.         $event = new EventArgs(
  79.             [
  80.                 'builder' => $builder,
  81.                 'ClassName' => $ClassName,
  82.                 'TargetClassCategory' => $TargetClassCategory,
  83.             ],
  84.             $request
  85.         );
  86.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_INITIALIZE);
  87.         $ClassCategories $this->classCategoryRepository->getList($ClassName);
  88.         $forms = [];
  89.         foreach ($ClassCategories as $ClassCategory) {
  90.             $id $ClassCategory->getId();
  91.             $forms[$id] = $this->formFactory->createNamed('class_category_'.$idClassCategoryType::class, $ClassCategory);
  92.         }
  93.         $form $builder->getForm();
  94.         if ($request->getMethod() === 'POST') {
  95.             $form->handleRequest($request);
  96.             if ($form->isSubmitted() && $form->isValid()) {
  97.                 log_info('規格分類登録開始', [$id]);
  98.                 $this->classCategoryRepository->save($TargetClassCategory);
  99.                 log_info('規格分類登録完了', [$id]);
  100.                 $event = new EventArgs(
  101.                     [
  102.                         'form' => $form,
  103.                         'ClassName' => $ClassName,
  104.                         'TargetClassCategory' => $TargetClassCategory,
  105.                     ],
  106.                     $request
  107.                 );
  108.                 $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE);
  109.                 $this->addSuccess('admin.common.save_complete''admin');
  110.                 return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  111.             }
  112.             foreach ($forms as $editForm) {
  113.                 $editForm->handleRequest($request);
  114.                 if ($editForm->isSubmitted() && $editForm->isValid()) {
  115.                     $this->classCategoryRepository->save($editForm->getData());
  116.                     $this->addSuccess('admin.common.save_complete''admin');
  117.                     return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  118.                 }
  119.             }
  120.         }
  121.         $formViews = [];
  122.         foreach ($forms as $key => $value) {
  123.             $formViews[$key] = $value->createView();
  124.         }
  125.         return [
  126.             'form' => $form->createView(),
  127.             'ClassName' => $ClassName,
  128.             'ClassCategories' => $ClassCategories,
  129.             'TargetClassCategory' => $TargetClassCategory,
  130.             'forms' => $formViews,
  131.         ];
  132.     }
  133.     /**
  134.      * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/delete", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_delete", methods={"DELETE"})
  135.      */
  136.     public function delete(Request $request$class_name_id$id)
  137.     {
  138.         $this->isTokenValid();
  139.         $ClassName $this->classNameRepository->find($class_name_id);
  140.         if (!$ClassName) {
  141.             throw new NotFoundHttpException();
  142.         }
  143.         log_info('規格分類削除開始', [$id]);
  144.         $TargetClassCategory $this->classCategoryRepository->find($id);
  145.         if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
  146.             $this->deleteMessage();
  147.             return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  148.         }
  149.         try {
  150.             $this->classCategoryRepository->delete($TargetClassCategory);
  151.             $event = new EventArgs(
  152.                 [
  153.                     'ClassName' => $ClassName,
  154.                     'TargetClassCategory' => $TargetClassCategory,
  155.                 ],
  156.                 $request
  157.             );
  158.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE);
  159.             $this->addSuccess('admin.common.delete_complete''admin');
  160.             log_info('規格分類削除完了', [$id]);
  161.         } catch (\Exception $e) {
  162.             log_error('規格分類削除エラー', [$id$e]);
  163.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetClassCategory->getName()]);
  164.             $this->addError($message'admin');
  165.         }
  166.         return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  167.     }
  168.     /**
  169.      * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/visibility", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_visibility", methods={"PUT"})
  170.      */
  171.     public function visibility(Request $request$class_name_id$id)
  172.     {
  173.         $this->isTokenValid();
  174.         $ClassName $this->classNameRepository->find($class_name_id);
  175.         if (!$ClassName) {
  176.             throw new NotFoundHttpException();
  177.         }
  178.         log_info('規格分類表示変更開始', [$id]);
  179.         $TargetClassCategory $this->classCategoryRepository->find($id);
  180.         if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
  181.             $this->deleteMessage();
  182.             return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  183.         }
  184.         $this->classCategoryRepository->toggleVisibility($TargetClassCategory);
  185.         log_info('規格分類表示変更完了', [$id]);
  186.         $event = new EventArgs(
  187.             [
  188.                 'ClassName' => $ClassName,
  189.                 'TargetClassCategory' => $TargetClassCategory,
  190.             ],
  191.             $request
  192.         );
  193.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE);
  194.         if ($TargetClassCategory->isVisible()) {
  195.             $this->addSuccess(trans('admin.common.to_show_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
  196.         } else {
  197.             $this->addSuccess(trans('admin.common.to_hide_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
  198.         }
  199.         return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
  200.     }
  201.     /**
  202.      * @Route("/%eccube_admin_route%/product/class_category/sort_no/move", name="admin_product_class_category_sort_no_move", methods={"POST"})
  203.      */
  204.     public function moveSortNo(Request $request)
  205.     {
  206.         if (!$request->isXmlHttpRequest()) {
  207.             throw new BadRequestHttpException();
  208.         }
  209.         if ($this->isTokenValid()) {
  210.             $sortNos $request->request->all();
  211.             foreach ($sortNos as $categoryId => $sortNo) {
  212.                 $ClassCategory $this->classCategoryRepository
  213.                     ->find($categoryId);
  214.                 $ClassCategory->setSortNo($sortNo);
  215.                 $this->entityManager->persist($ClassCategory);
  216.             }
  217.             $this->entityManager->flush();
  218.             return new Response('Successful');
  219.         }
  220.     }
  221. }