<?php
namespace Plugin\kkNewsCategory\Controller\Admin;
use Eccube\Controller\AbstractController;
use Eccube\Event\EventArgs;
use Eccube\Util\CacheUtil;
use Plugin\kkNewsCategory\Entity\NewsCategory;
use Plugin\kkNewsCategory\Form\Type\Admin\NewsCategoryType;
use Plugin\kkNewsCategory\Repository\NewsCategoryRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class NewsCategoryController extends AbstractController
{
/**
* @var NewsCategoryRepository
*/
protected $newsCategoryRepository;
/**
* NewsCategoryController constructor.
*
* @param NewsCategoryRepository $newsCategoryRepository
*/
public function __construct(
NewsCategoryRepository $newsCategoryRepository
) {
$this->newsCategoryRepository = $newsCategoryRepository;
}
/**
* @Route("/%eccube_admin_route%/kk_news_category_plugin/index", name="kk_news_category_plugin_admin_index")
* @Route("/%eccube_admin_route%/kk_news_category_plugin/{id}/edit", requirements={"id" = "\d+"}, name="kk_news_category_plugin_admin_edit")
* @Template("@kkNewsCategory/admin/index.twig")
*/
public function index(Request $request, $id = null, CacheUtil $cacheUtil)
{
if ($id) {
$TargetCategory = $this->newsCategoryRepository->find($id);
if (!$TargetCategory) {
throw new NotFoundHttpException();
}
} else {
$TargetCategory = new NewsCategory();
}
$Categories = $this->newsCategoryRepository->getList();
$builder = $this->formFactory
->createBuilder(NewsCategoryType::class, $TargetCategory);
$event = new EventArgs(
[
'builder' => $builder,
'TargetCategory' => $TargetCategory,
],
$request
);
//$this->eventDispatcher->dispatch( $event,'admin.content.category.index.initialize'));
$this->eventDispatcher->dispatch( $event,'admin.content.category.index.initialize');
// var_dump($builder);
$form = $builder->getForm();
$forms = [];
foreach ($Categories as $Category) {
$forms[$Category->getId()] = $this->formFactory
->createNamed('category_'.$Category->getId(), NewsCategoryType::class, $Category);
}
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
log_info('カテゴリ登録開始', [$id]);
$this->newsCategoryRepository->save($TargetCategory);
log_info('カテゴリ登録完了', [$id]);
// $formが保存されたフォーム
// 下の編集用フォームの場合とイベント名が共通のため
// このイベントのリスナーではsubmitされているフォームを判定する必要がある
$event = new EventArgs(
[
'form' => $form,
'TargetCategory' => $TargetCategory,
],
$request
);
//$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete'));
$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete');
$this->addSuccess('admin.common.save_complete', 'admin');
$cacheUtil->clearDoctrineCache();
return $this->redirectToRoute('kk_news_category_plugin_admin_index');
}
foreach ($forms as $editForm) {
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->newsCategoryRepository->save($editForm->getData());
// $editFormが保存されたフォーム
// 上の新規登録用フォームの場合とイベント名が共通のため
// このイベントのリスナーではsubmitされているフォームを判定する必要がある
$event = new EventArgs(
[
'form' => $form,
'editForm' => $editForm,
'TargetCategory' => $editForm->getData(),
],
$request
);
//$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete'));
$this->eventDispatcher->dispatch( $event,'admin.content.category.index.complete');
$this->addSuccess('admin.common.save_complete', 'admin');
$cacheUtil->clearDoctrineCache();
return $this->redirectToRoute('kk_news_category_plugin_admin_index');
}
}
}
$formViews = [];
foreach ($forms as $key => $value) {
$formViews[$key] = $value->createView();
}
return [
'form' => $form->createView(),
'Categories' => $Categories,
'TargetCategory' => $TargetCategory,
'forms' => $formViews,
];
}
/**
* @Route("/%eccube_admin_route%/kk_news_category_plugin/{id}/delete", requirements={"id" = "\d+"}, name="kk_news_category_plugin_admin_delete", methods={"DELETE"})
*/
public function delete(Request $request, $id, CacheUtil $cacheUtil)
{
$this->isTokenValid();
$TargetCategory = $this->newsCategoryRepository->find($id);
if (!$TargetCategory) {
$this->deleteMessage();
return $this->redirectToRoute('kk_news_category_plugin_admin_index');
}
log_info('カテゴリ削除開始', [$id]);
try {
$this->newsCategoryRepository->delete($TargetCategory);
$event = new EventArgs(
[
'TargetCategory' => $TargetCategory,
], $request
);
//$this->eventDispatcher->dispatch( $event,'admin.news.category.delete.complete'));
$this->eventDispatcher->dispatch( $event,'admin.news.category.delete.complete');
$this->addSuccess('admin.common.delete_complete', 'admin');
log_info('カテゴリ削除完了', [$id]);
$cacheUtil->clearDoctrineCache();
} catch (\Exception $e) {
log_info('カテゴリ削除エラー', [$id, $e]);
$message = trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetCategory->getName()]);
$this->addError($message, 'admin');
}
return $this->redirectToRoute('kk_news_category_plugin_admin_index');
}
/**
* @Route("/%eccube_admin_route%/kk_news_category_plugin/sort_no/move", name="kk_news_category_plugin_admin_sort_no_move", methods={"POST"})
*/
public function moveSortNo(Request $request, CacheUtil $cacheUtil)
{
if (!$request->isXmlHttpRequest()) {
throw new BadRequestHttpException();
}
if ($this->isTokenValid()) {
$sortNos = $request->request->all();
foreach ($sortNos as $newsCategoryId => $sortNo) {
/* @var $Category \Eccube\Entity\Category */
$newsCategory = $this->newsCategoryRepository
->find($newsCategoryId);
$newsCategory->setSortNo($sortNo);
$this->entityManager->persist($newsCategory);
}
$this->entityManager->flush();
$cacheUtil->clearDoctrineCache();
return new Response('Successful');
}
}
}