app/Plugin/NZCategoryLayoutManager42/Controller/Admin/CategoryLayoutController.php line 89

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCategoryLayoutManager42\Controller\Admin;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Repository\CategoryRepository;
  5. use Plugin\NZCategoryLayoutManager42\Entity\CategoryLayout;
  6. use Plugin\NZCategoryLayoutManager42\Entity\CategoryLayoutItem;
  7. use Plugin\NZCategoryLayoutManager42\Repository\CategoryLayoutRepository;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. class CategoryLayoutController extends AbstractController
  13. {
  14.     private $categoryLayoutRepository;
  15.     private $categoryRepository;
  16.     protected $entityManager;
  17.     private $parameterBag;
  18.     public function __construct(
  19.         CategoryLayoutRepository $categoryLayoutRepository,
  20.         CategoryRepository $categoryRepository,
  21.         EntityManagerInterface $entityManager,
  22.         ParameterBagInterface $parameterBag
  23.     ) {
  24.         $this->categoryLayoutRepository $categoryLayoutRepository;
  25.         $this->categoryRepository $categoryRepository;
  26.         $this->entityManager $entityManager;
  27.         $this->parameterBag $parameterBag;
  28.     }
  29.     /**
  30.      * @Route("/%eccube_admin_route%/nz_category_layout", name="nz_category_layout_admin_list")
  31.      */
  32.     public function index(Request $request)
  33.     {
  34.         return $this->render('@NZCategoryLayoutManager42/admin/index.twig', [
  35.             'layouts' => $this->categoryLayoutRepository->findBy([], ['sort_order' => 'ASC'])
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/%eccube_admin_route%/nz_category_layout/new", name="nz_category_layout_admin_new")
  40.      * @Route("/%eccube_admin_route%/nz_category_layout/{id}/edit", name="nz_category_layout_admin_edit", requirements={"id" = "\d+"})
  41.      */
  42.     public function edit(Request $request$id null)
  43.     {
  44.         $layout $id $this->categoryLayoutRepository->find($id) : new CategoryLayout();
  45.         if ($id && !$layout) throw $this->createNotFoundException();
  46.         if ($request->isMethod('POST')) {
  47.             $this->isTokenValid();
  48.             
  49.             $layout->setParentCategoryId($request->request->get('parent_category_id'))
  50.                 ->setLayoutName($request->request->get('layout_name'))
  51.                 ->setIsActive($request->request->get('is_active') == '1')
  52.                 ->setSortOrder($request->request->get('sort_order'0))
  53.                 ->setUpdatedAt(new \DateTime());
  54.             foreach ($layout->getCategoryLayoutItems() as $item) {
  55.                 $this->entityManager->remove($item);
  56.             }
  57.             $this->entityManager->flush();
  58.             foreach ($request->request->get('child_categories', []) as $index => $childData) {
  59.                 if (empty($childData['child_category_id'])) continue;
  60.                 
  61.                 $item = new CategoryLayoutItem();
  62.                 $item->setCategoryLayout($layout)
  63.                     ->setChildCategoryId($childData['child_category_id'])
  64.                     ->setDisplayName($childData['display_name'])
  65.                     ->setIconPath($childData['icon_path'] ?? null)
  66.                     ->setSortOrder($index);
  67.                 
  68.                 $layout->addCategoryLayoutItem($item);
  69.             }
  70.             $this->entityManager->persist($layout);
  71.             $this->entityManager->flush();
  72.             $this->addSuccess('保存しました。''admin');
  73.             
  74.             return $this->redirectToRoute('nz_category_layout_admin_list');
  75.         }
  76.         return $this->render('@NZCategoryLayoutManager42/admin/edit.twig', [
  77.             'layout' => $layout,
  78.             'categories' => $this->categoryRepository->getList()
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/%eccube_admin_route%/nz_category_layout/{id}/delete", name="nz_category_layout_admin_delete", requirements={"id" = "\d+"}, methods={"DELETE"})
  83.      */
  84.     public function delete(Request $request$id)
  85.     {
  86.         $this->isTokenValid();
  87.         $layout $this->categoryLayoutRepository->find($id);
  88.         if (!$layout) throw $this->createNotFoundException();
  89.         $this->entityManager->remove($layout);
  90.         $this->entityManager->flush();
  91.         $this->addSuccess('削除しました。''admin');
  92.         return $this->redirectToRoute('nz_category_layout_admin_list');
  93.     }
  94.     /**
  95.      * @Route("/%eccube_admin_route%/nz_category_layout/upload_icon", name="nz_category_layout_admin_upload_icon", methods={"POST"})
  96.      */
  97.     public function uploadIcon(Request $request)
  98.     {
  99.         $this->isTokenValid();
  100.         
  101.         $file $request->files->get('icon');
  102.         if (!$file) return $this->json(['success' => false'message' => 'ファイルが選択されていません']);
  103.         $allowedMimeTypes = ['image/jpeg''image/png''image/gif''image/webp'];
  104.         if (!in_array($file->getMimeType(), $allowedMimeTypes)) {
  105.             return $this->json(['success' => false'message' => '画像ファイルのみアップロード可能です']);
  106.         }
  107.         $uploadDir $this->parameterBag->get('eccube_html_dir') . '/user_data/game_icons';
  108.         if (!file_exists($uploadDir)) mkdir($uploadDir0755true);
  109.         $fileName uniqid() . '.' $file->guessExtension();
  110.         $file->move($uploadDir$fileName);
  111.         return $this->json(['success' => true'icon_path' => '/html/user_data/game_icons/' $fileName]);
  112.     }
  113. }