app/Customize/Controller/ProductController.php line 103

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller;
  13. use Eccube\Entity\Product;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\AddCartType;
  17. use Eccube\Form\Type\SearchProductType;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\CustomerFavoriteProductRepository;
  20. use Eccube\Repository\Master\ProductListMaxRepository;
  21. use Eccube\Repository\ProductRepository;
  22. use Eccube\Service\CartService;
  23. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  24. use Knp\Component\Pager\PaginatorInterface;
  25. use Plugin\PinpointSaleDx\Repository\ProductPinpointRepository;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  31. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  32. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  33. class ProductController extends \Eccube\Controller\ProductController
  34. {    
  35.         /**
  36.         * @var PurchaseFlow
  37.         */
  38.     protected $purchaseFlow;
  39.     /**
  40.         * @var CustomerFavoriteProductRepository
  41.         */
  42.     protected $customerFavoriteProductRepository;
  43.     /**
  44.         * @var CartService
  45.         */
  46.     protected $cartService;
  47.     /**
  48.         * @var ProductRepository
  49.         */
  50.     protected $productRepository;
  51.     /**
  52.         * @var BaseInfo
  53.         */
  54.     protected $BaseInfo;
  55.     /**
  56.         * @var AuthenticationUtils
  57.         */
  58.     protected $helper;
  59.     /**
  60.         * @var ProductListMaxRepository
  61.         */
  62.     protected $productListMaxRepository;
  63.     private $title '';
  64.     protected ProductPinpointRepository $productPinpointRepository;
  65.     
  66.     public function __construct(
  67.         ProductPinpointRepository $productPinpointRepository,
  68.         PurchaseFlow $cartPurchaseFlow,
  69.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  70.         CartService $cartService,
  71.         ProductRepository $productRepository,
  72.         BaseInfoRepository $baseInfoRepository,
  73.         AuthenticationUtils $helper,
  74.         ProductListMaxRepository $productListMaxRepository
  75.     ) {
  76.         $this->productPinpointRepository $productPinpointRepository;
  77.         $this->purchaseFlow $cartPurchaseFlow;
  78.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  79.         $this->cartService $cartService;
  80.         $this->productRepository $productRepository;
  81.         $this->BaseInfo $baseInfoRepository->get();
  82.         $this->helper $helper;
  83.         $this->productListMaxRepository $productListMaxRepository;
  84.     }
  85.     /**
  86.      * 商品一覧画面.
  87.      *
  88.      * @Route("/products/sale", name="product_sale", methods={"GET"})
  89.      * @Template("Product/sale.twig")
  90.      */
  91.     public function index(Request $requestPaginatorInterface $paginator)
  92.     {
  93.         // Doctrine SQLFilter
  94.         if ($this->BaseInfo->isOptionNostockHidden()) {
  95.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  96.         }
  97.         // handleRequestは空のqueryの場合は無視するため
  98.         if ($request->getMethod() === 'GET') {
  99.             $request->query->set('pageno'$request->query->get('pageno'''));
  100.         }
  101.         // searchForm
  102.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  103.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  104.         if ($request->getMethod() === 'GET') {
  105.             $builder->setMethod('GET');
  106.         }
  107.         $event = new EventArgs(
  108.             [
  109.                 'builder' => $builder,
  110.             ],
  111.             $request
  112.         );
  113.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  114.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  115.         $searchForm $builder->getForm();
  116.         $searchForm->handleRequest($request);
  117.         // paginator
  118.         $searchData $searchForm->getData();
  119.         $qb $this->productPinpointRepository->getQueryBuilderBySearchData($searchData);
  120.         $event = new EventArgs(
  121.             [
  122.                 'searchData' => $searchData,
  123.                 'qb' => $qb,
  124.             ],
  125.             $request
  126.         );
  127.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  128.         $searchData $event->getArgument('searchData');
  129.         $query $qb->getQuery()
  130.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  131.         /** @var SlidingPagination $pagination */
  132.         $pagination222 $paginator->paginate(
  133.             $query,
  134.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  135.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId(),
  136.             [
  137.                 'wrap-queries' => true
  138.             ]
  139.         );
  140.         $ids = [];
  141.         foreach ($pagination222 as $PinpointProduct) {
  142.             $ids[] = $PinpointProduct->getProductClass()->getProduct()->getId();
  143.         }
  144.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  145.         // addCart form
  146.         $forms = [];
  147.         foreach ($pagination222 as $PinpointProduct) {
  148.             $Product $PinpointProduct->getProductClass()->getProduct();
  149.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  150.             $builder $this->formFactory->createNamedBuilder(
  151.                 '',
  152.                 AddCartType::class,
  153.                 null,
  154.                 [
  155.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  156.                     'allow_extra_fields' => true,
  157.                 ]
  158.             );
  159.             $addCartForm $builder->getForm();
  160.             $forms[$Product->getId()] = $addCartForm->createView();
  161.         }
  162.         $Category $searchForm->get('category_id')->getData();
  163.         return [
  164.             'subtitle' => $this->getPageTitle($searchData),
  165.             'pagination' => $pagination222,
  166.             'search_form' => $searchForm->createView(),
  167.             'forms' => $forms,
  168.             'Category' => $Category,
  169.         ];
  170.     }
  171.     /**
  172.      * 商品一覧画面.
  173.      *
  174.      * @Route("/products/new", name="new_product", methods={"GET"})
  175.      * @Template("Product/new.twig")
  176.      */
  177.     public function new(Request $requestPaginatorInterface $paginator)
  178.     {
  179.         // Doctrine SQLFilter
  180.         if ($this->BaseInfo->isOptionNostockHidden()) {
  181.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  182.         }
  183.         // handleRequestは空のqueryの場合は無視するため
  184.         if ($request->getMethod() === 'GET') {
  185.             $request->query->set('pageno'$request->query->get('pageno'''));
  186.         }
  187.         // searchForm
  188.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  189.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  190.         if ($request->getMethod() === 'GET') {
  191.             $builder->setMethod('GET');
  192.         }
  193.         $event = new EventArgs(
  194.             [
  195.                 'builder' => $builder,
  196.             ],
  197.             $request
  198.         );
  199.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  200.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  201.         $searchForm $builder->getForm();
  202.         $searchForm->handleRequest($request);
  203.         // paginator
  204.         $searchData $searchForm->getData();
  205.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  206.         $event = new EventArgs(
  207.             [
  208.                 'searchData' => $searchData,
  209.                 'qb' => $qb,
  210.             ],
  211.             $request
  212.         );
  213.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  214.         $searchData $event->getArgument('searchData');
  215.         $query $qb->getQuery()
  216.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  217.         /** @var SlidingPagination $pagination */
  218.         $pagination $paginator->paginate(
  219.             $query,
  220.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  221.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  222.         );
  223.         $ids = [];
  224.         foreach ($pagination as $Product) {
  225.             $ids[] = $Product->getId();
  226.         }
  227.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  228.         // addCart form
  229.         $forms = [];
  230.         foreach ($pagination as $Product) {
  231.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  232.             $builder $this->formFactory->createNamedBuilder(
  233.                 '',
  234.                 AddCartType::class,
  235.                 null,
  236.                 [
  237.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  238.                     'allow_extra_fields' => true,
  239.                 ]
  240.             );
  241.             $addCartForm $builder->getForm();
  242.             $forms[$Product->getId()] = $addCartForm->createView();
  243.         }
  244.         $Category $searchForm->get('category_id')->getData();
  245.         return [
  246.             'subtitle' => $this->getPageTitle($searchData),
  247.             'pagination' => $pagination,
  248.             'search_form' => $searchForm->createView(),
  249.             'forms' => $forms,
  250.             'Category' => $Category,
  251.         ];
  252.     }
  253.     /**
  254.      * お気に入り追加.
  255.      *
  256.      * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  257.      */
  258.     public function addFavorite(Request $requestProduct $Product)
  259.     {
  260.         $this->checkVisibility($Product);
  261.         $event = new EventArgs(
  262.             [
  263.                 'Product' => $Product,
  264.             ],
  265.             $request
  266.         );
  267.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE);
  268.         if ($this->isGranted('ROLE_USER')) {
  269.             $Customer $this->getUser();
  270.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product);
  271.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  272.             $event = new EventArgs(
  273.                 [
  274.                     'Product' => $Product,
  275.                 ],
  276.                 $request
  277.             );
  278.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  279.             if ($request->isXmlHttpRequest()) {
  280.                 return $this->json([
  281.                     'done' => true
  282.                 ]);
  283.             } else {
  284.                 return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  285.             }
  286.             
  287.         } else {
  288.             // 非会員の場合、ログイン画面を表示
  289.             //  ログイン後の画面遷移先を設定
  290.             $this->setLoginTargetPath($this->generateUrl('product_add_favorite', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  291.             $this->session->getFlashBag()->set('eccube.add.favorite'true);
  292.             $event = new EventArgs(
  293.                 [
  294.                     'Product' => $Product,
  295.                 ],
  296.                 $request
  297.             );
  298.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  299.             if ($request->isXmlHttpRequest()) {
  300.                 return $this->json([
  301.                     'done' => false
  302.                 ]);
  303.             } else {
  304.                 return $this->redirectToRoute('mypage_login');
  305.             }
  306.         }
  307.     }
  308.     
  309.     /**
  310.      * お気に入り商品を削除する.
  311.      *
  312.      * @Route("/product/favorite/{id}/delete", name="product_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  313.      */
  314.     public function delete(Request $requestProduct $Product)
  315.     {
  316.         $this->isTokenValid();
  317.         /** @var \Eccube\Entity\Customer $Customer */
  318.         $Customer $this->getUser();
  319.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  320.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  321.         if ($CustomerFavoriteProduct) {
  322.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  323.         } else {
  324.             throw new BadRequestHttpException();
  325.         }
  326.         $event = new EventArgs(
  327.             [
  328.                 'Customer' => $Customer,
  329.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  330.             ], $request
  331.         );
  332.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  333.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  334.         if ($request->isXmlHttpRequest()) {
  335.             return $this->json([
  336.                 "done" => true
  337.             ]);
  338.         }
  339.         return $this->redirect($this->generateUrl('mypage_favorite'));
  340.     }
  341. }