<?php
namespace Plugin\NZCategoryLayoutManager42\Controller\Admin;
use Eccube\Controller\AbstractController;
use Eccube\Repository\CategoryRepository;
use Plugin\NZCategoryLayoutManager42\Entity\CategoryLayout;
use Plugin\NZCategoryLayoutManager42\Entity\CategoryLayoutItem;
use Plugin\NZCategoryLayoutManager42\Repository\CategoryLayoutRepository;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
class CategoryLayoutController extends AbstractController
{
private $categoryLayoutRepository;
private $categoryRepository;
protected $entityManager;
private $parameterBag;
public function __construct(
CategoryLayoutRepository $categoryLayoutRepository,
CategoryRepository $categoryRepository,
EntityManagerInterface $entityManager,
ParameterBagInterface $parameterBag
) {
$this->categoryLayoutRepository = $categoryLayoutRepository;
$this->categoryRepository = $categoryRepository;
$this->entityManager = $entityManager;
$this->parameterBag = $parameterBag;
}
/**
* @Route("/%eccube_admin_route%/nz_category_layout", name="nz_category_layout_admin_list")
*/
public function index(Request $request)
{
return $this->render('@NZCategoryLayoutManager42/admin/index.twig', [
'layouts' => $this->categoryLayoutRepository->findBy([], ['sort_order' => 'ASC'])
]);
}
/**
* @Route("/%eccube_admin_route%/nz_category_layout/new", name="nz_category_layout_admin_new")
* @Route("/%eccube_admin_route%/nz_category_layout/{id}/edit", name="nz_category_layout_admin_edit", requirements={"id" = "\d+"})
*/
public function edit(Request $request, $id = null)
{
$layout = $id ? $this->categoryLayoutRepository->find($id) : new CategoryLayout();
if ($id && !$layout) throw $this->createNotFoundException();
if ($request->isMethod('POST')) {
$this->isTokenValid();
$layout->setParentCategoryId($request->request->get('parent_category_id'))
->setLayoutName($request->request->get('layout_name'))
->setIsActive($request->request->get('is_active') == '1')
->setSortOrder($request->request->get('sort_order', 0))
->setUpdatedAt(new \DateTime());
foreach ($layout->getCategoryLayoutItems() as $item) {
$this->entityManager->remove($item);
}
$this->entityManager->flush();
foreach ($request->request->get('child_categories', []) as $index => $childData) {
if (empty($childData['child_category_id'])) continue;
$item = new CategoryLayoutItem();
$item->setCategoryLayout($layout)
->setChildCategoryId($childData['child_category_id'])
->setDisplayName($childData['display_name'])
->setIconPath($childData['icon_path'] ?? null)
->setSortOrder($index);
$layout->addCategoryLayoutItem($item);
}
$this->entityManager->persist($layout);
$this->entityManager->flush();
$this->addSuccess('保存しました。', 'admin');
return $this->redirectToRoute('nz_category_layout_admin_list');
}
return $this->render('@NZCategoryLayoutManager42/admin/edit.twig', [
'layout' => $layout,
'categories' => $this->categoryRepository->getList()
]);
}
/**
* @Route("/%eccube_admin_route%/nz_category_layout/{id}/delete", name="nz_category_layout_admin_delete", requirements={"id" = "\d+"}, methods={"DELETE"})
*/
public function delete(Request $request, $id)
{
$this->isTokenValid();
$layout = $this->categoryLayoutRepository->find($id);
if (!$layout) throw $this->createNotFoundException();
$this->entityManager->remove($layout);
$this->entityManager->flush();
$this->addSuccess('削除しました。', 'admin');
return $this->redirectToRoute('nz_category_layout_admin_list');
}
/**
* @Route("/%eccube_admin_route%/nz_category_layout/upload_icon", name="nz_category_layout_admin_upload_icon", methods={"POST"})
*/
public function uploadIcon(Request $request)
{
$this->isTokenValid();
$file = $request->files->get('icon');
if (!$file) return $this->json(['success' => false, 'message' => 'ファイルが選択されていません']);
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
if (!in_array($file->getMimeType(), $allowedMimeTypes)) {
return $this->json(['success' => false, 'message' => '画像ファイルのみアップロード可能です']);
}
$uploadDir = $this->parameterBag->get('eccube_html_dir') . '/user_data/game_icons';
if (!file_exists($uploadDir)) mkdir($uploadDir, 0755, true);
$fileName = uniqid() . '.' . $file->guessExtension();
$file->move($uploadDir, $fileName);
return $this->json(['success' => true, 'icon_path' => '/html/user_data/game_icons/' . $fileName]);
}
}