app/Customize/Controller/CustomProductController.php line 177

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\Controller\AbstractController;
  14. use Eccube\Controller\ProductController as BaseProductController;
  15. use Eccube\Entity\BaseInfo;
  16. use Eccube\Entity\Master\ProductStatus;
  17. use Eccube\Entity\Product;
  18. use Eccube\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Event\EventArgs as CacheClearEvent;
  21. use Eccube\Form\Type\AddCartType;
  22. use Eccube\Form\Type\Master\ProductListMaxType;
  23. use Eccube\Form\Type\Master\ProductListOrderByType;
  24. use Eccube\Form\Type\SearchProductType;
  25. use Eccube\Repository\BaseInfoRepository;
  26. use Eccube\Repository\CustomerFavoriteProductRepository;
  27. use Eccube\Repository\Master\ProductListMaxRepository;
  28. use Customize\Repository\ProductRepository;
  29. use Customize\Repository\Master\ContractTypeRepository;
  30. use Eccube\Service\CartService;
  31. use Eccube\Service\PurchaseFlow\PurchaseContext;
  32. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  33. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  34. use Knp\Component\Pager\PaginatorInterface;
  35. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  36. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  37. use Symfony\Component\HttpFoundation\Request;
  38. use Symfony\Component\HttpFoundation\RedirectResponse;
  39. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  40. use Symfony\Component\Routing\Annotation\Route;
  41. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  42. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  43. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  44. class CustomProductController extends BaseProductController
  45. {
  46.     /**
  47.      * @var PurchaseFlow
  48.      */
  49.     protected $purchaseFlow;
  50.     /**
  51.      * @var CustomerFavoriteProductRepository
  52.      */
  53.     protected $customerFavoriteProductRepository;
  54.     /**
  55.      * @var CartService
  56.      */
  57.     protected $cartService;
  58.     /**
  59.      * @var ProductRepository
  60.      */
  61.     protected $productRepository;
  62.     /**
  63.      * @var BaseInfo
  64.      */
  65.     protected $BaseInfo;
  66.     /**
  67.      * @var AuthenticationUtils
  68.      */
  69.     protected $helper;
  70.     /**
  71.      * @var ProductListMaxRepository
  72.      */
  73.     protected $productListMaxRepository;
  74.     /**
  75.      * @var ContractTypeRepository
  76.      */
  77.     protected $contractTypeRepository;
  78.     /**
  79.      * @var EventDispatcherInterface
  80.      */
  81.     protected $eventDispatcher;
  82.     private $title '';
  83.     /**
  84.      * ProductController constructor.
  85.      *
  86.      * @param PurchaseFlow $cartPurchaseFlow
  87.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  88.      * @param CartService $cartService
  89.      * @param ProductRepository $productRepository
  90.      * @param BaseInfoRepository $baseInfoRepository
  91.      * @param AuthenticationUtils $helper
  92.      * @param ProductListMaxRepository $productListMaxRepository
  93.      * @param ContractTypeRepository $contractTypeRepository
  94.      * @param EventDispatcherInterface $eventDispatcher
  95.      */
  96.     public function __construct(
  97.         PurchaseFlow $cartPurchaseFlow,
  98.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  99.         CartService $cartService,
  100.         ProductRepository $productRepository,
  101.         BaseInfoRepository $baseInfoRepository,
  102.         AuthenticationUtils $helper,
  103.         ProductListMaxRepository $productListMaxRepository,
  104.         ContractTypeRepository $contractTypeRepository,
  105.         EventDispatcherInterface $eventDispatcher
  106.     ) {
  107.         $this->purchaseFlow $cartPurchaseFlow;
  108.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  109.         $this->cartService $cartService;
  110.         $this->productRepository $productRepository;
  111.         $this->BaseInfo $baseInfoRepository->get();
  112.         $this->helper $helper;
  113.         $this->productListMaxRepository $productListMaxRepository;
  114.         $this->contractTypeRepository $contractTypeRepository;
  115.         $this->eventDispatcher $eventDispatcher;
  116.     }
  117.     /**
  118.      * 商品一覧画面.
  119.      *
  120.      * @Route("/products/list", name="product_list", methods={"GET"})
  121.      * @Template("Product/list.twig")
  122.      */
  123.     public function index(Request $requestPaginatorInterface $paginator)
  124.     {
  125.         $empty_cookies $request->query->get('empty_cookie');
  126.         if ($empty_cookies == '1') {
  127.             $response = new RedirectResponse($this->generateUrl('product_list'));
  128.             $response->headers->clearCookie('en_user');
  129.             //カートを空にする
  130.             $this->cartService->clear();
  131.             //キャッシュをクリアする
  132.             $this->eventDispatcher->dispatch(new CacheClearEvent());
  133.             return $response;
  134.         }
  135.         // Doctrine SQLFilter
  136.         if ($this->BaseInfo->isOptionNostockHidden()) {
  137.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  138.         }
  139.         // handleRequestは空のqueryの場合は無視するため
  140.         if ($request->getMethod() === 'GET') {
  141.             $request->query->set('pageno'$request->query->get('pageno'''));
  142.         }
  143.         // searchForm
  144.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  145.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  146.         if ($request->getMethod() === 'GET') {
  147.             $builder->setMethod('GET');
  148.         }
  149.         $event = new EventArgs(
  150.             [
  151.                 'builder' => $builder,
  152.             ],
  153.             $request
  154.         );
  155.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  156.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  157.         $searchForm $builder->getForm();
  158.         $searchForm->handleRequest($request);
  159.         // paginator
  160.         $searchData $searchForm->getData();
  161.         $LoginTypeInfo $this->getLoginTypeInfo();
  162.         $qb $this->productRepository->getQueryBuilderBySearchDataWithLoginTypeInfo($searchData$LoginTypeInfo);
  163.         $ContractTypeAll $this->contractTypeRepository->findAll();
  164.         $event = new EventArgs(
  165.             [
  166.                 'searchData' => $searchData,
  167.                 'qb' => $qb,
  168.             ],
  169.             $request
  170.         );
  171.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  172.         $searchData $event->getArgument('searchData');
  173.         $query $qb->getQuery()
  174.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  175.         /** @var SlidingPagination $pagination */
  176.         $pagination $paginator->paginate(
  177.             $query,
  178.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  179.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  180.         );
  181.         $ids = [];
  182.         foreach ($pagination as $Product) {
  183.             $ids[] = $Product->getId();
  184.         }
  185.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategoriesWithLoginTypeInfo($searchData$ids'p.id'$LoginTypeInfo);
  186.         // addCart form
  187.         $forms = [];
  188.         foreach ($pagination as $Product) {
  189.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  190.             $Product->setHiddenClassCategory($ContractTypeAll);
  191.             $builder $this->formFactory->createNamedBuilder(
  192.                 '',
  193.                 AddCartType::class,
  194.                 null,
  195.                 [
  196.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  197.                     'allow_extra_fields' => true,
  198.                 ]
  199.             );
  200.             $addCartForm $builder->getForm();
  201.             $forms[$Product->getId()] = $addCartForm->createView();
  202.         }
  203.         // 表示件数
  204.         $builder $this->formFactory->createNamedBuilder(
  205.             'disp_number',
  206.             ProductListMaxType::class,
  207.             null,
  208.             [
  209.                 'required' => false,
  210.                 'allow_extra_fields' => true,
  211.             ]
  212.         );
  213.         if ($request->getMethod() === 'GET') {
  214.             $builder->setMethod('GET');
  215.         }
  216.         $event = new EventArgs(
  217.             [
  218.                 'builder' => $builder,
  219.             ],
  220.             $request
  221.         );
  222.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_DISP$event);
  223.         $dispNumberForm $builder->getForm();
  224.         $dispNumberForm->handleRequest($request);
  225.         // ソート順
  226.         $builder $this->formFactory->createNamedBuilder(
  227.             'orderby',
  228.             ProductListOrderByType::class,
  229.             null,
  230.             [
  231.                 'required' => false,
  232.                 'allow_extra_fields' => true,
  233.             ]
  234.         );
  235.         if ($request->getMethod() === 'GET') {
  236.             $builder->setMethod('GET');
  237.         }
  238.         $event = new EventArgs(
  239.             [
  240.                 'builder' => $builder,
  241.             ],
  242.             $request
  243.         );
  244.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_ORDER$event);
  245.         $orderByForm $builder->getForm();
  246.         $orderByForm->handleRequest($request);
  247.         $Category $searchForm->get('category_id')->getData();
  248.         // if (wp_remote_get($request->getSchemeAndHttpHost() . $request->getBasePath() . '/shop/wp-json/wp/v2/store-news?per_page=3&_embed', false)) {
  249.         //     $response = wp_remote_get($request->getSchemeAndHttpHost() . $request->getBasePath() . '/shop/wp-json/wp/v2/store-news?per_page=3&_embed', false);
  250.         //     $posts = json_decode($response["body"]);
  251.         // } else {
  252.         //     $posts = false;
  253.         // }
  254.         // $storeBlogDatas = [];
  255.         // if ($posts) {
  256.         //   foreach ($posts as $data) {
  257.         //     $item = [];
  258.         //     $item['title'] = $data->title;
  259.         //     $item['date'] = $data->date;
  260.         //     $item['link'] = $data->link;
  261.         //     $name = 'wp:featuredmedia';
  262.         //     if (isset($data->_embedded->{$name})) {
  263.         //       $item['attachment'] = $data->_embedded->{$name}[0];
  264.         //     }
  265.         //     $name = 'wp:term';
  266.         //     if (isset($data->_embedded->{$name})) {
  267.         //       $item['category'] = $data->_embedded->{$name}[0];
  268.         //     }
  269.         //     $storeBlogDatas[] = $item;
  270.         //   }
  271.         // }
  272.         return [
  273.             'subtitle' => $this->getPageTitle($searchData),
  274.             'pagination' => $pagination,
  275.             'search_form' => $searchForm->createView(),
  276.             'disp_number_form' => $dispNumberForm->createView(),
  277.             'order_by_form' => $orderByForm->createView(),
  278.             'forms' => $forms,
  279.             'Category' => $Category,
  280.             // 'storeBlogDatas' => $storeBlogDatas,
  281.         ];
  282.     }
  283.     /**
  284.      * 商品詳細画面.
  285.      *
  286.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  287.      * @Template("Product/detail.twig")
  288.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  289.      *
  290.      * @param Request $request
  291.      * @param Product $Product
  292.      *
  293.      * @return array
  294.      */
  295.     public function detail(Request $requestProduct $Product)
  296.     {
  297.         if (!$this->checkVisibility($Product)) {
  298.             throw new NotFoundHttpException();
  299.         }
  300.         $LoginTypeInfo $this->getLoginTypeInfo();
  301.         $LoginType $LoginTypeInfo["LoginType"];
  302.         $Customer $LoginTypeInfo["Customer"];
  303.         $ChainStore $LoginTypeInfo["ChainStore"];
  304.         $ContractType $LoginTypeInfo["ContractType"];
  305.         $ContractTypeList $this->contractTypeRepository->findBy(['is_hidden' => 'N']);
  306.         $ContractTypeAll $this->contractTypeRepository->findAll();
  307.         $Product->setLoginTypeInfo($LoginTypeInfo$ContractTypeList);
  308.         $Product->setHiddenClassCategory($ContractTypeAll);
  309.         if(is_object($ContractType)){
  310.             if($ContractType->getShowProduct() != "Y"){
  311.                 throw new NotFoundHttpException();
  312.             }
  313.         }
  314.         $builder $this->formFactory->createNamedBuilder(
  315.             '',
  316.             AddCartType::class,
  317.             null,
  318.             [
  319.                 'product' => $Product,
  320.                 'id_add_product_id' => false,
  321.             ]
  322.         );
  323.         $event = new EventArgs(
  324.             [
  325.                 'builder' => $builder,
  326.                 'Product' => $Product,
  327.             ],
  328.             $request
  329.         );
  330.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE$event);
  331.         $is_favorite false;
  332.         if ($this->isGranted('ROLE_USER')) {
  333.             $Customer $this->getUser();
  334.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  335.         }
  336.         return [
  337.             'title' => $this->title,
  338.             'subtitle' => $Product->getName(),
  339.             'form' => $builder->getForm()->createView(),
  340.             'Product' => $Product,
  341.             'is_favorite' => $is_favorite
  342.         ];
  343.     }
  344.     private function getLoginTypeInfo()
  345.     {
  346.         $LoginType 1;         //Default is guest
  347.         $Customer null;
  348.         $ChainStore null;
  349.         $ContractType null;
  350.         if ($this->isGranted('IS_AUTHENTICATED_FULLY') || $this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  351.             $Customer $this->getUser();
  352.             $ChainStore $Customer->getChainStore();
  353.             if(is_object($ChainStore)){
  354.                 $LoginType 3;         //ChainStore member
  355.                 $ContractType $ChainStore->getContractType();
  356.             }else{
  357.                 $LoginType 2;         //Normal member
  358.             }
  359.         }
  360.         return [
  361.             'LoginType' => $LoginType,
  362.             'Customer' => $Customer,
  363.             'ChainStore' => $ChainStore,
  364.             'ContractType' => $ContractType,
  365.         ];
  366.     }
  367. }