<?php
namespace Customize\Controller;
use Customize\Controller\CartController as CustomizeCartController;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Entity\Product;
use Eccube\Form\Type\AddCartType;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Eccube\Repository\Master\ProductListMaxRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Service\CartService;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Customize\Repository\ProductRepository as CustomizeProductRepository;
class ProductController extends \Eccube\Controller\ProductController
{
/**
* 商品詳細画面.
*
* @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
* @Template("Product/detail.twig")
* @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
*
* @param Request $request
* @param Product $Product
*
* @return array
*/
private $title = '';
private CustomizeProductRepository $customizeProdRepo;
private CustomizeCartController $customizeCartController;
public function __construct(PurchaseFlow $cartPurchaseFlow, CustomerFavoriteProductRepository $customerFavoriteProductRepository, CartService $cartService, ProductRepository $productRepository, BaseInfoRepository $baseInfoRepository, AuthenticationUtils $helper, ProductListMaxRepository $productListMaxRepository, CustomizeProductRepository $customizeProdRepo, CustomizeCartController $customizeCartController)
{
parent::__construct($cartPurchaseFlow, $customerFavoriteProductRepository, $cartService, $productRepository, $baseInfoRepository, $helper, $productListMaxRepository);
$this->customizeProdRepo = $customizeProdRepo;
$this->customizeCartController = $customizeCartController;
}
/**
* 商品詳細画面.
*
* @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
* @Template("Product/detail.twig")
* @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
*
* @param Request $request
* @param Product $Product
*
* @return array | \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function detail(Request $request, Product $Product)
{
if (!$this->checkVisibility($Product)) {
throw new NotFoundHttpException();
}
$builder = $this->formFactory->createNamedBuilder(
'',
AddCartType::class,
null,
[
'product' => $Product,
'id_add_product_id' => false,
]
);
$event = new EventArgs(
[
'builder' => $builder,
'Product' => $Product,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE);
$is_favorite = false;
if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
$is_favorite = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product);
}
$isDirectCart = $request->get('direct_cart') == $this->customizeProdRepo->getValIsDirectCart();
$available = $Product->getStockFind() && $Product->getStatus()->getId() == ProductStatus::DISPLAY_SHOW;
if ($isDirectCart && $available) {
$flag = $this->customizeProdRepo->isDirectCart($Product, $this->cartService, $this->customizeCartController);
if ($flag) {
return $this->redirectToRoute('cart');
}
}
return [
'title' => $this->title,
'subtitle' => $Product->getName(),
'form' => $builder->getForm()->createView(),
'Product' => $Product,
'is_favorite' => $is_favorite,
];
}
}