src/Eccube/Controller/ProductController.php line 290

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\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\Entity\ProductClass;
  24. use Eccube\Entity\Category;
  25. use Eccube\Entity\ClassCategory;
  26. use Eccube\Entity\ClassName;
  27. use Eccube\Repository\ProductClassRepository;
  28. use Eccube\Repository\CustomerFavoriteProductRepository;
  29. use Eccube\Repository\CategoryRepository;
  30. use Eccube\Repository\Master\ProductListMaxRepository;
  31. use Customize\Repository\ProductImageAngleRepository;
  32. use Customize\Repository\ProductImageAngleSpRepository;
  33. use Customize\Repository\ProductImageColorRepository;
  34. use Customize\Repository\ProductImageColorSpRepository;
  35. use Eccube\Repository\ProductRepository;
  36. use Customize\Entity\ProductImageAngle;
  37. use Customize\Entity\ProductImageAngleSp;
  38. use Customize\Entity\ProductImageColor;
  39. use Customize\Entity\ProductImageColorSp;
  40. use Customize\Entity\ProductCategory;
  41. use Eccube\Service\CartService;
  42. use Eccube\Service\PurchaseFlow\PurchaseContext;
  43. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  44. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  45. use Knp\Component\Pager\Paginator;
  46. use Eccube\Repository\ClassNameRepository;
  47. use Eccube\Repository\ClassCategoryRepository;
  48. use Eccube\Repository\ProductCategoryRepository;
  49. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  50. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  51. use Symfony\Component\HttpFoundation\Request;
  52. use Symfony\Component\HttpFoundation\Response;
  53. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  54. use Symfony\Component\Routing\Annotation\Route;
  55. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  56. class ProductController extends AbstractController
  57. {
  58.     /**
  59.      * @var PurchaseFlow
  60.      */
  61.     protected $purchaseFlow;
  62.     /**
  63.      * @var CustomerFavoriteProductRepository
  64.      */
  65.     protected $customerFavoriteProductRepository;
  66.     /**
  67.      * @var ProductClassRepository
  68.      */
  69.     protected $productClassRepository;
  70.     /**
  71.      * @var CartService
  72.      */
  73.     protected $cartService;
  74.     /**
  75.      * @var CategoryRepository
  76.      */
  77.     protected $categoryRepository;
  78.     /**
  79.      * @var ClassCategoryRepository
  80.      */
  81.     protected $classCategoryRepository;
  82.     /**
  83.      * @var ClassNameRepository
  84.      */
  85.     protected $classNameRepository;
  86.     /**
  87.      * @var ProductRepository
  88.      */
  89.     protected $productRepository;
  90.     /**
  91.      * @var ProductImageAngleRepository
  92.      */
  93.     protected $productImageAngleRepository;
  94.     /**
  95.      * @var ProductImageAngleSpRepository
  96.      */
  97.     protected $productImageAngleSpRepository;
  98.     /**
  99.      * @var ProductImageColorRepository
  100.      */
  101.     protected $productImageColorRepository;
  102.     /**
  103.      * @var ProductImageColorSpRepository
  104.      */
  105.     protected $productImageColorSpRepository;
  106.     /**
  107.      * @var ProductCategoryRepository
  108.      */
  109.     protected $productCategoryaRepository;
  110.     /**
  111.      * @var BaseInfo
  112.      */
  113.     protected $BaseInfo;
  114.     /**
  115.      * @var AuthenticationUtils
  116.      */
  117.     protected $helper;
  118.     /**
  119.      * @var ProductListMaxRepository
  120.      */
  121.     protected $productListMaxRepository;
  122.     private $title '';
  123.     /**
  124.      * ProductController constructor.
  125.      *
  126.      * @param PurchaseFlow $cartPurchaseFlow
  127.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  128.      * @param CartService $cartService
  129.      * @param ProductRepository $productRepository
  130.      * @param ProductImageAngleRepository $productImageAngleRepository
  131.      * @param ProductImageAngleSpRepository $productImageAngleSpRepository
  132.      * @param ProductImageColorRepository $productImageColorRepository
  133.      * @param ProductImageColorSpRepository $productImageColorspRepository
  134.      * @param ProductCategoryRepository $productCategoryRepository
  135.      * @param BaseInfoRepository $baseInfoRepository
  136.      * @param CategoryRepository $categoryRepository
  137.      * @param AuthenticationUtils $helper
  138.      * @param ClassCategoryRepository $classCategoryRepository
  139.      * @param ClassNameRepository $classNameRepository
  140.      * @param ProductListMaxRepository $productListMaxRepository
  141.      * @param ProductClassRepository $productClassRepository
  142.      */
  143.     public function __construct(
  144.         PurchaseFlow $cartPurchaseFlow,
  145.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  146.         CartService $cartService,
  147.         ProductRepository $productRepository,
  148.         BaseInfoRepository $baseInfoRepository,
  149.         ProductImageAngleRepository $productImageAngleRepository,
  150.         ProductImageAngleSpRepository $productImageAngleSpRepository,
  151.         ClassCategoryRepository $classCategoryRepository,
  152.         CategoryRepository $categoryRepository,
  153.         ClassNameRepository $classNameRepository,
  154.         ProductClassRepository $productClassRepository,
  155.         ProductImageColorRepository $productImageColorRepository,
  156.         ProductImageColorSpRepository $productImageColorSpRepository,
  157.         ProductCategoryRepository $productCategoryRepository,
  158.         AuthenticationUtils $helper,
  159.         ProductListMaxRepository $productListMaxRepository
  160.     ) {
  161.         $this->purchaseFlow $cartPurchaseFlow;
  162.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  163.         $this->productClassRepository $productClassRepository;
  164.         $this->classCategoryRepository $classCategoryRepository;
  165.         $this->classNameRepository $classNameRepository;
  166.         $this->categoryRepository $categoryRepository;
  167.         $this->cartService $cartService;
  168.         $this->productRepository $productRepository;
  169.         $this->productImageAngleRepository $productImageAngleRepository;
  170.         $this->productImageAngleSpRepository $productImageAngleSpRepository;
  171.         $this->productImageColorRepository $productImageColorRepository;
  172.         $this->productImageColorSpRepository $productImageColorSpRepository;
  173.         $this->productCategoryRepository $productCategoryRepository;
  174.         $this->BaseInfo $baseInfoRepository->get();
  175.         $this->helper $helper;
  176.         $this->productListMaxRepository $productListMaxRepository;
  177.     }
  178.     /**
  179.      * 商品一覧画面.
  180.      *
  181.      * @Route("/products/list", name="products_all")
  182.      * @Template("Product/index.twig")
  183.      */
  184.      public function index(Request $requestPaginator $paginator)
  185.      {
  186.         $all_items $this->productRepository->findBy(array());
  187.         // Dump($all_items);
  188.         // foreach ($all_items as $product){
  189.         //     $colors_product = $this->productClassRepository->findBy(['Product' => $Product]);
  190.         //     foreach ($colors_product as $one_class){
  191.         //         $one_class->getClassCategory1
  192.         //     }
  193.         // }
  194.         $categories $this->categoryRepository->findBy(array());
  195.         $category_array = array();
  196.         $parents_all_dic = array();
  197.         foreach ($categories as $category){
  198.             $parent $category->getParent();
  199.             // Dump($parent);
  200.             // exit;
  201.             // $parent = $parent->getId();
  202.             if ($parent != null){
  203.                 $parent $parent->getId();
  204.                 $child $category->getId();
  205.                 array_push($category_array[$parent], $child);
  206.             }else{
  207.                 $category_array[$category->getId()] = array();
  208.             }
  209.             $parents_all_dic[$category->getId()] = $category;
  210.         }
  211.         $all_class_array = array();
  212.         foreach ($all_items as $Product){
  213.             $key $Product->getId();
  214.             // if (!array_key_exists($key, $all_class_array)){
  215.             //     $all_class_array[$key] = array();
  216.             // }
  217.             $class_array = array();
  218.             $diffrent_products $this->productClassRepository->findBy(array('Product' => $Product'visible' => 1));
  219.             $class_name $this->classNameRepository->findOneBy(array('name' => 'color'));
  220.             $class_cates $this->classCategoryRepository->findBy(array('ClassName' => $class_name));
  221.             $product_class_1 $this->productClassRepository->findBy(array('ClassCategory1' => $class_cates'Product' => $Product'visible' => 1));
  222.             $product_class_2 $this->productClassRepository->findBy(array('ClassCategory2' => $class_cates'Product' => $Product'visible' => 1));
  223.             // Dump($product_class_1);
  224.             // Dump($product_class_2);
  225.             foreach ($product_class_1 as $product_class){
  226.               $class_cate1 $product_class->getClassCategory1();
  227.               if (!in_array($class_cate1$class_array)){
  228.                   Dump($class_cate1);
  229.                  // exit;
  230.                 array_push($class_array$class_cate1);
  231.               }
  232.             }
  233.             foreach ($product_class_2 as $product_class){
  234.               $class_cate2 $product_class->getClassCategory2();
  235.               if (!in_array($class_cate2$class_array)){
  236.                 array_push($class_array$class_cate2);
  237.               }
  238.             }
  239.             $all_class_array[$key] = $class_array;
  240.         }
  241.         return [
  242.             'Products' => $all_items,
  243.             'Category_relation' => $category_array,
  244.             'Categories' => $parents_all_dic,
  245.             'Colors' => $all_class_array
  246.         ];
  247.      }
  248.      /**
  249.       * 商品一覧画面.
  250.       *
  251.       * @Route("/products", name="product_list")
  252.       * @Template("Product/index.twig")
  253.       */
  254.      public function list(Request $requestPaginator $paginator)
  255.      {
  256.          // Doctrine SQLFilter
  257.          if ($this->BaseInfo->isOptionNostockHidden()) {
  258.              $this->entityManager->getFilters()->enable('option_nostock_hidden');
  259.          }
  260.          // handleRequestは空のqueryの場合は無視するため
  261.          if ($request->getMethod() === 'GET') {
  262.              $request->query->set('pageno'$request->query->get('pageno'''));
  263.          }
  264.          // searchForm
  265.          /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  266.          $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  267.          if ($request->getMethod() === 'GET') {
  268.              $builder->setMethod('GET');
  269.          }
  270.          $event = new EventArgs(
  271.              [
  272.                  'builder' => $builder,
  273.              ],
  274.              $request
  275.          );
  276.          $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  277.          /* @var $searchForm \Symfony\Component\Form\FormInterface */
  278.          $searchForm $builder->getForm();
  279.          $searchForm->handleRequest($request);
  280.          // paginator
  281.          $searchData $searchForm->getData();
  282.          $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  283.          $event = new EventArgs(
  284.              [
  285.                  'searchData' => $searchData,
  286.                  'qb' => $qb,
  287.              ],
  288.              $request
  289.          );
  290.          $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  291.          $searchData $event->getArgument('searchData');
  292.          $query $qb->getQuery()
  293.              ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  294.          //$ProductsAndClassCategories = $this->productRepository->findProductsBySearchData(null, 'p.id');
  295.          $categories $this->categoryRepository->findBy(array());
  296.          $category_array = array();
  297.          $parents_all_dic = array();
  298.          foreach ($categories as $category){
  299.              $parent $category->getParent();
  300.              if ($parent != null){
  301.                  $parent $parent->getId();
  302.                  $child $category->getId();
  303.                  array_push($category_array[$parent], $child);
  304.              }else{
  305.                  $category_array[$category->getId()] = array();
  306.              }
  307.              $parents_all_dic[$category->getId()] = $category;
  308.          }
  309.          $all_class_array = array();
  310.          $list_image_array = array();
  311.          // foreach ($ProductsAndClassCategories as $Product){
  312.          //     $key = $Product->getId();
  313.          //
  314.          //     $class_array = array();
  315.          //     $diffrent_products = $this->productClassRepository->findBy(array('Product' => $Product, 'visible' => 1));
  316.          //     $class_name = $this->classNameRepository->findOneBy(array('name' => 'color'));
  317.          //
  318.          //     $class_cates = $this->classCategoryRepository->findBy(array('ClassName' => $class_name));
  319.          //     $product_class_1 = $this->productClassRepository->findBy(array('ClassCategory1' => $class_cates, 'Product' => $Product, 'visible' => 1));
  320.          //     $product_class_2 = $this->productClassRepository->findBy(array('ClassCategory2' => $class_cates, 'Product' => $Product, 'visible' => 1));
  321.          //
  322.          //     foreach ($product_class_1 as $product_class){
  323.          //       $class_cate1 = $product_class->getClassCategory1();
  324.          //       if (!in_array($class_cate1, $class_array)){
  325.          //         array_push($class_array, $class_cate1);
  326.          //       }
  327.          //     }
  328.          //
  329.          //
  330.          //     foreach ($product_class_2 as $product_class){
  331.          //       $class_cate2 = $product_class->getClassCategory2();
  332.          //       if (!in_array($class_cate2, $class_array)){
  333.          //         array_push($class_array, $class_cate2);
  334.          //       }
  335.          //     }
  336.          //
  337.          //     $all_class_array[$key] = $class_array;
  338.          //
  339.          //     $list_image = $this->productImageAngleRepository->findOneBy(['Product' => $Product, 'angle' => 'list']);
  340.          //     $list_image_array[$key] = $list_image;
  341.          //
  342.          // }
  343.          $rootCategory $this->getRootCategory();
  344.          $all_colors $this->getAllClassCategories('color');
  345.          $all_sizes $this->getAllClassCategories('size');
  346.          // Dump($ProductsAndClassCategories[134]);
  347.          // exit;
  348.          return [
  349.              'Category_relation' => $category_array,
  350.              'rootCategories' => $rootCategory,
  351.              'all_colors' => $all_colors,
  352.              'all_sizes' => $all_sizes,
  353.              'Categories' => $parents_all_dic,
  354.          ];
  355.      }
  356.      public function getAllClassCategories($class)
  357.      {
  358.          $class_name $this->classNameRepository->findOneBy(array('name' => $class));
  359.          $all_class_categories $this->classCategoryRepository->findBy(array('ClassName' => $class_name));
  360.          return $all_class_categories;
  361.      }
  362.      /**
  363.       * 商品一覧画面.
  364.       *
  365.       * @Route("/products/list_api", name="product_list_api")
  366.       * @Template("Product/Product_items.twig")
  367.       */
  368.      public function list_api(Request $requestPaginator $paginator)
  369.      {
  370.          //?category_id=23とかで絞れる
  371.          //?detail_flg=1 or 0
  372.          //if: detail_flg ==1, then it means it came from products/detail
  373.          // $detail_flg = $request->query->get('detail_flg');
  374.          //
  375.          // //special_flg is 1 if it receive ajax from /special
  376.          // $special_flg = $request->query->get('special_flg');
  377.          // Doctrine SQLFilter
  378.          if ($this->BaseInfo->isOptionNostockHidden()) {
  379.              $this->entityManager->getFilters()->enable('option_nostock_hidden');
  380.          }
  381.          // handleRequestは空のqueryの場合は無視するため
  382.          if ($request->getMethod() === 'GET') {
  383.              $request->query->set('pageno'$request->query->get('pageno'''));
  384.          }
  385.          // searchForm
  386.          /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  387.          $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  388.          if ($request->getMethod() === 'GET') {
  389.              $builder->setMethod('GET');
  390.          }
  391.          $event = new EventArgs(
  392.              [
  393.                  'builder' => $builder,
  394.              ],
  395.              $request
  396.          );
  397.          $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  398.          /* @var $searchForm \Symfony\Component\Form\FormInterface */
  399.          $searchForm $builder->getForm();
  400.          $searchForm->handleRequest($request);
  401.          // paginator
  402.          $searchData $searchForm->getData();
  403.          $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  404.          $event = new EventArgs(
  405.              [
  406.                  'searchData' => $searchData,
  407.                  'qb' => $qb,
  408.              ],
  409.              $request
  410.          );
  411.          $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  412.          $searchData $event->getArgument('searchData');
  413.          $query $qb->getQuery()
  414.              ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  415.         $price $request->query->get('price');
  416.         $category $request->query->get('category');
  417.         $color $request->query->get('color');
  418.         $searchData = array();
  419.         if ($category !== 'all'){
  420.             $searchData['category'] = $category;
  421.         }
  422.         if ($price !== null){
  423.             $searchData['price'] = $price;
  424.         }
  425.         if ($color !== null){
  426.             $searchData['color'] = $color;
  427.         }
  428.          $ProductsAndClassCategories $this->productRepository->findProductsBySearchData($searchData);
  429.          $categories $this->categoryRepository->findBy(array());
  430.          $category_array = array();
  431.          $parents_all_dic = array();
  432.          foreach ($categories as $category){
  433.              $parent $category->getParent();
  434.              // Dump($parent);
  435.              // exit;
  436.              // $parent = $parent->getId();
  437.              if ($parent != null){
  438.                  $parent $parent->getId();
  439.                  $child $category->getId();
  440.                  array_push($category_array[$parent], $child);
  441.              }else{
  442.                  $category_array[$category->getId()] = array();
  443.              }
  444.              $parents_all_dic[$category->getId()] = $category;
  445.          }
  446.          //Dump($ProductsAndClassCategories);
  447.          //exit;
  448.          $all_class_array = array();
  449.          foreach ($ProductsAndClassCategories as $Product){
  450.              $key $Product->getId();
  451.              $class_array = array();
  452.              $diffrent_products $this->productClassRepository->findBy(array('Product' => $Product'visible' => 1));
  453.              $class_name $this->classNameRepository->findOneBy(array('name' => 'color'));
  454.              $class_cates $this->classCategoryRepository->findBy(array('ClassName' => $class_name));
  455.              $product_class_1 $this->productClassRepository->findBy(array('ClassCategory1' => $class_cates'Product' => $Product'visible' => 1));
  456.              $product_class_2 $this->productClassRepository->findBy(array('ClassCategory2' => $class_cates'Product' => $Product'visible' => 1));
  457.              // Dump($product_class_1);
  458.              // Dump($product_class_2);
  459.              foreach ($product_class_1 as $product_class){
  460.                $class_cate1 $product_class->getClassCategory1();
  461.                if (!in_array($class_cate1$class_array)){
  462.                    //Dump($class_cate1);
  463.                   // exit;
  464.                  array_push($class_array$class_cate1);
  465.                }
  466.              }
  467.              foreach ($product_class_2 as $product_class){
  468.                $class_cate2 $product_class->getClassCategory2();
  469.                if (!in_array($class_cate2$class_array)){
  470.                  array_push($class_array$class_cate2);
  471.                }
  472.              }
  473.              $all_class_array[$key] = $class_array;
  474.          }
  475.          $new_products = array();
  476.          foreach ($ProductsAndClassCategories as $product){
  477.              $default_item $this->productImageColorRepository->findOneBy(['Product' => $product'default' => 1]);
  478.              $list_image $this->productImageAngleRepository->findOneBy(['Product' => $product'angle' => 'list']);
  479.             $filename '';
  480.             $default_color '';
  481.              if ($default_item != null){
  482.                  $filename $default_item->getFileName();
  483.                  $default_color $default_item->getColor();
  484.              }
  485.              $default_color_name '';
  486.              if ($default_color !== null){
  487.                  $default_color_name $this->classCategoryRepository->find($default_color);
  488.              }
  489.              $new_product = array(
  490.                  'id' => $product->getId(),
  491.                  'name' => $product->getName(),
  492.                  'product_name_en' => $product->getProductNameEn(),
  493.                  'styleno' => $product->getStyleNo(),
  494.                  'photo_path' => $filename,
  495.                  'default_color' => $default_color,
  496.                  'default_color_name' => $default_color_name,
  497.                  'list_image' => $list_image,
  498.                  'tags' => $product->getTags(),
  499.                  'is_sale' => $product->getIsSale()
  500.              );
  501.              array_push($new_products$new_product);
  502.          }
  503.          $source $request->query->get('source'); //どこのurlから飛んできたか
  504.          // Dump($detail_flg);
  505.          //exit;
  506.          if ($source == 'products_list'){
  507.              return [
  508.                  'Products' => $new_products,
  509.                  'Colors' => $all_class_array,
  510.                  'source' => $source
  511.              ];
  512.          }else if ($source == 'product_detail'){
  513.              $new_products array_slice($new_products,07);
  514.              return [
  515.                  'Products' => $new_products,
  516.                  'source' => $source
  517.              ];
  518.          }else if ($source == 'special'){
  519.              return [
  520.                  'Products' => $new_products,
  521.                  'source' => $source
  522.              ];
  523.          }
  524.      }
  525.      //ルートカテゴリを取得する関数
  526.      public function getRootCategory(){
  527.          $rootCategory $this->categoryRepository->findBy(array('Parent' => null));
  528.          return $rootCategory;
  529.      }
  530.      /**
  531.       * 子カテゴリを取得するapi
  532.       *
  533.       * @Route("/products/getCategoryChildren", name="get_ategory_children")
  534.       * @Template("Product/categoryChildren.twig")
  535.       */
  536.      public function getCategoryChildren(Request $request)
  537.      {
  538.          $category_id $request->query->get('parent_id');
  539.          $category $this->categoryRepository->findOneBy(array('id' => $category_id));
  540.          $children $category->getChildren();
  541.          return [
  542.             'children' => $children
  543.          ];
  544.      }
  545.     /**
  546.      * 商品一覧画面.
  547.      *
  548.      * @Route("/products/___list", name="product_li")
  549.      * @Template("Product/list.twig")
  550.      */
  551.     public function aaa(Request $requestPaginator $paginator)
  552.     {
  553.         // Doctrine SQLFilter
  554.         if ($this->BaseInfo->isOptionNostockHidden()) {
  555.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  556.         }
  557.         // handleRequestは空のqueryの場合は無視するため
  558.         if ($request->getMethod() === 'GET') {
  559.             $request->query->set('pageno'$request->query->get('pageno'''));
  560.         }
  561.         // searchForm
  562.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  563.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  564.         if ($request->getMethod() === 'GET') {
  565.             $builder->setMethod('GET');
  566.         }
  567.         $event = new EventArgs(
  568.             [
  569.                 'builder' => $builder,
  570.             ],
  571.             $request
  572.         );
  573.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  574.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  575.         $searchForm $builder->getForm();
  576.         $searchForm->handleRequest($request);
  577.         // paginator
  578.         $searchData $searchForm->getData();
  579.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  580.         $event = new EventArgs(
  581.             [
  582.                 'searchData' => $searchData,
  583.                 'qb' => $qb,
  584.             ],
  585.             $request
  586.         );
  587.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  588.         $searchData $event->getArgument('searchData');
  589.         $query $qb->getQuery()
  590.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  591.         /** @var SlidingPagination $pagination */
  592.         $pagination $paginator->paginate(
  593.             $query,
  594.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  595.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  596.         );
  597.         $ids = [];
  598.         foreach ($pagination as $Product) {
  599.             $ids[] = $Product->getId();
  600.         }
  601.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  602.         Dump($ProductsAndClassCategories);
  603.         exit;
  604.         // addCart form
  605.         $forms = [];
  606.         foreach ($pagination as $Product) {
  607.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  608.             $builder $this->formFactory->createNamedBuilder(
  609.                 '',
  610.                 AddCartType::class,
  611.                 null,
  612.                 [
  613.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  614.                     'allow_extra_fields' => true,
  615.                 ]
  616.             );
  617.             $addCartForm $builder->getForm();
  618.             $forms[$Product->getId()] = $addCartForm->createView();
  619.         }
  620.         // 表示件数
  621.         $builder $this->formFactory->createNamedBuilder(
  622.             'disp_number',
  623.             ProductListMaxType::class,
  624.             null,
  625.             [
  626.                 'required' => false,
  627.                 'allow_extra_fields' => true,
  628.             ]
  629.         );
  630.         if ($request->getMethod() === 'GET') {
  631.             $builder->setMethod('GET');
  632.         }
  633.         $event = new EventArgs(
  634.             [
  635.                 'builder' => $builder,
  636.             ],
  637.             $request
  638.         );
  639.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_DISP$event);
  640.         $dispNumberForm $builder->getForm();
  641.         $dispNumberForm->handleRequest($request);
  642.         // ソート順
  643.         $builder $this->formFactory->createNamedBuilder(
  644.             'orderby',
  645.             ProductListOrderByType::class,
  646.             null,
  647.             [
  648.                 'required' => false,
  649.                 'allow_extra_fields' => true,
  650.             ]
  651.         );
  652.         if ($request->getMethod() === 'GET') {
  653.             $builder->setMethod('GET');
  654.         }
  655.         $event = new EventArgs(
  656.             [
  657.                 'builder' => $builder,
  658.             ],
  659.             $request
  660.         );
  661.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_ORDER$event);
  662.         $orderByForm $builder->getForm();
  663.         $orderByForm->handleRequest($request);
  664.         $Category $searchForm->get('category_id')->getData();
  665.         return [
  666.             'subtitle' => $this->getPageTitle($searchData),
  667.             'pagination' => $pagination,
  668.             'search_form' => $searchForm->createView(),
  669.             'disp_number_form' => $dispNumberForm->createView(),
  670.             'order_by_form' => $orderByForm->createView(),
  671.             'forms' => $forms,
  672.             'Category' => $Category,
  673.         ];
  674.     }
  675.     /**
  676.      * 商品詳細画面.
  677.      *
  678.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  679.      * @Template("Product/detail.twig")
  680.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  681.      *
  682.      * @param Request $request
  683.      * @param Product $Product
  684.      *
  685.      * @return array
  686.      */
  687.     public function detail(Request $requestProduct $Product)
  688.     {
  689.         if (!$this->checkVisibility($Product)) {
  690.             throw new NotFoundHttpException();
  691.         }
  692.         //tag確認
  693.         // Dump($Product->getTags()[0]->getName());
  694.         // exit;
  695.         $builder $this->formFactory->createNamedBuilder(
  696.             '',
  697.             AddCartType::class,
  698.             null,
  699.             [
  700.                 'product' => $Product,
  701.                 'id_add_product_id' => false,
  702.             ]
  703.         );
  704.         $event = new EventArgs(
  705.             [
  706.                 'builder' => $builder,
  707.                 'Product' => $Product,
  708.             ],
  709.             $request
  710.         );
  711.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE$event);
  712.         $is_favorite false;
  713.         if ($this->isGranted('ROLE_USER')) {
  714.             $Customer $this->getUser();
  715.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  716.         }
  717.         //Dump($Product);
  718.         // $color = $this->productImageColorRepository->findBy(['Product' => $Product]);
  719.         // Dump($color);
  720.         // exit;
  721.         $class_array = array();
  722.         //どんな色があるか調べる
  723.         $diffrent_products $this->productClassRepository->findBy(array('Product' => $Product'visible' => 1));
  724.         $class_name $this->classNameRepository->findOneBy(array('name' => 'color'));
  725.         $class_cates $this->classCategoryRepository->findBy(array('ClassName' => $class_name));
  726.         $product_class_1 $this->productClassRepository->findBy(array('ClassCategory1' => $class_cates'Product' => $Product'visible' => 1));
  727.         $product_class_2 $this->productClassRepository->findBy(array('ClassCategory2' => $class_cates'Product' => $Product'visible' => 1));
  728.         foreach ($product_class_1 as $product_class){
  729.           $class_cate1 $product_class->getClassCategory1();
  730.           if (!in_array($class_cate1$class_array)){
  731.             array_push($class_array$class_cate1);
  732.           }
  733.         }
  734.         foreach ($product_class_2 as $product_class){
  735.           $class_cate2 $product_class->getClassCategory2();
  736.           if (!in_array($class_cate2$class_array)){
  737.             array_push($class_array$class_cate2);
  738.           }
  739.         }
  740.         //Dump($class_array);
  741.         $colors '';
  742.         $colors_imgs = array(
  743.             'default'=> array(),
  744.             'all' => array()
  745.         );
  746.         //スマホならば true
  747.         function is_mobile()
  748.         {
  749.             $user_agent $_SERVER['HTTP_USER_AGENT']; // HTTP ヘッダからユーザー エージェントの文字列を取り出す
  750.             return preg_match('/iphone|ipod|ipad|android/ui'$user_agent) != 0// 既知の判定用文字列を検索
  751.         }
  752.         //管理画面で、「色画像の順番」
  753.         if ($Product->getNote() !== null){
  754.             $color_orders explode(":"$Product->getNote());
  755.             foreach ($color_orders as $color){
  756.                 $upper_color strtoupper($color);
  757.                 $color $this->classCategoryRepository->findOneBy(['name' => $upper_color]);
  758.                 $color_id $color->getId();
  759.                 $filename '';
  760.                 if ($color_id !== 0){
  761.                     if (is_mobile() == true){
  762.                         $colorimage $this->productImageColorSpRepository->findOneBy(['Product' => $Product'color' => $color_id]);
  763.                     }else{
  764.                         $colorimage $this->productImageColorRepository->findOneBy(['Product' => $Product'color' => $color_id]);
  765.                     }
  766.                     if ($colorimage !== null){
  767.                         $filename $colorimage->getFileName();
  768.                     }
  769.                 }
  770.                 array_push($colors_imgs['all'],
  771.                     array(
  772.                         'class' => $color,
  773.                         'filename' => $filename
  774.                     )
  775.                 );
  776.             }
  777.         }
  778.         $default_item $this->productImageColorRepository->findOneBy(['Product' => $Product'default' => 1]);
  779.         if ($default_item != null){
  780.             $colors_imgs['default']['class'] = 1;
  781.             $colors_imgs['default']['filename'] = $default_item->getFileName();
  782.         }
  783.         if (count($class_array) !== 0){
  784.             $color_first $class_array[0];
  785.             $colors .= $color_first->getName();
  786.             $class_array array_slice($class_array1);
  787.             foreach ($class_array as $color){
  788.                 $name $color->getName();
  789.                 $colors .= ', '.$name;
  790.             }
  791.         }
  792.         $all_images_filename '';
  793.         $all_images $this->productImageColorRepository->findOneBy(['Product' => $Product'color' => 0]);
  794.         if ($all_images !== null){
  795.                 $all_images_filename $all_images->getFileName();
  796.         }
  797.         //angle 画像ファイルを取ってくる
  798.         $angle_imgs = array(
  799.             'front' => '',
  800.             'side' => '',
  801.             'top' => '',
  802.             'back' => '',
  803.             'detail1' => '',
  804.             'detail2' => '',
  805.             'model' => '',
  806.             'list' => ''
  807.         );
  808.         $image_angles $this->productImageAngleRepository->findBy(['Product' => $Product]);
  809.         foreach($image_angles as $img){
  810.             $angle $img->getAngle();
  811.             $filename $img->getFileName();
  812.             $angle_imgs[$angle] = $filename;
  813.         }
  814.         //angle sp 画像ファイルを取ってくる
  815.         $angleSp_imgs = array(
  816.             'front' => '',
  817.             'side' => '',
  818.             'top' => '',
  819.             'back' => '',
  820.             'detail1' => '',
  821.             'detail2' => '',
  822.             'model' => '',
  823.         );
  824.         $image_anglesSp $this->productImageAngleSpRepository->findBy(['Product' => $Product]);
  825.         foreach($image_anglesSp as $img){
  826.             $angle $img->getAngle();
  827.             $filename $img->getFileName();
  828.             $angleSp_imgs[$angle] = $filename;
  829.         }
  830.         //関連するカテゴリ
  831.         $categories $this->productCategoryRepository->findBy(['Product' => $Product]);
  832.         //Dump($aaa);
  833.         $close_cat '';
  834.         foreach($categories as $cat){
  835.             //Dump($key->getCategory()->getChildren());
  836.             $parent_cat $cat->getCategory()->getParents();
  837.             if (count($parent_cat) != 0){
  838.                 $close_cat $cat->getCategory();
  839.             }
  840.         }
  841.         
  842.         //これでサーバーと通信して商品一覧を取ってくる
  843.         return [
  844.             'title' => $this->title,
  845.             'subtitle' => $Product->getName(),
  846.             'form' => $builder->getForm()->createView(),
  847.             'Product' => $Product,
  848.             'is_favorite' => $is_favorite,
  849.             'colors' => $colors,
  850.             'angles' => $angle_imgs,
  851.             'anglesSp' => $angleSp_imgs,
  852.             'all_images' => $all_images_filename,
  853.             'colors_imgs' => $colors_imgs,
  854.             'close_cat' => $close_cat
  855.         ];
  856.     }
  857.     /**
  858.      * お気に入り追加.
  859.      *
  860.      * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"})
  861.      */
  862.     public function addFavorite(Request $requestProduct $Product)
  863.     {
  864.         $this->checkVisibility($Product);
  865.         $event = new EventArgs(
  866.             [
  867.                 'Product' => $Product,
  868.             ],
  869.             $request
  870.         );
  871.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE$event);
  872.         if ($this->isGranted('ROLE_USER')) {
  873.             $Customer $this->getUser();
  874.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product);
  875.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  876.             $event = new EventArgs(
  877.                 [
  878.                     'Product' => $Product,
  879.                 ],
  880.                 $request
  881.             );
  882.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  883.             return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  884.         } else {
  885.             // 非会員の場合、ログイン画面を表示
  886.             //  ログイン後の画面遷移先を設定
  887.             $this->setLoginTargetPath($this->generateUrl('product_add_favorite', ['id' => $Product->getId()]));
  888.             $this->session->getFlashBag()->set('eccube.add.favorite'true);
  889.             $event = new EventArgs(
  890.                 [
  891.                     'Product' => $Product,
  892.                 ],
  893.                 $request
  894.             );
  895.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  896.             return $this->redirectToRoute('mypage_login');
  897.         }
  898.     }
  899.     /**
  900.      * カートに追加.
  901.      *
  902.      * @Route("/products/add_cart/{id}", name="product_add_cart", methods={"POST"}, requirements={"id" = "\d+"})
  903.      */
  904.     public function addCart(Request $requestProduct $Product)
  905.     {
  906.         // エラーメッセージの配列
  907.         $errorMessages = [];
  908.         if (!$this->checkVisibility($Product)) {
  909.             throw new NotFoundHttpException();
  910.         }
  911.         $builder $this->formFactory->createNamedBuilder(
  912.             '',
  913.             AddCartType::class,
  914.             null,
  915.             [
  916.                 'product' => $Product,
  917.                 'id_add_product_id' => false,
  918.             ]
  919.         );
  920.         $event = new EventArgs(
  921.             [
  922.                 'builder' => $builder,
  923.                 'Product' => $Product,
  924.             ],
  925.             $request
  926.         );
  927.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE$event);
  928.         /* @var $form \Symfony\Component\Form\FormInterface */
  929.         $form $builder->getForm();
  930.         $form->handleRequest($request);
  931.         if (!$form->isValid()) {
  932.             throw new NotFoundHttpException();
  933.         }
  934.         $addCartData $form->getData();
  935.         log_info(
  936.             'カート追加処理開始',
  937.             [
  938.                 'product_id' => $Product->getId(),
  939.                 'product_class_id' => $addCartData['product_class_id'],
  940.                 'quantity' => $addCartData['quantity'],
  941.             ]
  942.         );
  943.         // カートへ追加
  944.         $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']);
  945.         // 明細の正規化
  946.         $Carts $this->cartService->getCarts();
  947.         foreach ($Carts as $Cart) {
  948.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  949.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  950.             if ($result->hasError()) {
  951.                 $this->cartService->removeProduct($addCartData['product_class_id']);
  952.                 foreach ($result->getErrors() as $error) {
  953.                     $errorMessages[] = $error->getMessage();
  954.                 }
  955.             }
  956.             foreach ($result->getWarning() as $warning) {
  957.                 $errorMessages[] = $warning->getMessage();
  958.             }
  959.         }
  960.         $this->cartService->save();
  961.         log_info(
  962.             'カート追加処理完了',
  963.             [
  964.                 'product_id' => $Product->getId(),
  965.                 'product_class_id' => $addCartData['product_class_id'],
  966.                 'quantity' => $addCartData['quantity'],
  967.             ]
  968.         );
  969.         $event = new EventArgs(
  970.             [
  971.                 'form' => $form,
  972.                 'Product' => $Product,
  973.             ],
  974.             $request
  975.         );
  976.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE$event);
  977.         if ($event->getResponse() !== null) {
  978.             return $event->getResponse();
  979.         }
  980.         if ($request->isXmlHttpRequest()) {
  981.             // ajaxでのリクエストの場合は結果をjson形式で返す。
  982.             // 初期化
  983.             $done null;
  984.             $messages = [];
  985.             if (empty($errorMessages)) {
  986.                 // エラーが発生していない場合
  987.                 $done true;
  988.                 array_push($messagestrans('front.product.add_cart_complete'));
  989.             } else {
  990.                 // エラーが発生している場合
  991.                 $done false;
  992.                 $messages $errorMessages;
  993.             }
  994.             return $this->json(['done' => $done'messages' => $messages]);
  995.         } else {
  996.             // ajax以外でのリクエストの場合はカート画面へリダイレクト
  997.             foreach ($errorMessages as $errorMessage) {
  998.                 $this->addRequestError($errorMessage);
  999.             }
  1000.             return $this->redirectToRoute('cart');
  1001.         }
  1002.     }
  1003.     public function defaultColorFromProduct($Product)
  1004.     {
  1005.         $default_item $this->productImageColorRepository->findOneBy(['Product' => $Product'default' => 1]);
  1006.         if ($default_item == null){
  1007.         }else{
  1008.             $default_color $default_item->getColor();
  1009.             $default_color_photo_url $default_item->getFileName();
  1010.         }
  1011.     }
  1012.     /**
  1013.      * ページタイトルの設定
  1014.      *
  1015.      * @param  null|array $searchData
  1016.      *
  1017.      * @return str
  1018.      */
  1019.     protected function getPageTitle($searchData)
  1020.     {
  1021.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  1022.             return trans('front.product.search_result');
  1023.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  1024.             return $searchData['category_id']->getName();
  1025.         } else {
  1026.             return trans('front.product.all_products');
  1027.         }
  1028.     }
  1029.     /**
  1030.      * 閲覧可能な商品かどうかを判定
  1031.      *
  1032.      * @param Product $Product
  1033.      *
  1034.      * @return boolean 閲覧可能な場合はtrue
  1035.      */
  1036.     protected function checkVisibility(Product $Product)
  1037.     {
  1038.         $is_admin $this->session->has('_security_admin');
  1039.         // Dump($is_admin);
  1040.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  1041.         // if (!$is_admin) {
  1042.         //     // 在庫なし商品の非表示オプションが有効な場合.
  1043.         //     // if ($this->BaseInfo->isOptionNostockHidden()) {
  1044.         //     //     if (!$Product->getStockFind()) {
  1045.         //     //         return false;
  1046.         //     //     }
  1047.         //     // }
  1048.         //     // 公開ステータスでない商品は表示しない.
  1049.         //
  1050.         //     if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  1051.         //         return false;
  1052.         //     }
  1053.         // }
  1054.         if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  1055.             return false;
  1056.         }
  1057.         return true;
  1058.     }
  1059. }