<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Customize\Repository\AllergenRepository;
use Eccube\Common\Constant;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\ProductRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BrandController extends AbstractController
{
/**
* @var ProductRepository
*/
private ProductRepository $productRepository;
/**
* @var CategoryRepository
*/
private CategoryRepository $categoryRepository;
/**
* @var AllergenRepository
*/
private AllergenRepository $allergenRepository;
/**
* CategoryController constructor.
*/
public function __construct(
CategoryRepository $categoryRepository,
ProductRepository $productRepository,
AllergenRepository $allergenRepository
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->allergenRepository = $allergenRepository;
}
/**
* ブランドラインナップ
*
* @Route("/brand", name="brand")
* @Template("Brand/brand.twig")
*/
public function index()
{
// アレルゲン項目の取得
$allergen = $this->allergenRepository->getLabelList();
return [
'allergen' => $allergen,
];
}
/**
* 商品一覧(ニュータッチ)
*
* @Route("/brand/newtouch", name="brand_newtouch")
* @Template("Brand/newtouch.twig")
*/
public function newtouch()
{
return $this->getBrandPageData('newtouch');
}
/**
* 商品詳細(ニュータッチ)
*
* @Route("/brand/newtouch/{id}", name="brand_newtouchdetail")
*/
public function newtouchDetail($id)
{
return $this->detail('newtouch', $id);
}
/**
* 商品一覧(凄麺)
*
* @Route("/brand/sugomen", name="brand_sugomen")
* @Template("Brand/sugomen.twig")
*/
public function sugomen()
{
return $this->getBrandPageData('sugomen');
}
/**
* 商品詳細(凄麺)
*
* @Route("/brand/sugomen/{id}", name="brand_sugomendetail")
*/
public function sugomenDetail($id)
{
return $this->detail('sugomen', $id);
}
/**
* 商品一覧(手緒里庵)
*
* @Route("/brand/teorian", name="brand_teorian")
* @Template("Brand/teorian.twig")
*/
public function teorian()
{
return $this->getBrandPageData('teorian');
}
/**
* 商品詳細(手緒里庵)
*
* @Route("/brand/teorian/{id}", name="brand_teoriandetail")
*/
public function teorianDetail($id)
{
return $this->detail('teorian', $id);
}
/**
* 商品一覧(スープ・デ・パスタ)
*
* @Route("/brand/soupdepasta", name="brand_soupdepasta")
* @Template("Brand/soupdepasta.twig")
*/
public function soupdepasta()
{
return $this->getBrandPageData('soupdepasta');
}
/**
* 商品詳細(スープ・デ・パスタ)
*
* @Route("/brand/soupdepasta/{id}", name="brand_soupdepastadetail")
*/
public function soupdepastaDetail($id)
{
return $this->detail('soupdepasta', $id);
}
/**
* 商品一覧(手緒里めん)
*
* @Route("/brand/teorimen", name="brand_teorimen")
* @Template("Brand/teorimen.twig")
*/
public function teorimen()
{
return $this->getBrandPageData('teorimen');
}
/**
* 商品詳細(手緒里めん)
*
* @Route("/brand/teorimen/{id}", name="brand_teorimendetail")
*/
public function teorimenDetail($id)
{
return $this->detail('teorimen', $id);
}
/**
* 商品一覧(ヴィーガンヌードル)
*
* @Route("/brand/vegan", name="brand_vegan")
* @Template("Brand/vegan.twig")
*/
public function vegan()
{
return $this->getBrandPageData('vegan');
}
/**
* 商品詳細(ヴィーガンヌードル)
*
* @Route("/brand/vegan/{id}", name="brand_vegandetail")
*/
public function veganDetail($id)
{
return $this->detail('vegan', $id);
}
private function getBrandPageData($brandName)
{
$cidList = $this->eccubeConfig->get('category_id');
$category_id = $cidList[$brandName];
$categoryData = $this->categoryRepository->find($category_id);
$_categoryList = $this->categoryRepository->getList($categoryData);
$productList = [];
$categoryList = [];
$productLimit = $this->eccubeConfig->get('PRODUCT_LIMIT', 0);
if ($_categoryList) {
foreach ($_categoryList as $_category) {
$_products = $this->productRepository->getQueryBuilderBySearchData([
'category_id' => $_category,
])
->andWhere('p.summarize_flg = :disabled')
->setParameter('disabled', Constant::DISABLED)
->getQuery()
->getResult();
if ($_products) {
$categoryList[$_category['id']] = $_category['name'];
$productList[$_category['id']] = [];
$cnt = 0;
foreach ($_products as $_product) {
$productList[$_category['id']][] = $_product;
$cnt++;
if ($cnt >= $productLimit && $productLimit != 0) {
break;
}
}
}
}
}
return [
'categoryData' => $categoryData,
'productList' => $productList,
'categoryList' => $categoryList,
];
}
/**
* @param $brandCode
* @param $id
* @return Response
*/
private function detail($brandCode, $id)
{
$cidList = $this->eccubeConfig->get('category_id');
$category_id = $cidList[$brandCode];
// TODO 商品IDとブランドの整合性チェック
// /IDにあった商品がない場合は404
// IDが一致していても指定したブランドにない商品の場合は/brandへリダイレクト
$product = $this->productRepository->find($id);
if (!$product) {
throw new NotFoundHttpException('おさがしの商品はみつかりませんでした');
}
$_categoryList = $this->entityManager
->getRepository('Eccube\Entity\Category')
->findBy(array('Parent' => $category_id));
foreach ($_categoryList as $_category) {
$_products = $this->productRepository->createQueryBuilder('p')
->innerJoin('p.ProductCategories', 'pct')
->andWhere('p.summarize_flg = :flg_disabled')
->andWhere('p.Status = :status_show')
->andWhere('pct.category_id = :category_id')
->andWhere('p.id = :product_id')
->setParameter('flg_disabled', Constant::DISABLED)
->setParameter('status_show', ProductStatus::DISPLAY_SHOW)
->setParameter('product_id', $id)
->setParameter('category_id', $_category->getId())
->getQuery()
->getResult();
if ($_products) {
return $this->forwardToRoute('product_detail', ['id' => $id]);
}
}
return $this->redirectToRoute('brand');
}
/**
* アレルゲン検索
*
* @Route("/brand/allergensearch", name="brand_allergensearch", methods={"GET","POST"})
*/
public function allergenSearch(Request $request)
{
$ret = array('list' => []);
$allergens = $request->get('allergens', []);
$includeFlg = intval($request->get('include_flg', false));
$qb = $this->productRepository->createQueryBuilder('p')
->andWhere('p.summarize_flg = 0')
->andWhere('p.Status = 1');
if ($includeFlg) {
// アレルゲン成分が一つでも含まれる商品の検索
$orStatements = $qb->expr()->orX();
foreach($allergens as $idx => $allergen) {
$placeholder = 'allergens' . $idx;
$orStatements->add(
$qb->expr()->like('p.allergens', ':'.$placeholder)
);
$qb->setParameter($placeholder, '%' . $allergen . ',%');
}
$qb->andWhere($orStatements);
} else {
// 一つも含まれない商品検索
foreach($allergens as $idx => $allergen) {
$placeholder = 'allergens' . $idx;
$qb->andWhere('p.allergens NOT LIKE :'.$placeholder)
->setParameter($placeholder, '%' . $allergen . ',%');
}
}
$Products = $qb->groupBy('p.id')
->getQuery()
->getResult();
if ($Products) {
foreach ($Products as $Product) {
$ret['list'][] = array(
'id' => $Product->getId(),
'name' => $Product->getName(),
'main_image' => $Product['mainListImage'] . ''
);
}
}
return $this->json($ret);
}
}