app/Plugin/kkNewsCategory/Controller/Admin/NewsCategoryController.php line 39

Open in your IDE?
  1. <?php
  2. namespace Plugin\kkNewsCategory\Controller\Admin;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Event\EventArgs;
  5. use Eccube\Util\CacheUtil;
  6. use Plugin\kkNewsCategory\Entity\NewsCategory;
  7. use Plugin\kkNewsCategory\Form\Type\Admin\NewsCategoryType;
  8. use Plugin\kkNewsCategory\Repository\NewsCategoryRepository;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class NewsCategoryController extends AbstractController
  14. {
  15.     /**
  16.      * @var NewsCategoryRepository
  17.      */
  18.     protected $newsCategoryRepository;
  19.     /**
  20.      * NewsCategoryController constructor.
  21.      *
  22.      * @param NewsCategoryRepository $newsCategoryRepository
  23.      */
  24.     public function __construct(
  25.         NewsCategoryRepository $newsCategoryRepository
  26.     ) {
  27.         $this->newsCategoryRepository $newsCategoryRepository;
  28.     }
  29.     /**
  30.      * @Route("/%eccube_admin_route%/kk_news_category_plugin/index", name="kk_news_category_plugin_admin_index")
  31.      * @Route("/%eccube_admin_route%/kk_news_category_plugin/{id}/edit", requirements={"id" = "\d+"}, name="kk_news_category_plugin_admin_edit")
  32.      * @Template("@kkNewsCategory/admin/index.twig")
  33.      */
  34.     public function index(Request $request$id nullCacheUtil $cacheUtil)
  35.     {
  36.         if ($id) {
  37.             $TargetCategory $this->newsCategoryRepository->find($id);
  38.             if (!$TargetCategory) {
  39.                 throw new NotFoundHttpException();
  40.             }
  41.         } else {
  42.             $TargetCategory = new NewsCategory();
  43.         }
  44.         $Categories $this->newsCategoryRepository->getList();
  45.         $builder $this->formFactory
  46.             ->createBuilder(NewsCategoryType::class, $TargetCategory);
  47.         $event = new EventArgs(
  48.             [
  49.                 'builder' => $builder,
  50.                 'TargetCategory' => $TargetCategory,
  51.             ],
  52.             $request
  53.         );
  54.         //$this->eventDispatcher->dispatch( $event,'admin.content.category.index.initialize'));
  55.         $this->eventDispatcher->dispatch$event,'admin.content.category.index.initialize');
  56.         // var_dump($builder);
  57.         $form $builder->getForm();
  58.         $forms = [];
  59.         foreach ($Categories as $Category) {
  60.             $forms[$Category->getId()] = $this->formFactory
  61.                 ->createNamed('category_'.$Category->getId(), NewsCategoryType::class, $Category);
  62.         }
  63.         if ($request->getMethod() === 'POST') {
  64.             $form->handleRequest($request);
  65.             if ($form->isValid()) {
  66.                 log_info('カテゴリ登録開始', [$id]);
  67.                 $this->newsCategoryRepository->save($TargetCategory);
  68.                 log_info('カテゴリ登録完了', [$id]);
  69.                 // $formが保存されたフォーム
  70.                 // 下の編集用フォームの場合とイベント名が共通のため
  71.                 // このイベントのリスナーではsubmitされているフォームを判定する必要がある
  72.                 $event = new EventArgs(
  73.                     [
  74.                         'form' => $form,
  75.                         'TargetCategory' => $TargetCategory,
  76.                     ],
  77.                     $request
  78.                 );
  79.                 //$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete'));
  80.         $this->eventDispatcher->dispatch$event,'admin.content.category.index.complete');
  81.                 $this->addSuccess('admin.common.save_complete''admin');
  82.                 $cacheUtil->clearDoctrineCache();
  83.                 return $this->redirectToRoute('kk_news_category_plugin_admin_index');
  84.             }
  85.             foreach ($forms as $editForm) {
  86.                 $editForm->handleRequest($request);
  87.                 if ($editForm->isSubmitted() && $editForm->isValid()) {
  88.                     $this->newsCategoryRepository->save($editForm->getData());
  89.                     // $editFormが保存されたフォーム
  90.                     // 上の新規登録用フォームの場合とイベント名が共通のため
  91.                     // このイベントのリスナーではsubmitされているフォームを判定する必要がある
  92.                     $event = new EventArgs(
  93.                         [
  94.                             'form' => $form,
  95.                             'editForm' => $editForm,
  96.                             'TargetCategory' => $editForm->getData(),
  97.                         ],
  98.                         $request
  99.                     );
  100.                     //$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete'));
  101.                     $this->eventDispatcher->dispatch$event,'admin.content.category.index.complete');
  102.                     $this->addSuccess('admin.common.save_complete''admin');
  103.                     $cacheUtil->clearDoctrineCache();
  104.                     return $this->redirectToRoute('kk_news_category_plugin_admin_index');
  105.                 }
  106.             }
  107.         }
  108.         $formViews = [];
  109.         foreach ($forms as $key => $value) {
  110.             $formViews[$key] = $value->createView();
  111.         }
  112.         return [
  113.             'form' => $form->createView(),
  114.             'Categories' => $Categories,
  115.             'TargetCategory' => $TargetCategory,
  116.             'forms' => $formViews,
  117.         ];
  118.     }
  119.     /**
  120.      * @Route("/%eccube_admin_route%/kk_news_category_plugin/{id}/delete", requirements={"id" = "\d+"}, name="kk_news_category_plugin_admin_delete", methods={"DELETE"})
  121.      */
  122.     public function delete(Request $request$idCacheUtil $cacheUtil)
  123.     {
  124.         $this->isTokenValid();
  125.         $TargetCategory $this->newsCategoryRepository->find($id);
  126.         if (!$TargetCategory) {
  127.             $this->deleteMessage();
  128.             return $this->redirectToRoute('kk_news_category_plugin_admin_index');
  129.         }
  130.         log_info('カテゴリ削除開始', [$id]);
  131.         try {
  132.             $this->newsCategoryRepository->delete($TargetCategory);
  133.             $event = new EventArgs(
  134.                 [
  135.                     'TargetCategory' => $TargetCategory,
  136.                 ], $request
  137.             );
  138.             //$this->eventDispatcher->dispatch( $event,'admin.news.category.delete.complete'));
  139.             $this->eventDispatcher->dispatch$event,'admin.news.category.delete.complete');
  140.             $this->addSuccess('admin.common.delete_complete''admin');
  141.             log_info('カテゴリ削除完了', [$id]);
  142.             $cacheUtil->clearDoctrineCache();
  143.         } catch (\Exception $e) {
  144.             log_info('カテゴリ削除エラー', [$id$e]);
  145.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetCategory->getName()]);
  146.             $this->addError($message'admin');
  147.         }
  148.         return $this->redirectToRoute('kk_news_category_plugin_admin_index');
  149.     }
  150.     /**
  151.      * @Route("/%eccube_admin_route%/kk_news_category_plugin/sort_no/move", name="kk_news_category_plugin_admin_sort_no_move", methods={"POST"})
  152.      */
  153.     public function moveSortNo(Request $requestCacheUtil $cacheUtil)
  154.     {
  155.         if (!$request->isXmlHttpRequest()) {
  156.             throw new BadRequestHttpException();
  157.         }
  158.         if ($this->isTokenValid()) {
  159.             $sortNos $request->request->all();
  160.             foreach ($sortNos as $newsCategoryId => $sortNo) {
  161.                 /* @var $Category \Eccube\Entity\Category */
  162.                 $newsCategory $this->newsCategoryRepository
  163.                     ->find($newsCategoryId);
  164.                 $newsCategory->setSortNo($sortNo);
  165.                 $this->entityManager->persist($newsCategory);
  166.             }
  167.             $this->entityManager->flush();
  168.             $cacheUtil->clearDoctrineCache();
  169.             return new Response('Successful');
  170.         }
  171.     }
  172. }