src/Eccube/Controller/ProductController.php line 154

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 Eccube\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Master\ProductStatus;
  15. use Eccube\Entity\Product;
  16. use Eccube\Entity\FavoriteKey;    //@##ADD 2021-11-21 upseek
  17. use Eccube\Event\EccubeEvents;
  18. use Eccube\Event\EventArgs;
  19. use Eccube\Form\Type\AddCartType;
  20. use Eccube\Form\Type\SearchProductType;
  21. use Eccube\Repository\BaseInfoRepository;
  22. use Eccube\Repository\CustomerFavoriteProductRepository;
  23. use Eccube\Repository\FavoriteKeyRepository;    //@##ADD 2021-11-21 upseek
  24. use Eccube\Repository\FavoriteProductRepository;    //@##ADD 2021-11-21 upseek
  25. use Eccube\Repository\Master\ProductListMaxRepository;
  26. use Eccube\Repository\ProductRepository;
  27. use Eccube\Repository\ProductClassRepository;    //@##ADD 2023-02-10 upseek
  28. use Eccube\Service\CalculatePriceService;    //@##ADD 2021-11-05 upseek
  29. use Eccube\Service\CartService;
  30. use Eccube\Service\PurchaseFlow\PurchaseContext;
  31. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  32. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  33. use Knp\Component\Pager\PaginatorInterface;
  34. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  35. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  38. use Symfony\Component\Routing\Annotation\Route;
  39. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  40. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  41. use Plugin\ShelvingOption4\Entity\ShelvingOptionFavoriteProduct;    //@##ADD 2022-11-16 upseek
  42. use Plugin\ShelvingOption4\Entity\ShelvingOptionCustomerFavoriteProduct;    //@##ADD 2022-11-16 upseek
  43. class ProductController extends AbstractController
  44. {
  45.     /**
  46.      * @var PurchaseFlow
  47.      */
  48.     protected $purchaseFlow;
  49.     /**
  50.      * @var CustomerFavoriteProductRepository
  51.      */
  52.     protected $customerFavoriteProductRepository;
  53. //@##ADD START 2021-11-21
  54.     /**
  55.      * @var FavoriteKeyRepository
  56.      */
  57.     protected $favoriteKeyRepository;
  58.     /**
  59.      * @var FavoriteProductRepository
  60.      */
  61.     protected $favoriteProductRepository;
  62. //@##ADD END
  63.     /**
  64.      * @var CartService
  65.      */
  66.     protected $cartService;
  67.     /**
  68.      * @var CalculatePriceService
  69.      */
  70.     protected $calculatePriceService;
  71.     /**
  72.      * @var ProductRepository
  73.      */
  74.     protected $productRepository;
  75. //@##ADD START 2023-02-10
  76.     protected $productClassRepository;
  77. //@##ADD END
  78.     /**
  79.      * @var BaseInfo
  80.      */
  81.     protected $BaseInfo;
  82.     /**
  83.      * @var AuthenticationUtils
  84.      */
  85.     protected $helper;
  86.     /**
  87.      * @var ProductListMaxRepository
  88.      */
  89.     protected $productListMaxRepository;
  90.     private $title '';
  91.     /**
  92.      * ProductController constructor.
  93.      *
  94.      * @param PurchaseFlow $cartPurchaseFlow
  95.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  96.      * @param FavoriteKeyRepository $favoriteKeyRepository
  97.      * @param FavoriteProductRepository $favoriteProductRepository
  98.      * @param CartService $cartService
  99.      * @param CalculatePriceService $calculatePriceService
  100.      * @param ProductRepository $productRepository
  101.      * @param ProductClassRepository $productClassRepository
  102.      * @param BaseInfoRepository $baseInfoRepository
  103.      * @param AuthenticationUtils $helper
  104.      * @param ProductListMaxRepository $productListMaxRepository
  105.      */
  106.     public function __construct(
  107.         PurchaseFlow $cartPurchaseFlow,
  108.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  109.         FavoriteKeyRepository $favoriteKeyRepository,            //@##ADD 2021-11-21
  110.         FavoriteProductRepository $favoriteProductRepository,    //@##ADD 2021-11-21
  111.         CartService $cartService,
  112.         CalculatePriceService $calculatePriceService,
  113.         ProductRepository $productRepository,
  114.         ProductClassRepository $productClassRepository,            //@##ADD 2023-02-10
  115.         BaseInfoRepository $baseInfoRepository,
  116.         AuthenticationUtils $helper,
  117.         ProductListMaxRepository $productListMaxRepository
  118.     ) {
  119.         $this->purchaseFlow $cartPurchaseFlow;
  120.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  121.         $this->favoriteKeyRepository $favoriteKeyRepository;            //@##ADD 2021-11-21
  122.         $this->favoriteProductRepository $favoriteProductRepository;    //@##ADD 2021-11-21
  123.         $this->cartService $cartService;
  124.         $this->calculatePriceService $calculatePriceService;
  125.         $this->productRepository $productRepository;
  126.         $this->productClassRepository $productClassRepository;        //@##ADD 2023-02-10
  127.         $this->BaseInfo $baseInfoRepository->get();
  128.         $this->helper $helper;
  129.         $this->productListMaxRepository $productListMaxRepository;
  130.     }
  131.     /**
  132.      * 商品一覧画面.
  133.      *
  134.      * @Route("/products/list", name="product_list", methods={"GET"})
  135.      * @Template("Product/list.twig")
  136.      */
  137.     public function index(Request $requestPaginatorInterface $paginator)
  138.     {
  139.         // Doctrine SQLFilter
  140.         if ($this->BaseInfo->isOptionNostockHidden()) {
  141.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  142.         }
  143.         // handleRequestは空のqueryの場合は無視するため
  144.         if ($request->getMethod() === 'GET') {
  145.             $request->query->set('pageno'$request->query->get('pageno'''));
  146.         }
  147.         // searchForm
  148.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  149.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  150.         if ($request->getMethod() === 'GET') {
  151.             $builder->setMethod('GET');
  152.         }
  153.         $event = new EventArgs(
  154.             [
  155.                 'builder' => $builder,
  156.             ],
  157.             $request
  158.         );
  159.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  160.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  161.         $searchForm $builder->getForm();
  162.         $searchForm->handleRequest($request);
  163.         // paginator
  164.         $searchData $searchForm->getData();
  165.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  166.         $event = new EventArgs(
  167.             [
  168.                 'searchData' => $searchData,
  169.                 'qb' => $qb,
  170.             ],
  171.             $request
  172.         );
  173.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  174.         $searchData $event->getArgument('searchData');
  175.         $query $qb->getQuery()
  176.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  177.         /** @var SlidingPagination $pagination */
  178.         $pagination $paginator->paginate(
  179.             $query,
  180.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  181.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  182.         );
  183.         $ids = [];
  184.         foreach ($pagination as $Product) {
  185.             $ids[] = $Product->getId();
  186.         }
  187.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  188.         // addCart form
  189.         $forms = [];
  190.         foreach ($pagination as $Product) {
  191.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  192.             $builder $this->formFactory->createNamedBuilder(
  193.                 '',
  194.                 AddCartType::class,
  195.                 null,
  196.                 [
  197.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  198.                     'allow_extra_fields' => true,
  199.                 ]
  200.             );
  201.             $addCartForm $builder->getForm();
  202.             $forms[$Product->getId()] = $addCartForm->createView();
  203.         }
  204.         $Category $searchForm->get('category_id')->getData();
  205. //@##ADD START 2021-10-25
  206.         $Category0 $searchForm->get('c0')->getData();
  207.         $Category1 $searchForm->get('c1')->getData();
  208.         $Category2 $searchForm->get('c2')->getData();
  209.         $Category3 $searchForm->get('c3')->getData();
  210.         $Category4 $searchForm->get('c4')->getData();
  211.         $Category5 $searchForm->get('c5')->getData();
  212.         $Category6 $searchForm->get('c6')->getData();
  213.         $Category7 $searchForm->get('c7')->getData();
  214.         $Category8 $searchForm->get('c8')->getData();
  215.         $Category9 $searchForm->get('c9')->getData();
  216. //@##ADD END
  217.         return [
  218.             'subtitle' => $this->getPageTitle($searchData),
  219.             'pagination' => $pagination,
  220.             'search_form' => $searchForm->createView(),
  221.             'forms' => $forms,
  222.             'Category' => $Category,
  223. //@##ADD START 2021-10-25
  224.             'Category0' => $Category0,
  225.             'Category1' => $Category1,
  226.             'Category2' => $Category2,
  227.             'Category3' => $Category3,
  228.             'Category4' => $Category4,
  229.             'Category5' => $Category5,
  230.             'Category6' => $Category6,
  231.             'Category7' => $Category7,
  232.             'Category8' => $Category8,
  233.             'Category9' => $Category9,
  234. //@##ADD END
  235. //@##ADD START 2021-10-31
  236.             'color_id' => $searchData['color_id'],
  237.             'w' => $searchData['w'],
  238.             'h' => $searchData['h'],
  239. //@##ADD END 2021-10-31
  240.         ];
  241.     }
  242.     /**
  243.      * 商品詳細画面.
  244.      *
  245.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  246.      * @Template("Product/detail.twig")
  247.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  248.      *
  249.      * @param Request $request
  250.      * @param Product $Product
  251.      *
  252.      * @return array
  253.      */
  254.     public function detail(Request $requestProduct $Product)
  255.     {
  256.         if (!$this->checkVisibility($Product)) {
  257.             throw new NotFoundHttpException();
  258.         }
  259. //@##ADD START 2022-01-17
  260.         $cfp $request->query->get('cfp');
  261.         $fp $request->query->get('fp');
  262.         if($cfp) {
  263.             $CustomerFavoriteProduct $this->customerFavoriteProductRepository->find($cfp);
  264.             if($CustomerFavoriteProduct) {
  265.                 if($Product->getShelvingOption() && $CustomerFavoriteProduct->getShelvingOption()) {
  266.                     $Product->getShelvingOption()->setPattern$CustomerFavoriteProduct->getShelvingOption()->getPattern() );
  267.                     $Product->getShelvingOption()->setHeightList$CustomerFavoriteProduct->getShelvingOption()->getHeightList() );
  268.                     $Product->getShelvingOption()->setWidthList$CustomerFavoriteProduct->getShelvingOption()->getWidthList() );
  269.                     $Product->getShelvingOption()->setFullHeight$CustomerFavoriteProduct->getShelvingOption()->getFullHeight() );
  270.                     $Product->getShelvingOption()->setFullWidth$CustomerFavoriteProduct->getShelvingOption()->getFullWidth() );
  271.                     $Product->getShelvingOption()->setItems$CustomerFavoriteProduct->getShelvingOption()->getItems() );
  272.                     $Product->getShelvingOption()->setRowDoors$CustomerFavoriteProduct->getShelvingOption()->getRowDoors() );
  273.                     $Product->getShelvingOption()->setRowDrawers$CustomerFavoriteProduct->getShelvingOption()->getRowDrawers() );
  274.                     $Product->getShelvingOption()->setDepth$CustomerFavoriteProduct->getShelvingOption()->getDepth() );
  275.                     $Product->getShelvingOption()->setBackPanel$CustomerFavoriteProduct->getShelvingOption()->getBackPanel() );
  276.                     $Product->getShelvingOption()->setTexture$CustomerFavoriteProduct->getShelvingOption()->getTexture() );
  277.                     $Product->getShelvingOption()->setColorId$CustomerFavoriteProduct->getShelvingOption()->getColorId() );
  278.                     $Product->getShelvingOption()->setDoorSwing$CustomerFavoriteProduct->getShelvingOption()->getDoorSwing() );
  279.                     //※ファイルは連携しない
  280.                 }
  281.             }
  282.         } else if($fp) {
  283.             $FavoriteProduct $this->favoriteProductRepository->find($fp);
  284.             if($FavoriteProduct) {
  285.                 if($Product->getShelvingOption() && $FavoriteProduct->getShelvingOption()) {
  286.                     $Product->getShelvingOption()->setPattern$FavoriteProduct->getShelvingOption()->getPattern() );
  287.                     $Product->getShelvingOption()->setHeightList$FavoriteProduct->getShelvingOption()->getHeightList() );
  288.                     $Product->getShelvingOption()->setWidthList$FavoriteProduct->getShelvingOption()->getWidthList() );
  289.                     $Product->getShelvingOption()->setFullHeight$FavoriteProduct->getShelvingOption()->getFullHeight() );
  290.                     $Product->getShelvingOption()->setFullWidth$FavoriteProduct->getShelvingOption()->getFullWidth() );
  291.                     $Product->getShelvingOption()->setItems$FavoriteProduct->getShelvingOption()->getItems() );
  292.                     $Product->getShelvingOption()->setRowDoors$FavoriteProduct->getShelvingOption()->getRowDoors() );
  293.                     $Product->getShelvingOption()->setRowDrawers$FavoriteProduct->getShelvingOption()->getRowDrawers() );
  294.                     $Product->getShelvingOption()->setDepth$FavoriteProduct->getShelvingOption()->getDepth() );
  295.                     $Product->getShelvingOption()->setBackPanel$FavoriteProduct->getShelvingOption()->getBackPanel() );
  296.                     $Product->getShelvingOption()->setTexture$FavoriteProduct->getShelvingOption()->getTexture() );
  297.                     $Product->getShelvingOption()->setColorId$FavoriteProduct->getShelvingOption()->getColorId() );
  298.                     $Product->getShelvingOption()->setDoorSwing$FavoriteProduct->getShelvingOption()->getDoorSwing() );
  299.                     //※ファイルは連携しない
  300.                 }
  301.             }
  302.         }
  303. //@##ADD END
  304. //@##ADD START 2023-02-07 2023-03-17 2023-04-04 Product追加
  305.         //メタproduct:price:amountのデフォルト値(最小販売価格 税込)
  306.         //<meta property="product:price:amount" content="{{ Product.getPrice02IncTaxMin }}"/>
  307.         $Product->meta_product_price_amount $Product->getPrice02IncTaxMin();
  308.         //codeパラメータから規格情報を取得してデフォルト表示
  309.         $code $request->query->get('code');
  310.         $classcategory_id1 '';
  311.         $classcategory_id2 '';
  312.         if($code) {
  313.             //log_warning('code [' . $code . ']');
  314.             $productClasses $this->productClassRepository->findBy(['Product' => $Product'code' => $code'visible' => 1]);
  315.             if(count($productClasses)) {
  316.                 $productClass current($productClasses);
  317.                 if($productClass->getClassCategory1() && $productClass->getClassCategory1()->getId()) {
  318.                     $classcategory_id1 $productClass->getClassCategory1()->getId();
  319.                 }
  320.                 if($productClass->getClassCategory2() && $productClass->getClassCategory2()->getId()) {
  321.                     $classcategory_id2 $productClass->getClassCategory2()->getId();
  322.                 }
  323.                 //規格管理の販売価格(税込)をセット
  324.                 $Product->meta_product_price_amount $productClass->getPrice02IncTax();
  325.             }
  326.             //log_warning('@classcategory_id1=[' . $classcategory_id1 . ']');
  327.             //log_warning('@classcategory_id2=[' . $classcategory_id2 . ']');
  328.         }
  329. //@##ADD END
  330.         $builder $this->formFactory->createNamedBuilder(
  331.             '',
  332.             AddCartType::class,
  333.             null,
  334.             [
  335.                 'product' => $Product,
  336.                 'id_add_product_id' => false,
  337.             ]
  338.         );
  339.         $event = new EventArgs(
  340.             [
  341.                 'builder' => $builder,
  342.                 'Product' => $Product,
  343.             ],
  344.             $request
  345.         );
  346.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE);
  347.         $is_favorite false;
  348.         if ($this->isGranted('ROLE_USER')) {
  349.             $Customer $this->getUser();
  350.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  351.         }
  352.         return [
  353.             'title' => $this->title,
  354.             'subtitle' => $Product->getName(),
  355.             'form' => $builder->getForm()->createView(),
  356.             'Product' => $Product,
  357.             'is_favorite' => $is_favorite,
  358. //@##ADD 2023-02-07
  359. 'classcategory_id1' => $classcategory_id1,
  360. 'classcategory_id2' => $classcategory_id2,
  361. //@##ADD END
  362.         ];
  363.     }
  364.     /**
  365.      * お気に入り追加.
  366.      *
  367.      * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  368.      */
  369.     public function addFavorite(Request $requestProduct $Product)
  370.     {
  371.         $this->checkVisibility($Product);
  372.         $event = new EventArgs(
  373.             [
  374.                 'Product' => $Product,
  375.             ],
  376.             $request
  377.         );
  378.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE);
  379.         if ($this->isGranted('ROLE_USER')) {
  380.             $Customer $this->getUser();
  381. //@##CHG START 2022-01-17
  382. //            $this->customerFavoriteProductRepository->addFavorite($Customer, $Product);
  383.             $ShelvingOption null;
  384.             $shelving_option $request->query->get('shelving_option');
  385.             if($shelving_option) {
  386.                 $height_list = [];
  387.                 $width_list = [];
  388.                 $row_doors = [];
  389.                 $row_drawers = [];
  390.                 $items = [];
  391.                 $filename '';
  392.                 $back_panel false;
  393.                 if($shelving_option['height_list']) {
  394.                     $height_list explode(','str_replace(['[',']'], ''$shelving_option['height_list']));
  395.                 }
  396.                 if($shelving_option['width_list']) {
  397.                     $width_list explode(','str_replace(['[',']'], ''$shelving_option['width_list']));
  398.                 }
  399.                 if($shelving_option['row_doors']) {
  400.                     $row_doors explode(','str_replace(['[',']'], ''$shelving_option['row_doors']));
  401.                 }
  402.                 if($shelving_option['row_drawers']) {
  403.                     $row_drawers explode(','str_replace(['[',']'], ''$shelving_option['row_drawers']));
  404.                 }
  405.                 if($shelving_option['items']) {
  406.                     $item1s explode("],["$shelving_option['items']);
  407.                     foreach($item1s as $item) {
  408.                         $items[]= array_map('intval'explode(','str_replace(['[',']'], ''$item)));
  409.                     }
  410.                 }
  411.                 if(!empty($shelving_option['file_name'])) {
  412.                     $filename $shelving_option['file_name'];
  413.                     if(file_exists($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename)) {
  414.                         rename($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename$this->eccubeConfig['eccube_save_shelving_image_dir'] . '/' $filename);
  415.                     }
  416.                 }
  417.                 if($shelving_option['back_panel']) {
  418.                     if($shelving_option['back_panel'] == 'true') {
  419.                         $back_panel true;
  420.                     }
  421.                 }
  422.                 $ShelvingOption = new ShelvingOptionCustomerFavoriteProduct();
  423.                 $ShelvingOption->setPattern$shelving_option['pattern'] );
  424.                 $ShelvingOption->setHeightList$height_list );
  425.                 $ShelvingOption->setWidthList$width_list );
  426.                 $ShelvingOption->setFullHeight$shelving_option['full_height'] );
  427.                 $ShelvingOption->setFullWidth$shelving_option['full_width'] );
  428.                 $ShelvingOption->setItems$items );
  429.                 $ShelvingOption->setRowDoors$row_doors );
  430.                 $ShelvingOption->setRowDrawers$row_drawers );
  431.                 $ShelvingOption->setDepth$shelving_option['depth'] );
  432.                 $ShelvingOption->setBackPanel$back_panel  );
  433.                 $ShelvingOption->setTexture$shelving_option['texture'] );
  434.                 $ShelvingOption->setColorId$shelving_option['color_id'] );
  435.                 $ShelvingOption->setDoorSwing$shelving_option['door_swing'] );
  436.                 $ShelvingOption->setFileName$filename );
  437.             }
  438.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product$ShelvingOption);
  439. //@##CHG END
  440.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  441.             $event = new EventArgs(
  442.                 [
  443.                     'Product' => $Product,
  444.                 ],
  445.                 $request
  446.             );
  447.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  448.             return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  449.         } else {
  450. /*
  451.             // 非会員の場合、ログイン画面を表示
  452.             //  ログイン後の画面遷移先を設定
  453.             $this->setLoginTargetPath($this->generateUrl('product_add_favorite', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  454.             $this->session->getFlashBag()->set('eccube.add.favorite', true);
  455.             $event = new EventArgs(
  456.                 [
  457.                     'Product' => $Product,
  458.                 ],
  459.                 $request
  460.             );
  461.             $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  462.             return $this->redirectToRoute('mypage_login');
  463. */
  464.             // 非会員の場合
  465.             $FavoriteKey null;
  466.             $cookieKey $request->cookies->get('eccube_favorite');
  467.             if($cookieKey) {
  468.                 $FavoriteKey $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
  469.             }
  470.             if(!$FavoriteKey) {
  471.                 $FavoriteKey $this->favoriteKeyRepository->createFavoriteKey();
  472.             }
  473.             setcookie('eccube_favorite'$FavoriteKey->getCookieKey(), (time() + 60 60 24 30), $this->eccubeConfig['env(ECCUBE_COOKIE_PATH)']);
  474.             log_warning('add_favorite Product=[' $Product->getId() . '] cookie_key=[' $FavoriteKey->getCookieKey() . ']');
  475.             $ShelvingOption null;
  476.             $shelving_option $request->query->get('shelving_option');
  477.             if($shelving_option) {
  478.                 $height_list = [];
  479.                 $width_list = [];
  480.                 $row_doors = [];
  481.                 $row_drawers = [];
  482.                 $items = [];
  483.                 $filename '';
  484.                 $back_panel false;
  485.                 if($shelving_option['height_list']) {
  486.                     $height_list explode(','str_replace(['[',']'], ''$shelving_option['height_list']));
  487.                 }
  488.                 if($shelving_option['width_list']) {
  489.                     $width_list explode(','str_replace(['[',']'], ''$shelving_option['width_list']));
  490.                 }
  491.                 if($shelving_option['row_doors']) {
  492.                     $row_doors explode(','str_replace(['[',']'], ''$shelving_option['row_doors']));
  493.                 }
  494.                 if($shelving_option['row_drawers']) {
  495.                     $row_drawers explode(','str_replace(['[',']'], ''$shelving_option['row_drawers']));
  496.                 }
  497.                 if($shelving_option['items']) {
  498.                     $item1s explode("],["$shelving_option['items']);
  499.                     foreach($item1s as $item) {
  500.                         $items[]= array_map('intval'explode(','str_replace(['[',']'], ''$item)));
  501.                     }
  502.                 }
  503.                 if(!empty($shelving_option['file_name'])) {
  504.                     $filename $shelving_option['file_name'];
  505.                     if(file_exists($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename)) {
  506.                         rename($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename$this->eccubeConfig['eccube_save_shelving_image_dir'] . '/' $filename);
  507.                     }
  508.                 }
  509.                 if($shelving_option['back_panel']) {
  510.                     if($shelving_option['back_panel'] == 'true') {
  511.                         $back_panel true;
  512.                     }
  513.                 }
  514.                 $ShelvingOption = new ShelvingOptionFavoriteProduct();
  515.                 $ShelvingOption->setPattern$shelving_option['pattern'] );
  516.                 $ShelvingOption->setHeightList$height_list );
  517.                 $ShelvingOption->setWidthList$width_list );
  518.                 $ShelvingOption->setFullHeight$shelving_option['full_height'] );
  519.                 $ShelvingOption->setFullWidth$shelving_option['full_width'] );
  520.                 $ShelvingOption->setItems$items );
  521.                 $ShelvingOption->setRowDoors$row_doors );
  522.                 $ShelvingOption->setRowDrawers$row_drawers );
  523.                 $ShelvingOption->setDepth$shelving_option['depth'] );
  524.                 $ShelvingOption->setBackPanel$back_panel );
  525.                 $ShelvingOption->setTexture$shelving_option['texture'] );
  526.                 $ShelvingOption->setColorId$shelving_option['color_id'] );
  527.                 $ShelvingOption->setDoorSwing$shelving_option['door_swing'] );
  528.                 $ShelvingOption->setFileName$filename );
  529.             }
  530.             $this->favoriteProductRepository->addFavorite($FavoriteKey$Product$ShelvingOption);
  531.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  532.             $event = new EventArgs(
  533.                 [
  534.                     'Product' => $Product,
  535.                 ],
  536.                 $request
  537.             );
  538.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  539.             return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  540.         }
  541.     }
  542.     /**
  543.      * カートに追加.
  544.      *
  545.      * @Route("/products/add_cart/{id}", name="product_add_cart", methods={"POST"}, requirements={"id" = "\d+"})
  546.      */
  547.     public function addCart(Request $requestProduct $Product)
  548.     {
  549.         // エラーメッセージの配列
  550.         $errorMessages = [];
  551.         if (!$this->checkVisibility($Product)) {
  552.             throw new NotFoundHttpException();
  553.         }
  554.         $builder $this->formFactory->createNamedBuilder(
  555.             '',
  556.             AddCartType::class,
  557.             null,
  558.             [
  559.                 'product' => $Product,
  560.                 'id_add_product_id' => false,
  561.             ]
  562.         );
  563.         $event = new EventArgs(
  564.             [
  565.                 'builder' => $builder,
  566.                 'Product' => $Product,
  567.             ],
  568.             $request
  569.         );
  570.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE);
  571.         /* @var $form \Symfony\Component\Form\FormInterface */
  572.         $form $builder->getForm();
  573.         $form->handleRequest($request);
  574.         if (!$form->isValid()) {
  575.             throw new NotFoundHttpException();
  576.         }
  577.         $addCartData $form->getData();
  578.         log_info(
  579.             'カート追加処理開始',
  580.             [
  581.                 'product_id' => $Product->getId(),
  582.                 'product_class_id' => $addCartData['product_class_id'],
  583.                 'quantity' => $addCartData['quantity'],
  584.                 'shelving_option' => $addCartData['shelving_option'],    //@##ADD 2021-11-05 upseek
  585.             ]
  586.         );
  587.         // カートへ追加
  588.         //@##CHG START 2021-11-05 upseek
  589.         //$this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']);
  590.         $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity'], $addCartData['shelving_option']);
  591.         if($addCartData['shelving_option'] != null) {
  592.             log_warning('addCart [' $Product->getId() . '] image=[' $addCartData['shelving_option']->getFileName() . ']');
  593.             $filename $addCartData['shelving_option']->getFileName();
  594.             if(file_exists($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename)) {
  595.                 rename($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename$this->eccubeConfig['eccube_save_shelving_image_dir'] . '/' $filename);
  596.             }
  597.         }
  598.         //@##CHG END
  599.         // 明細の正規化
  600.         $Carts $this->cartService->getCarts();
  601.         foreach ($Carts as $Cart) {
  602.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  603.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  604.             if ($result->hasError()) {
  605.                 $this->cartService->removeProduct($addCartData['product_class_id']);
  606.                 foreach ($result->getErrors() as $error) {
  607.                     $errorMessages[] = $error->getMessage();
  608.                 }
  609.             }
  610.             foreach ($result->getWarning() as $warning) {
  611.                 $errorMessages[] = $warning->getMessage();
  612.             }
  613.         }
  614.         $this->cartService->save();
  615.         log_info(
  616.             'カート追加処理完了',
  617.             [
  618.                 'product_id' => $Product->getId(),
  619.                 'product_class_id' => $addCartData['product_class_id'],
  620.                 'quantity' => $addCartData['quantity'],
  621.             ]
  622.         );
  623.         $event = new EventArgs(
  624.             [
  625.                 'form' => $form,
  626.                 'Product' => $Product,
  627.             ],
  628.             $request
  629.         );
  630.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE);
  631.         if ($event->getResponse() !== null) {
  632.             return $event->getResponse();
  633.         }
  634.         if ($request->isXmlHttpRequest()) {
  635.             // ajaxでのリクエストの場合は結果をjson形式で返す。
  636.             // 初期化
  637.             $messages = [];
  638.             if (empty($errorMessages)) {
  639.                 // エラーが発生していない場合
  640.                 $done true;
  641.                 array_push($messagestrans('front.product.add_cart_complete'));
  642.             } else {
  643.                 // エラーが発生している場合
  644.                 $done false;
  645.                 $messages $errorMessages;
  646.             }
  647.             return $this->json(['done' => $done'messages' => $messages]);
  648.         } else {
  649.             // ajax以外でのリクエストの場合はカート画面へリダイレクト
  650.             foreach ($errorMessages as $errorMessage) {
  651.                 $this->addRequestError($errorMessage);
  652.             }
  653.             return $this->redirectToRoute('cart');
  654.         }
  655.     }
  656.     /**
  657.      * ページタイトルの設定
  658.      *
  659.      * @param  array|null $searchData
  660.      *
  661.      * @return str
  662.      */
  663.     protected function getPageTitle($searchData)
  664.     {
  665.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  666.             return trans('front.product.search_result');
  667.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  668.             return $searchData['category_id']->getName();
  669.         } else {
  670.             return trans('front.product.all_products');
  671.         }
  672.     }
  673.     /**
  674.      * 閲覧可能な商品かどうかを判定
  675.      *
  676.      * @param Product $Product
  677.      *
  678.      * @return boolean 閲覧可能な場合はtrue
  679.      */
  680.     protected function checkVisibility(Product $Product)
  681.     {
  682.         $is_admin $this->session->has('_security_admin');
  683.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  684.         if (!$is_admin) {
  685.             // 在庫なし商品の非表示オプションが有効な場合.
  686.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  687.             //     if (!$Product->getStockFind()) {
  688.             //         return false;
  689.             //     }
  690.             // }
  691.             // 公開ステータスでない商品は表示しない.
  692.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  693.                 return false;
  694.             }
  695.         }
  696.         return true;
  697.     }
  698. //@##ADD START 2021-11-05 upseek
  699.     /**
  700.      * 棚の値段算出する.
  701.      *
  702.      * @Route("/products/price", name="product_price", methods={"POST"})
  703.      *
  704.      * @param Request $request
  705.      *
  706.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  707.      */
  708.     public function calcPrice(Request $request)
  709.     {
  710.         if (!$request->isXmlHttpRequest()) {
  711.             return $this->json(['status' => 'NG'], 500);
  712.         }
  713.         log_warning('calc shelving price.');
  714.         if (is_null($request)) {
  715.             log_error('cannot calc shelving price.');
  716.             return $this->json([], 404);
  717.         }
  718.         log_debug('validate for calc shelving price.');
  719.         $params json_decode($request->getContent());
  720.         $pattern $params->pattern ?? null;
  721.         $heightList $params->heightList ?? null;
  722.         $widthList $params->widthList ?? null;
  723.         $items $params->items ?? null;
  724.         $depth $params->depth ?? null;
  725.         $backPanel $params->backPanel ?? null;
  726.         $colorId $params->colorId ?? null;
  727.         $includeTax true;
  728.         if (!isset(
  729.             $pattern,
  730.             $heightList,
  731.             $widthList,
  732.             $items,
  733.             $depth,
  734.             $backPanel,
  735.             $colorId
  736.         )) {
  737.             log_error('invalid params for calc shelving price.');
  738.             return $this->json(['status' => 'NG'], 403);
  739.         }
  740.         try {
  741.             $price $this->calculatePriceService->calculate(
  742.                 $pattern,
  743.                 $heightList,
  744.                 $widthList,
  745.                 $items,
  746.                 $depth,
  747.                 $backPanel,
  748.                 $colorId,
  749.                 $includeTax
  750.             );
  751.         } catch (\RuntimeException $error) {
  752.             return $this->json(['status' => 'NG''message' => $error->getMessage()], 403);
  753.         } catch (\Throwable $th) {
  754.             // return $this->json(['status' => 'NG', 'message' => $th->getMessage()], 403);
  755.             return $this->json(['status' => 'NG'], 500);
  756.         }
  757.         return $this->json([
  758.             'price' => $price,
  759.         ]);
  760.     }
  761. //@##ADD END
  762. //@##ADD START 2021-11-08 upseek
  763.     /**
  764.      * シミュレーション画像を保存する
  765.      *
  766.      * @Route("/product/image/add", name="product_image_add", methods={"POST"})
  767.      */
  768.     public function addImage(Request $request)
  769.     {
  770.         if (!$request->isXmlHttpRequest()) {
  771.             throw new BadRequestHttpException();
  772.         }
  773.         $product_id $request->get('id');
  774.         $image_base64 $request->get('image');
  775.         $allowExtensions = ['gif''jpg''jpeg''png'];
  776.         $data_image_png_base64 'data:image/png;base64,';
  777.         $file '';
  778.         if($product_id && $image_base64 && substr($image_base640strlen($data_image_png_base64)) == $data_image_png_base64) {
  779.             $image str_replace($data_image_png_base64''$image_base64);
  780.             $image str_replace(' ''+'$image);
  781.             $image base64_decode($image);
  782.             $extension 'png';
  783.             $filename $product_id '_' date('mdHis').uniqid('_') . '.' $extension;
  784.             file_put_contents($this->eccubeConfig['eccube_temp_shelving_image_dir'] . '/' $filename$image);
  785.             $file $filename;
  786.             log_warning('addImage product_id=[' $product_id '] filename=[' $filename ']');
  787.         } else {
  788.             log_error('addImage invalid parameter');
  789.         }
  790.         $event = new EventArgs(
  791.             [
  792. //                'image' => $image,
  793.                 'file' => $file,
  794.             ],
  795.             $request
  796.         );
  797. //        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_ADD_IMAGE_COMPLETE, $event);
  798.         $file $event->getArgument('file');
  799.         return $this->json(['file' => $file], 200);
  800.     }
  801. //@## ADD END
  802. }