app/Plugin/BrandManager/Controller/BrandController.php line 263

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 Plugin\BrandManager\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Plugin\BrandManager\Entity\Brand;
  15. use Plugin\BrandManager\Form\Type\BrandType;
  16. use Plugin\BrandManager\Repository\BrandRepository;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Eccube\Repository\TagRepository;
  25. /**
  26.  * Class Controller.
  27.  */
  28. class BrandController extends AbstractController
  29. {
  30.     /**
  31.      * @var Repository
  32.      */
  33.     protected $brandRepository;
  34.     /**
  35.      * @var TagRepository
  36.      */
  37.     private $tagRepository;
  38.     
  39.     /**
  40.      * Controller constructor.
  41.      *
  42.      */
  43.     public function __construct(BrandRepository $brandRepositoryTagRepository $tagRepository)
  44.     {
  45.         $this->brandRepository $brandRepository;
  46.         $this->tagRepository $tagRepository;
  47.     }
  48.     /**
  49.      * @param Request $request
  50.      *
  51.      * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  52.      *
  53.      * @throws \Doctrine\ORM\NoResultException
  54.      * @throws \Doctrine\ORM\NonUniqueResultException
  55.      * @throws \Doctrine\ORM\OptimisticLockException
  56.      *
  57.      * @Route("/%eccube_admin_route%/brand", name="brand_admin_index")
  58.      * @Template("@BrandManager/admin/brand.twig")
  59.      */
  60.     public function index(Request $request)
  61.     {
  62.         $Brand = new Brand();
  63.         $Brands $this->brandRepository->findBy([], ['sort_no' => 'DESC']);
  64.         /**
  65.          * 新規登録フォーム
  66.          */
  67.         $builder $this->formFactory->createBuilder(BrandType::class, $Brand);
  68.         $form $builder->getForm();
  69.         /**
  70.          * 編集用フォーム
  71.          */
  72.         $forms = [];
  73.         foreach ($Brands as $item) {
  74.             $id $item->getId();
  75.             $forms[$id] = $this->formFactory->createNamed('brand_'.$idBrandType::class, $item);
  76.         }
  77.           
  78.         if ('POST' === $request->getMethod()) {
  79.           
  80.           
  81.             /*
  82.              * 登録処理
  83.              */
  84.             $form->handleRequest($request);
  85.             if ($form->isSubmitted() && $form->isValid()) {
  86.                 $this->brandRepository->save($form->getData());
  87.                 $this->addSuccess('brand.admin.save.complete''admin');
  88.                 return $this->redirectToRoute('brand_admin_index');
  89.             }
  90.             /*
  91.              * 編集処理
  92.              */
  93.             foreach ($forms as $editForm) {
  94.                 $editForm->handleRequest($request);
  95.                 if ($editForm->isSubmitted() && $editForm->isValid()) {
  96.                     $this->brandRepository->save($editForm->getData());
  97.                     $this->addSuccess('brand.admin.save.complete''admin');
  98.                     return $this->redirectToRoute('brand_admin_index');
  99.                 }
  100.             }
  101.         }
  102.         $formViews = [];
  103.         foreach ($forms as $key => $value) {
  104.             $formViews[$key] = $value->createView();
  105.         }
  106.         return [
  107.             'form' => $form->createView(),
  108.             'Brands' => $Brands,
  109.             'Brand' => $Brand,
  110.             'forms' => $formViews,
  111.         ];
  112.     }
  113.     
  114.     /**
  115.      * @Route("/%eccube_admin_route%/brand/{id}/visibility", requirements={"id" = "\d+"}, name="admin_brand_visibility", methods={"PUT"})
  116.      */
  117.     public function visibility(Request $requestBrand $Brand)
  118.     {
  119.         $this->isTokenValid();
  120.         // 表示・非表示を切り替える
  121.         if ($Brand->isVisible()) {
  122.             $message trans('admin.common.to_hide_complete', ['%name%' => $Brand->getName()]);
  123.             $Brand->setVisible(false);
  124.         } else {
  125.             $message trans('admin.common.to_show_complete', ['%name%' => $Brand->getName()]);
  126.             $Brand->setVisible(true);
  127.         }
  128.         $this->entityManager->persist($Brand);
  129.         $this->entityManager->flush();
  130.         
  131.         /*
  132.         $event = new EventArgs(
  133.             [
  134.                 'Brand' => $Brand,
  135.             ],
  136.             $request
  137.         );
  138.         $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_VISIBILITY_COMPLETE, $event);
  139.         */
  140.         $this->addSuccess($message'admin');
  141.         
  142.         return $this->redirectToRoute('brand_admin_index');
  143.     }
  144.     /**
  145.      * Delete Brand.
  146.      *
  147.      * @param Request $request
  148.      * @param Brand $Brand
  149.      *
  150.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  151.      *
  152.      * @Route(
  153.      *     "/%eccube_admin_route%/brand/{id}/delete",
  154.      *     name="brand_admin_delete", requirements={"id":"\d+"},
  155.      *     methods={"DELETE"}
  156.      * )
  157.      */
  158.     public function delete(Request $requestBrand $Brand)
  159.     {
  160.         $this->isTokenValid();
  161.         try {
  162.             $this->brandRepository->delete($Brand);
  163.             $this->addSuccess('brand.admin.delete.complete''admin');
  164.             log_info('メーカー削除完了', ['Brand id' => $Brand->getId()]);
  165.         } catch (\Exception $e) {
  166.             log_info('メーカー削除エラー', ['Brand id' => $Brand->getId(), $e]);
  167.             $message trans('admin.delete.failed.foreign_key', ['%name%' => $Brand->getName()]);
  168.             $this->addError($message'admin');
  169.         }
  170.         return $this->redirectToRoute('brand_admin_index');
  171.     }
  172.     /**
  173.      * Move sort no with ajax.
  174.      *
  175.      * @param Request $request
  176.      *
  177.      * @return Response
  178.      *
  179.      * @throws \Exception
  180.      *
  181.      * @Route(
  182.      *     "/%eccube_admin_route%/brand/move_sort_no",
  183.      *     name="brand_admin_move_sort_no",
  184.      *     methods={"POST"}
  185.      * )
  186.      */
  187.     public function moveSortNo(Request $request)
  188.     {
  189.         if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
  190.             $sortNos $request->request->all();
  191.             foreach ($sortNos as $brandId => $sortNo) {
  192.                 $Brand $this->brandRepository->find($brandId);
  193.                 $Brand->setSortNo($sortNo);
  194.                 $this->entityManager->persist($Brand);
  195.             }
  196.             $this->entityManager->flush();
  197.         }
  198.         return new Response();
  199.     }
  200.     
  201.     /**
  202.      * @Method("GET")
  203.      * @Route("/brand", name="brand_index")
  204.      * @Template("Brand/index.twig")
  205.      */
  206.     public function front_index()
  207.     {
  208.       
  209.       // ----------------- //
  210.       // 取得
  211.       // ----------------- //
  212.       $Brands $this->brandRepository->getList();
  213.       
  214.       // ----------------- //
  215.       // ページ設定
  216.       // ----------------- //
  217.       $Page = [
  218.           'url' => 'brand',
  219.           'meta_tags' => array(),
  220.           'edit_type' => 0,
  221.           'description' => '',
  222.           'author' => '',
  223.           'keyword' => '',
  224.           'meta_robots' => '',
  225.       ];
  226.       
  227.       return ['Page'=>$Page'Brands'=>$Brands];
  228.       //return new Response('Hello sample page !');
  229.     }
  230.     
  231.     /**
  232.      * @Method("GET")
  233.      * @Route("/brand/view/{id}", name="brand_view")
  234.      * @Template("Brand/view.twig")
  235.      */
  236.     public function front_view($id)
  237.     {
  238.       
  239.       // ----------------- //
  240.       // 取得
  241.       // ----------------- //
  242.       $Brand $this->brandRepository->get($id);
  243.       
  244.       $Products = [];
  245.       
  246.       // ----------------- //
  247.       // ページ設定
  248.       // ----------------- //
  249.       $Page = [
  250.           'url' => 'brand',
  251.           'meta_tags' => array(),
  252.           'edit_type' => 0,
  253.           'description' => '',
  254.           'author' => '',
  255.           'keyword' => '',
  256.           'meta_robots' => '',
  257.       ];
  258.       
  259.       return ['Page'=>$Page'Brand'=>$Brand'Products'=>$Products,];
  260.       //return new Response('Hello sample page !');
  261.     }
  262. }