app/Plugin/TasteManager/Controller/TasteController.php line 256

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