app/Customize/Controller/BrandController.php line 174

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 Customize\Repository\AllergenRepository;
  14. use Eccube\Common\Constant;
  15. use Eccube\Controller\AbstractController;
  16. use Eccube\Entity\Master\ProductStatus;
  17. use Eccube\Repository\CategoryRepository;
  18. use Eccube\Repository\ProductRepository;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. class BrandController extends AbstractController
  25. {
  26.     /**
  27.      * @var ProductRepository
  28.      */
  29.     private ProductRepository $productRepository;
  30.     /**
  31.      * @var CategoryRepository
  32.      */
  33.     private CategoryRepository $categoryRepository;
  34.     /**
  35.      * @var AllergenRepository
  36.      */
  37.     private AllergenRepository $allergenRepository;
  38.     /**
  39.      * CategoryController constructor.
  40.      */
  41.     public function __construct(
  42.         CategoryRepository $categoryRepository,
  43.         ProductRepository $productRepository,
  44.         AllergenRepository $allergenRepository
  45.     ) {
  46.         $this->categoryRepository $categoryRepository;
  47.         $this->productRepository $productRepository;
  48.         $this->allergenRepository $allergenRepository;
  49.     }
  50.     /**
  51.      * ブランドラインナップ
  52.      *
  53.      * @Route("/brand", name="brand")
  54.      * @Template("Brand/brand.twig")
  55.      */
  56.     public function index()
  57.     {
  58.         // アレルゲン項目の取得
  59.         $allergen $this->allergenRepository->getLabelList();
  60.         return [
  61.             'allergen' => $allergen,
  62.         ];
  63.     }
  64.     /**
  65.      * 商品一覧(ニュータッチ)
  66.      *
  67.      * @Route("/brand/newtouch", name="brand_newtouch")
  68.      * @Template("Brand/newtouch.twig")
  69.      */
  70.     public function newtouch()
  71.     {
  72.         return $this->getBrandPageData('newtouch');
  73.     }
  74.     /**
  75.      * 商品詳細(ニュータッチ)
  76.      *
  77.      * @Route("/brand/newtouch/{id}", name="brand_newtouchdetail")
  78.      */
  79.     public function newtouchDetail($id)
  80.     {
  81.         return $this->detail('newtouch'$id);
  82.     }
  83.     /**
  84.      * 商品一覧(凄麺)
  85.      *
  86.      * @Route("/brand/sugomen", name="brand_sugomen")
  87.      * @Template("Brand/sugomen.twig")
  88.      */
  89.     public function sugomen()
  90.     {
  91.         return $this->getBrandPageData('sugomen');
  92.     }
  93.     /**
  94.      * 商品詳細(凄麺)
  95.      *
  96.      * @Route("/brand/sugomen/{id}", name="brand_sugomendetail")
  97.      */
  98.     public function sugomenDetail($id)
  99.     {
  100.         return $this->detail('sugomen'$id);
  101.     }
  102.     /**
  103.      * 商品一覧(手緒里庵)
  104.      *
  105.      * @Route("/brand/teorian", name="brand_teorian")
  106.      * @Template("Brand/teorian.twig")
  107.      */
  108.     public function teorian()
  109.     {
  110.         return $this->getBrandPageData('teorian');
  111.     }
  112.     /**
  113.      * 商品詳細(手緒里庵)
  114.      *
  115.      * @Route("/brand/teorian/{id}", name="brand_teoriandetail")
  116.      */
  117.     public function teorianDetail($id)
  118.     {
  119.         return $this->detail('teorian'$id);
  120.     }
  121.     /**
  122.      * 商品一覧(スープ・デ・パスタ)
  123.      *
  124.      * @Route("/brand/soupdepasta", name="brand_soupdepasta")
  125.      * @Template("Brand/soupdepasta.twig")
  126.      */
  127.     public function soupdepasta()
  128.     {
  129.         return $this->getBrandPageData('soupdepasta');
  130.     }
  131.     /**
  132.      * 商品詳細(スープ・デ・パスタ)
  133.      *
  134.      * @Route("/brand/soupdepasta/{id}", name="brand_soupdepastadetail")
  135.      */
  136.     public function soupdepastaDetail($id)
  137.     {
  138.         return $this->detail('soupdepasta'$id);
  139.     }
  140.     /**
  141.      * 商品一覧(手緒里めん)
  142.      *
  143.      * @Route("/brand/teorimen", name="brand_teorimen")
  144.      * @Template("Brand/teorimen.twig")
  145.      */
  146.     public function teorimen()
  147.     {
  148.         return $this->getBrandPageData('teorimen');
  149.     }
  150.     /**
  151.      * 商品詳細(手緒里めん)
  152.      *
  153.      * @Route("/brand/teorimen/{id}", name="brand_teorimendetail")
  154.      */
  155.     public function teorimenDetail($id)
  156.     {
  157.         return $this->detail('teorimen'$id);
  158.     }
  159.     /**
  160.      * 商品一覧(ヴィーガンヌードル)
  161.      *
  162.      * @Route("/brand/vegan", name="brand_vegan")
  163.      * @Template("Brand/vegan.twig")
  164.      */
  165.     public function vegan()
  166.     {
  167.         return $this->getBrandPageData('vegan');
  168.     }
  169.     /**
  170.      * 商品詳細(ヴィーガンヌードル)
  171.      *
  172.      * @Route("/brand/vegan/{id}", name="brand_vegandetail")
  173.      */
  174.     public function veganDetail($id)
  175.     {
  176.         return $this->detail('vegan'$id);
  177.     }
  178.     private function getBrandPageData($brandName)
  179.     {
  180.         $cidList $this->eccubeConfig->get('category_id');
  181.         $category_id $cidList[$brandName];
  182.         $categoryData $this->categoryRepository->find($category_id);
  183.         $_categoryList $this->categoryRepository->getList($categoryData);
  184.         $productList = [];
  185.         $categoryList = [];
  186.         $productLimit $this->eccubeConfig->get('PRODUCT_LIMIT'0);
  187.         if ($_categoryList) {
  188.             foreach ($_categoryList as $_category) {
  189.                 $_products $this->productRepository->getQueryBuilderBySearchData([
  190.                     'category_id' => $_category,
  191.                 ])
  192.                     ->andWhere('p.summarize_flg = :disabled')
  193.                     ->setParameter('disabled'Constant::DISABLED)
  194.                     ->getQuery()
  195.                     ->getResult();
  196.                 if ($_products) {
  197.                     $categoryList[$_category['id']] = $_category['name'];
  198.                     $productList[$_category['id']] = [];
  199.                     $cnt 0;
  200.                     foreach ($_products as $_product) {
  201.                         $productList[$_category['id']][] = $_product;
  202.                         $cnt++;
  203.                         if ($cnt >= $productLimit && $productLimit != 0) {
  204.                             break;
  205.                         }
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.         return [
  211.             'categoryData' => $categoryData,
  212.             'productList' => $productList,
  213.             'categoryList' => $categoryList,
  214.         ];
  215.     }
  216.     /**
  217.      * @param $brandCode
  218.      * @param $id
  219.      * @return Response
  220.      */
  221.     private function detail($brandCode$id)
  222.     {
  223.         $cidList $this->eccubeConfig->get('category_id');
  224.         $category_id $cidList[$brandCode];
  225.         // TODO 商品IDとブランドの整合性チェック
  226.         // /IDにあった商品がない場合は404
  227.         // IDが一致していても指定したブランドにない商品の場合は/brandへリダイレクト
  228.         $product $this->productRepository->find($id);
  229.         if (!$product) {
  230.             throw new NotFoundHttpException('おさがしの商品はみつかりませんでした');
  231.         }
  232.         $_categoryList $this->entityManager
  233.             ->getRepository('Eccube\Entity\Category')
  234.             ->findBy(array('Parent' => $category_id));
  235.         foreach ($_categoryList as $_category) {
  236.             $_products $this->productRepository->createQueryBuilder('p')
  237.                 ->innerJoin('p.ProductCategories''pct')
  238.                 ->andWhere('p.summarize_flg = :flg_disabled')
  239.                 ->andWhere('p.Status = :status_show')
  240.                 ->andWhere('pct.category_id = :category_id')
  241.                 ->andWhere('p.id = :product_id')
  242.                 ->setParameter('flg_disabled'Constant::DISABLED)
  243.                 ->setParameter('status_show'ProductStatus::DISPLAY_SHOW)
  244.                 ->setParameter('product_id'$id)
  245.                 ->setParameter('category_id'$_category->getId())
  246.                 ->getQuery()
  247.                 ->getResult();
  248.             if ($_products) {
  249.                 return $this->forwardToRoute('product_detail', ['id' => $id]);
  250.             }
  251.         }
  252.         return $this->redirectToRoute('brand');
  253.     }
  254.     /**
  255.      * アレルゲン検索
  256.      *
  257.      * @Route("/brand/allergensearch", name="brand_allergensearch", methods={"GET","POST"})
  258.      */
  259.     public function allergenSearch(Request $request)
  260.     {
  261.         $ret = array('list' => []);
  262.         $allergens $request->get('allergens', []);
  263.         $includeFlg intval($request->get('include_flg'false));
  264.         $qb $this->productRepository->createQueryBuilder('p')
  265.             ->andWhere('p.summarize_flg = 0')
  266.             ->andWhere('p.Status = 1');
  267.         if ($includeFlg) {
  268.             // アレルゲン成分が一つでも含まれる商品の検索
  269.             $orStatements $qb->expr()->orX();
  270.             foreach($allergens as $idx => $allergen) {
  271.                 $placeholder 'allergens' $idx;
  272.                 $orStatements->add(
  273.                     $qb->expr()->like('p.allergens'':'.$placeholder)
  274.                 );
  275.                 $qb->setParameter($placeholder'%' $allergen ',%');
  276.             }
  277.             $qb->andWhere($orStatements);
  278.         } else {
  279.             // 一つも含まれない商品検索
  280.             foreach($allergens as $idx => $allergen) {
  281.                 $placeholder 'allergens' $idx;
  282.                 $qb->andWhere('p.allergens NOT LIKE :'.$placeholder)
  283.                     ->setParameter($placeholder'%' $allergen ',%');
  284.             }
  285.         }
  286.         $Products $qb->groupBy('p.id')
  287.             ->getQuery()
  288.             ->getResult();
  289.         if ($Products) {
  290.             foreach ($Products as $Product) {
  291.                 $ret['list'][] = array(
  292.                     'id' => $Product->getId(),
  293.                     'name' => $Product->getName(),
  294.                     'main_image' => $Product['mainListImage'] . ''
  295.                 );
  296.             }
  297.         }
  298.         return $this->json($ret);
  299.     }
  300. }