<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\TasteManager\Controller;
use Eccube\Controller\AbstractController;
use Plugin\TasteManager\Entity\Taste;
use Plugin\TasteManager\Form\Type\TasteType;
use Plugin\TasteManager\Repository\TasteRepository;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class Controller.
*/
class TasteController extends AbstractController
{
/**
* @var Repository
*/
protected $tasteRepository;
/**
* Controller constructor.
*
*/
public function __construct(TasteRepository $tasteRepository)
{
$this->tasteRepository = $tasteRepository;
}
/**
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Doctrine\ORM\OptimisticLockException
*
* @Route("/%eccube_admin_route%/taste", name="taste_admin_index")
* @Template("@TasteManager/admin/taste.twig")
*/
public function index(Request $request)
{
$Taste = new Taste();
$Tastes = $this->tasteRepository->findBy([], ['sort_no' => 'DESC']);
/**
* 新規登録フォーム
*/
$builder = $this->formFactory->createBuilder(TasteType::class, $Taste);
$form = $builder->getForm();
/**
* 編集用フォーム
*/
$forms = [];
foreach ($Tastes as $item) {
$id = $item->getId();
$forms[$id] = $this->formFactory->createNamed('taste_'.$id, TasteType::class, $item);
}
if ('POST' === $request->getMethod()) {
/*
* 登録処理
*/
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->tasteRepository->save($form->getData());
$this->addSuccess('taste.admin.save.complete', 'admin');
return $this->redirectToRoute('taste_admin_index');
}
/*
* 編集処理
*/
foreach ($forms as $editForm) {
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->tasteRepository->save($editForm->getData());
$this->addSuccess('taste.admin.save.complete', 'admin');
return $this->redirectToRoute('taste_admin_index');
}
}
}
$formViews = [];
foreach ($forms as $key => $value) {
$formViews[$key] = $value->createView();
}
return [
'form' => $form->createView(),
'Tastes' => $Tastes,
'Taste' => $Taste,
'forms' => $formViews,
];
}
/**
* @Route("/%eccube_admin_route%/taste/{id}/visibility", requirements={"id" = "\d+"}, name="admin_taste_visibility", methods={"PUT"})
*/
public function visibility(Request $request, Taste $Taste)
{
$this->isTokenValid();
// 表示・非表示を切り替える
if ($Taste->isVisible()) {
$message = trans('admin.common.to_hide_complete', ['%name%' => $Taste->getName()]);
$Taste->setVisible(false);
} else {
$message = trans('admin.common.to_show_complete', ['%name%' => $Taste->getName()]);
$Taste->setVisible(true);
}
$this->entityManager->persist($Taste);
$this->entityManager->flush();
/*
$event = new EventArgs(
[
'Taste' => $Taste,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_VISIBILITY_COMPLETE, $event);
*/
$this->addSuccess($message, 'admin');
return $this->redirectToRoute('taste_admin_index');
}
/**
* Delete Taste.
*
* @param Request $request
* @param Taste $Taste
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*
* @Route(
* "/%eccube_admin_route%/taste/{id}/delete",
* name="taste_admin_delete", requirements={"id":"\d+"},
* methods={"DELETE"}
* )
*/
public function delete(Request $request, Taste $Taste)
{
$this->isTokenValid();
try {
$this->tasteRepository->delete($Taste);
$this->addSuccess('taste.admin.delete.complete', 'admin');
log_info('メーカー削除完了', ['Taste id' => $Taste->getId()]);
} catch (\Exception $e) {
log_info('メーカー削除エラー', ['Taste id' => $Taste->getId(), $e]);
$message = trans('admin.delete.failed.foreign_key', ['%name%' => $Taste->getName()]);
$this->addError($message, 'admin');
}
return $this->redirectToRoute('taste_admin_index');
}
/**
* Move sort no with ajax.
*
* @param Request $request
*
* @return Response
*
* @throws \Exception
*
* @Route(
* "/%eccube_admin_route%/taste/move_sort_no",
* name="taste_admin_move_sort_no",
* methods={"POST"}
* )
*/
public function moveSortNo(Request $request)
{
if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
$sortNos = $request->request->all();
foreach ($sortNos as $tasteId => $sortNo) {
$Taste = $this->tasteRepository->find($tasteId);
$Taste->setSortNo($sortNo);
$this->entityManager->persist($Taste);
}
$this->entityManager->flush();
}
return new Response();
}
/**
* @Method("GET")
* @Route("/taste", name="taste_index")
* @Template("Taste/index.twig")
*/
public function front_index()
{
// ----------------- //
// 取得
// ----------------- //
$Tastes = $this->tasteRepository->getList();
// ----------------- //
// ページ設定
// ----------------- //
$Page = [
'url' => 'taste',
'meta_tags' => array(),
'edit_type' => 0,
'description' => '',
'author' => '',
'keyword' => '',
'meta_robots' => '',
];
return ['Page'=>$Page, 'Tastes'=>$Tastes];
//return new Response('Hello sample page !');
}
/**
* @Method("GET")
* @Route("/taste/view/{id}", name="taste_view")
* @Template("Taste/view.twig")
*/
public function front_view($id)
{
// ----------------- //
// 取得
// ----------------- //
$Taste = $this->tasteRepository->get($id);
$Products = [];
// ----------------- //
// ページ設定
// ----------------- //
$Page = [
'url' => 'taste',
'meta_tags' => array(),
'edit_type' => 0,
'description' => '',
'author' => '',
'keyword' => '',
'meta_robots' => '',
];
return ['Page'=>$Page, 'Taste'=>$Taste, 'Products'=>$Products,];
//return new Response('Hello sample page !');
}
}