app/Customize/Controller/ProductController.php line 135

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\BaseInfo;
  14. use Eccube\Entity\Master\ProductStatus;
  15. use Eccube\Entity\Product;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\AddCartType;
  19. use Eccube\Form\Type\Master\ProductListMaxType;
  20. use Eccube\Form\Type\Master\ProductListOrderByType;
  21. use Eccube\Form\Type\SearchProductType;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\Master\ProductListMaxRepository;
  25. use Customize\Repository\ProductRepository;
  26. use Eccube\Repository\ProductClassRepository;
  27. use Eccube\Service\CartService;
  28. use Eccube\Service\PurchaseFlow\PurchaseContext;
  29. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  30. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  31. use Knp\Component\Pager\Paginator;
  32. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  33. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  34. use Symfony\Component\HttpFoundation\Request;
  35. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  36. use Symfony\Component\Routing\Annotation\Route;
  37. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  38. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  39. use Eccube\Repository\CustomerRepository;
  40. use Eccube\Controller\AbstractController;
  41. class ProductController extends AbstractController
  42. {
  43.      /**
  44.      * @var CustomerRepository
  45.      */
  46.     protected $customerRepository;
  47.    
  48.     /**
  49.      * @var ProductClassRepository
  50.      */
  51.     protected $productClassRepository;
  52.     /**
  53.      * @var PurchaseFlow
  54.      */
  55.     protected $purchaseFlow;
  56.     /**
  57.      * @var CustomerFavoriteProductRepository
  58.      */
  59.     protected $customerFavoriteProductRepository;
  60.     /**
  61.      * @var CartService
  62.      */
  63.     protected $cartService;
  64.     /**
  65.      * @var ProductRepository
  66.      */
  67.     protected $productRepository;
  68.     /**
  69.      * @var BaseInfo
  70.      */
  71.     protected $BaseInfo;
  72.     /**
  73.      * @var AuthenticationUtils
  74.      */
  75.     protected $helper;
  76.     /**
  77.      * @var ProductListMaxRepository
  78.      */
  79.     protected $productListMaxRepository;
  80.     private $title '';
  81.     /**
  82.      * ProductController constructor.
  83.      *
  84.      * @param PurchaseFlow $cartPurchaseFlow
  85.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  86.      * @param CartService $cartService
  87.      * @param ProductRepository $productRepository
  88.      * @param ProductClassRepository $productClassRepository
  89.      * @param BaseInfoRepository $baseInfoRepository
  90.      * @param AuthenticationUtils $helper
  91.      * @param CustomerRepository $customerRepository
  92.      * @param ProductListMaxRepository $productListMaxRepository
  93.      */
  94.     public function __construct(
  95.         PurchaseFlow $cartPurchaseFlow,
  96.         ProductClassRepository $productClassRepository,
  97.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  98.         CartService $cartService,
  99.         ProductRepository $productRepository,
  100.         BaseInfoRepository $baseInfoRepository,
  101.         AuthenticationUtils $helper,
  102.         ProductListMaxRepository $productListMaxRepository,
  103.         CustomerRepository $customerRepository
  104.     ) {
  105.         $this->purchaseFlow $cartPurchaseFlow;
  106.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  107.         $this->cartService $cartService;
  108.         $this->productRepository $productRepository;
  109.         $this->productClassRepository $productClassRepository;
  110.         $this->BaseInfo $baseInfoRepository->get();
  111.         $this->helper $helper;
  112.         $this->productListMaxRepository $productListMaxRepository;
  113.         $this->customerRepository $customerRepository;
  114.     }
  115.     /**
  116.      * 商品一覧画面.
  117.      *
  118.      * @Route("/products/list", name="product_list")
  119.      * @Template("Product/list.twig")
  120.      */
  121.     public function index(Request $requestPaginator $paginator)
  122.     {
  123.         if($this->getUser() == null){
  124.             return $this->redirectToRoute('mypage_login');
  125.             }
  126.         else{
  127.             $productData $this->productRepository->getPublicStatus();
  128.             $productCount count($productData);
  129.             
  130.         
  131.             
  132.             // Doctrine SQLFilter
  133.             if ($this->BaseInfo->isOptionNostockHidden()) {
  134.                 $this->entityManager->getFilters()->enable('option_nostock_hidden');
  135.             }
  136.             // handleRequestは空のqueryの場合は無視するため
  137.             if ($request->getMethod() === 'GET') {
  138.                 $request->query->set('pageno'$request->query->get('pageno'''));
  139.                 if( preg_match('/\bcategory_id=1\b/'$request->getURI()) ){       
  140.                     $mydata $this->getUser()->getCustomData(12);
  141.                         foreach ($mydata as $store) {
  142.                             if($store == 10){
  143.                                 $store 1;
  144.                                 $request->query->set('storename'.$store$store);
  145.                             }
  146.                             elseif ($store == 11) {
  147.                                 $store 2;
  148.                                 $request->query->set('storename'.$store$store);
  149.         
  150.                             }
  151.                             elseif ($store == 12) {
  152.                                 $store 3;
  153.                                 $request->query->set('storename'.$store$store);
  154.         
  155.                             }
  156.                             elseif ($store == 13) {
  157.                                 $store 5;
  158.                                 $request->query->set('storename'.$store$store);
  159.         
  160.                             }
  161.                             elseif ($store == 14) {
  162.                                 $store 7;
  163.                                 $request->query->set('storename'.$store$store);
  164.         
  165.                             }
  166.                         }
  167.                 }
  168.                 else{
  169.                     if(! preg_match('/\bname\b/'$request->getURI())){
  170.                         $request->query->set('orderby'"2");
  171.                     }
  172.               
  173.                     $request->query->set('category'"2");
  174.                     if( preg_match('/\bstorename1\b/'$request->getURI()) || preg_match('/\bstorename2\b/'$request->getURI()) || preg_match('/\bstorename3\b/'$request->getURI()) || preg_match('/\bstorename5\b/'$request->getURI()) || preg_match('/\bstorename7\b/'$request->getURI())){
  175.                     }
  176.                     else{
  177.                         $mydata $this->getUser()->getCustomData(12);
  178.                         foreach ($mydata as $store) {
  179.                             if($store == 10){
  180.                                 $store 1;
  181.                                 $request->query->set('storename'.$store$store);
  182.                             }
  183.                             elseif ($store == 11) {
  184.                                 $store 2;
  185.                                 $request->query->set('storename'.$store$store);
  186.         
  187.                             }
  188.                             elseif ($store == 12) {
  189.                                 $store 3;
  190.                                 $request->query->set('storename'.$store$store);
  191.         
  192.                             }
  193.                             elseif ($store == 13) {
  194.                                 $store 5;
  195.                                 $request->query->set('storename'.$store$store);
  196.         
  197.                             }
  198.                             elseif ($store == 14) {
  199.                                 $store 7;
  200.                                 $request->query->set('storename'.$store$store);
  201.         
  202.                             }
  203.                         }
  204.                     }
  205.                     
  206.                 }
  207.                
  208.             
  209.             }
  210.         
  211.             // searchForm
  212.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  213.             $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  214.             
  215.             if ($request->getMethod() === 'GET') {
  216.                 $builder->setMethod('GET');
  217.             }
  218.             $event = new EventArgs(
  219.                 [
  220.                     'builder' => $builder,
  221.                 ],
  222.                 $request
  223.             );
  224.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  225.             /* @var $searchForm \Symfony\Component\Form\FormInterface */
  226.             $searchForm $builder->getForm();
  227.             $searchForm->handleRequest($request);
  228.             // paginator
  229.             $searchData $searchForm->getData();
  230.             $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  231.             $event = new EventArgs(
  232.                 [
  233.                     'searchData' => $searchData,
  234.                     'qb' => $qb,
  235.                 ],
  236.                 $request
  237.             );
  238.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  239.             $searchData $event->getArgument('searchData');
  240.             $query $qb->getQuery()
  241.                 ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  242.             /** @var SlidingPagination $pagination */
  243.             if( preg_match('/\bcategory_id=1\b/'$request->getURI()) ){      
  244.                 
  245.                 $pagination $paginator->paginate(
  246.                     $query,
  247.                     !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  248.                     !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  249.                 );
  250.             }
  251.             else{
  252.                 $pagination $paginator->paginate(
  253.                     $query,
  254.                     !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  255.                     !empty($searchData['disp_number']) ? 100 $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  256.                 );
  257.             }
  258.             
  259.             $ids = [];
  260.             foreach ($pagination as $Product) {
  261.                 $ids[] = $Product->getId();
  262.             }
  263.             $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  264.             // addCart form
  265.             $forms = [];
  266.             foreach ($pagination as $Product) {
  267.                 /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  268.                 $builder $this->formFactory->createNamedBuilder(
  269.                     '',
  270.                     AddCartType::class,
  271.                     null,
  272.                     [
  273.                         'product' => $ProductsAndClassCategories[$Product->getId()],
  274.                         'allow_extra_fields' => true,
  275.                     ]
  276.                 );
  277.                 $addCartForm $builder->getForm();
  278.                 $forms[$Product->getId()] = $addCartForm->createView();
  279.             }
  280.             // 表示件数
  281.             $builder $this->formFactory->createNamedBuilder(
  282.                 'disp_number',
  283.                 ProductListMaxType::class,
  284.                 null,
  285.                 [
  286.                     'required' => false,
  287.                     'allow_extra_fields' => true,
  288.                 ]
  289.             );
  290.             if ($request->getMethod() === 'GET') {
  291.                 $builder->setMethod('GET');
  292.             }
  293.             $event = new EventArgs(
  294.                 [
  295.                     'builder' => $builder,
  296.                 ],
  297.                 $request
  298.             );
  299.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_DISP$event);
  300.             $dispNumberForm $builder->getForm();
  301.             $dispNumberForm->handleRequest($request);
  302.             // ソート順
  303.             $builder $this->formFactory->createNamedBuilder(
  304.                 'orderby',
  305.                 ProductListOrderByType::class,
  306.                 null,
  307.                 [
  308.                     'required' => false,
  309.                     'allow_extra_fields' => true,
  310.                 ]
  311.             );
  312.             if ($request->getMethod() === 'GET') {
  313.                 $builder->setMethod('GET');
  314.             }
  315.             $event = new EventArgs(
  316.                 [
  317.                     'builder' => $builder,
  318.                 ],
  319.                 $request
  320.             );
  321.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_ORDER$event);
  322.             $orderByForm $builder->getForm();
  323.             $orderByForm->handleRequest($request);
  324.             $Category $searchForm->get('category_id')->getData();
  325.             return [
  326.                 'subtitle' => $this->getPageTitle($searchData),
  327.                 'pagination' => $pagination,
  328.                 'search_form' => $searchForm->createView(),
  329.                 'disp_number_form' => $dispNumberForm->createView(),
  330.                 'order_by_form' => $orderByForm->createView(),
  331.                 'forms' => $forms,
  332.                 'Category' => $Category,
  333.                 'result' => $productCount,
  334.             ];
  335.         }
  336.     }
  337.     /**
  338.      * 商品詳細画面.
  339.      *
  340.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  341.      * @Template("Product/detail.twig")
  342.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  343.      *
  344.      * @param Request $request
  345.      * @param Product $Product
  346.      *
  347.      * @return array
  348.      */
  349.     public function detail(Request $requestProduct $Product)
  350.     {
  351.         if($this->getUser() == null){
  352.             return $this->redirectToRoute('mypage_login');
  353.             }
  354.         else{
  355.             $ca_flag false;
  356.             foreach($Product->getProductCategories() as $ProductCategory) {
  357.                 if( $ProductCategory->getCategory()->getId() == 2){
  358.                     $ca_flag true;
  359.                 }
  360.             }
  361.             if($ca_flag == true){
  362.                 return $this->redirectToRoute('product_list');
  363.             }
  364.             else{
  365.                 $mydata $this->getUser()->getCustomData(12);
  366.                 $storetype = array();
  367.                 
  368.                 foreach ($mydata as $store) {
  369.                     if($store == 10){
  370.                         $storetype[] = 1;
  371.                     }
  372.                     elseif ($store == 11) {
  373.                         $storetype[] = 2;
  374.                     }
  375.                     elseif ($store == 12) {
  376.                         $storetype[] = 3;
  377.                     }
  378.                     elseif ($store == 13) {
  379.                         $storetype[] = 5;
  380.     
  381.                     }
  382.                     elseif ($store == 14) {
  383.                         $storetype[] = 7;
  384.     
  385.                     }
  386.                 }
  387.     
  388.     
  389.                 $store_flag false;
  390.     
  391.                 foreach($storetype as $storename){
  392.                     if ($storename == (int)$Product->getStoretype()) {
  393.                         $store_flag true;
  394.     
  395.                     }
  396.                 }
  397.     
  398.                 if($store_flag == false){
  399.                     return $this->redirectToRoute('product_list' , ['category_id' => '1']);
  400.                 }
  401.             }
  402.         
  403.             if (!$this->checkVisibility($Product)) {
  404.                 throw new NotFoundHttpException();
  405.             }
  406.             $builder $this->formFactory->createNamedBuilder(
  407.                 '',
  408.                 AddCartType::class,
  409.                 null,
  410.                 [
  411.                     'product' => $Product,
  412.                     'id_add_product_id' => false,
  413.                 ]
  414.             );
  415.             $event = new EventArgs(
  416.                 [
  417.                     'builder' => $builder,
  418.                     'Product' => $Product,
  419.                 ],
  420.                 $request
  421.             );
  422.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE$event);
  423.             $is_favorite false;
  424.             if ($this->isGranted('ROLE_USER')) {
  425.                 $Customer $this->getUser();
  426.                 $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  427.             }
  428.             return [
  429.                 'title' => $this->title,
  430.                 'subtitle' => $Product->getName(),
  431.                 'form' => $builder->getForm()->createView(),
  432.                 'Product' => $Product,
  433.                 'is_favorite' => $is_favorite,
  434.             ];
  435.         }
  436.     }
  437.     /**
  438.      * お気に入り追加.
  439.      *
  440.      * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"})
  441.      */
  442.     public function addFavorite(Request $requestProduct $Product)
  443.     {
  444.         $this->checkVisibility($Product);
  445.         $event = new EventArgs(
  446.             [
  447.                 'Product' => $Product,
  448.             ],
  449.             $request
  450.         );
  451.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE$event);
  452.         if ($this->isGranted('ROLE_USER')) {
  453.             $Customer $this->getUser();
  454.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product);
  455.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  456.             $event = new EventArgs(
  457.                 [
  458.                     'Product' => $Product,
  459.                 ],
  460.                 $request
  461.             );
  462.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  463.             return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  464.         } else {
  465.             // 非会員の場合、ログイン画面を表示
  466.             //  ログイン後の画面遷移先を設定
  467.             $this->setLoginTargetPath($this->generateUrl('product_add_favorite', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  468.             $this->session->getFlashBag()->set('eccube.add.favorite'true);
  469.             $event = new EventArgs(
  470.                 [
  471.                     'Product' => $Product,
  472.                 ],
  473.                 $request
  474.             );
  475.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  476.             return $this->redirectToRoute('mypage_login');
  477.         }
  478.     }
  479.     /**
  480.      * カートに追加.
  481.      *
  482.      * @Route("/products/add_cart/{id}", name="product_add_cart", methods={"POST"}, requirements={"id" = "\d+"})
  483.      */
  484.     public function addCart(Request $requestProduct $Product)
  485.     {
  486.         // エラーメッセージの配列
  487.         $errorMessages = [];
  488.         if (!$this->checkVisibility($Product)) {
  489.             throw new NotFoundHttpException();
  490.         }
  491.         $builder $this->formFactory->createNamedBuilder(
  492.             '',
  493.             AddCartType::class,
  494.             null,
  495.             [
  496.                 'product' => $Product,
  497.                 'id_add_product_id' => false,
  498.             ]
  499.         );
  500.         $event = new EventArgs(
  501.             [
  502.                 'builder' => $builder,
  503.                 'Product' => $Product,
  504.             ],
  505.             $request
  506.         );
  507.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE$event);
  508.         /* @var $form \Symfony\Component\Form\FormInterface */
  509.         $form $builder->getForm();
  510.         $form->handleRequest($request);
  511.         if (!$form->isValid()) {
  512.             throw new NotFoundHttpException();
  513.         }
  514.         $addCartData $form->getData();
  515.         log_info(
  516.             'カート追加処理開始',
  517.             [
  518.                 'product_id' => $Product->getId(),
  519.                 'product_class_id' => $addCartData['product_class_id'],
  520.                 'quantity' => $addCartData['quantity'],
  521.             ]
  522.         );
  523.         // カートへ追加
  524.         $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']);
  525.         // 明細の正規化
  526.         $Carts $this->cartService->getCarts();
  527.         foreach ($Carts as $Cart) {
  528.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  529.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  530.             if ($result->hasError()) {
  531.                 $this->cartService->removeProduct($addCartData['product_class_id']);
  532.                 foreach ($result->getErrors() as $error) {
  533.                     $errorMessages[] = $error->getMessage();
  534.                 }
  535.             }
  536.             foreach ($result->getWarning() as $warning) {
  537.                 $errorMessages[] = $warning->getMessage();
  538.             }
  539.         }
  540.         $this->cartService->save();
  541.         log_info(
  542.             'カート追加処理完了',
  543.             [
  544.                 'product_id' => $Product->getId(),
  545.                 'product_class_id' => $addCartData['product_class_id'],
  546.                 'quantity' => $addCartData['quantity'],
  547.             ]
  548.         );
  549.         $event = new EventArgs(
  550.             [
  551.                 'form' => $form,
  552.                 'Product' => $Product,
  553.             ],
  554.             $request
  555.         );
  556.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE$event);
  557.         if ($event->getResponse() !== null) {
  558.             return $event->getResponse();
  559.         }
  560.         if ($request->isXmlHttpRequest()) {
  561.             // ajaxでのリクエストの場合は結果をjson形式で返す。
  562.             // 初期化
  563.             $done null;
  564.             $messages = [];
  565.             if (empty($errorMessages)) {
  566.                 // エラーが発生していない場合
  567.                 $done true;
  568.                 array_push($messagestrans('front.product.add_cart_complete'));
  569.             } else {
  570.                 // エラーが発生している場合
  571.                 $done false;
  572.                 $messages $errorMessages;
  573.             }
  574.             return $this->json(['done' => $done'messages' => $messages]);
  575.         } else {
  576.             // ajax以外でのリクエストの場合はカート画面へリダイレクト
  577.             foreach ($errorMessages as $errorMessage) {
  578.                 $this->addRequestError($errorMessage);
  579.             }
  580.             return $this->redirectToRoute('cart');
  581.         }
  582.     }
  583.     /**
  584.      * ページタイトルの設定
  585.      *
  586.      * @param  null|array $searchData
  587.      *
  588.      * @return str
  589.      */
  590.     protected function getPageTitle($searchData)
  591.     {
  592.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  593.             return trans('front.product.search_result');
  594.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  595.             return $searchData['category_id']->getName();
  596.         } else {
  597.             return trans('front.product.all_products');
  598.         }
  599.     }
  600.     /**
  601.      * 閲覧可能な商品かどうかを判定
  602.      *
  603.      * @param Product $Product
  604.      *
  605.      * @return boolean 閲覧可能な場合はtrue
  606.      */
  607.     protected function checkVisibility(Product $Product)
  608.     {
  609.         $is_admin $this->session->has('_security_admin');
  610.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  611.         if (!$is_admin) {
  612.             // 在庫なし商品の非表示オプションが有効な場合.
  613.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  614.             //     if (!$Product->getStockFind()) {
  615.             //         return false;
  616.             //     }
  617.             // }
  618.             // 公開ステータスでない商品は表示しない.
  619.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  620.                 return false;
  621.             }
  622.         }
  623.         return true;
  624.     }
  625. }