<?php
namespace Customize\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Form\Type\AddCartType;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\NewsRepository;
use Eccube\Repository\ProductRepository;
use Knp\Component\Pager\Paginator;
use Plugin\CategoryRecommend4\Repository\CategoryRecommendProductRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
class PageController extends AbstractController
{
// 設定
protected $brandMaxCategoryId = 3;
protected $shopDialogueId = 100;
protected $shopEnsembleId = 101;
protected $shopEnsembledeuxId = 1217;
protected $shopFlairId = 102;
protected $shopGatheringId = 413;
protected $pickupCategoryId = 1095;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var NewsRepository
*/
protected $newsRepository;
/**
* @var CategoryRepository
*/
protected $categoryRepository;
/**
* @var CategoryRecommendProductRepository
*/
protected $categoryRecommendProductRepository;
protected $brandCategories;
public function __construct(
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
NewsRepository $newsRepository,
CategoryRecommendProductRepository $categoryRecommendProductRepository
) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->newsRepository = $newsRepository;
$brandMaxCategory = $this->categoryRepository->find($this->brandMaxCategoryId);
$this->brandCategories = $this->categoryRepository->getList($brandMaxCategory);
$this->categoryRecommendProductRepository = $categoryRecommendProductRepository;
}
/**
* @Method("GET")
* @Route("/faq", name="faq")
* @Template("Other/faq.twig")
*/
public function faq()
{
return [];
}
/**
* @Method("GET")
* @Route("/about", name="about")
* @Template("Other/about.twig")
*/
public function about()
{
return [];
}
/**
* @Method("GET")
* @Route("/dialogue", name="shop_dialogue_top")
* @Template("Shop/dialogue_top.twig")
*/
public function shop_dialogue()
{
$info = $this->getShopInfo($this->shopDialogueId, 'dialogue', 5);
return $info;
}
/**
* @Method("GET")
* @Route("/blogs", name="blog_list")
* @Template("Blog/list.twig")
*/
public function blog_list(Request $request)
{
$shop = $request->query->get('shop');
$pageno = $request->query->get('pageno', 1);
$cat_id = '';
if ($shop == 'dialogue') {
$cat_id = 5;
} elseif ($shop == 'ensemble') {
$cat_id = 2;
} elseif ($shop == 'ensembledeux') {
$cat_id = 1521;
} elseif ($shop == 'flair') {
$cat_id = 55;
} elseif ($shop == 'gathering') {
$cat_id = 397;
}
if ($pageno == '1') {
$count = 17;
$offset = 0;
} else {
$count = 18;
$offset = 18 * ($pageno - 1) - 1;
}
$args = [
'posts_per_page' => $count,
'offset' => $offset,
'orderby' => 'date',
'order' => 'DESC',
];
if ($cat_id) {
$args['cat'] = $cat_id;
}
$the_query = new \WP_Query($args);
$blogs = [];
while ($the_query->have_posts()): $the_query->the_post();
$categories = get_the_category();
$cat_name = "";
if ($categories) {
foreach ($categories as $category) {
if ($category->term_id === 2 || $category->term_id === 5 || $category->term_id === 55 || $category->term_id === 397 || $category->term_id === 1521) {
if ($cat_id) {
if ($shop == 'dialogue' && $category->term_id === 5) {
$cat_name = $category->name;
break;
} elseif ($shop == 'ensemble' && $category->term_id === 2) {
$cat_name = $category->name;
break;
} elseif ($shop == 'ensembledeux' && $category->term_id === 1521) {
$cat_name = $category->name;
break;
} elseif ($shop == 'flair' && $category->term_id === 55) {
$cat_name = $category->name;
break;
} elseif ($shop == 'gathering' && $category->term_id === 397) {
$cat_name = $category->name;
break;
}
} else {
// 設定なし
$cat_name = $category->name;
break;
}
}
}
}
$blogs[] = [
'id' => get_the_ID(),
'title' => get_the_title(),
'cat_name' => $cat_name,
'date' => get_the_date('Y.m.d'),
'thumbnail' => get_the_post_thumbnail_url(),
];
endwhile;
wp_reset_postdata();
$max_posts_num = $the_query->found_posts;
if ($max_posts_num <= 17) {
$max_page_num = 1;
} else {
$max_page_num = 1 + ceil(($max_posts_num - 17) / 18);
}
return [
'shop' => $shop,
'Blogs' => $blogs,
'currentPage' => $pageno,
'max_page_num' => $max_page_num,
];
}
/**
* @Method("GET")
* @Route("/blogs/detail/{id}", name="blog_detail")
* @Template("Blog/detail.twig")
*/
public function blog_detail(Request $request, $id)
{
$args = [
'p' => $id,
'category__in' => [5, 2, 55, 397, 1521],
];
$the_query = new \WP_Query($args);
$acf = new \ACF();
$blog = [];
while ($the_query->have_posts()): $the_query->the_post();
$prev_post = get_next_post(true);
$next_post = get_previous_post(true);
$blog = [
'id' => get_the_ID(),
'title' => get_the_title(),
'date' => get_the_date('Y.m.d'),
'content' => do_shortcode(get_the_content()),
'thumbnail' => get_the_post_thumbnail_url(),
'next_link' => $prev_post ? "/blogs/detail/" . $prev_post->ID : null,
'prev_link' => $next_post ? "/blogs/detail/" . $next_post->ID : null,
'recommend' => get_field('recommend'),
];
$cate_slugs = [];
foreach (get_the_category() as $c) {
$cate_slugs[$c->cat_ID] = $c->slug;
}
if (in_array('dialogue', $cate_slugs)) {
$shop = 'dialogue';
// $pickup_category_id = 1102;
$pickup_category_id = $this->shopDialogueId;
} elseif (in_array('ensemble', $cate_slugs)) {
$shop = 'ensemble';
// $pickup_category_id = 1103;
$pickup_category_id = $this->shopEnsembleId;
} elseif (in_array('ensembledeux', $cate_slugs)) {
$shop = 'ensembledeux';
// $pickup_category_id = 1104;
$pickup_category_id = $this->shopEnsembledeuxId;
} elseif (in_array('flair', $cate_slugs)) {
$shop = 'flair';
// $pickup_category_id = 1105;
$pickup_category_id = $this->shopFlairId;
} elseif (in_array('gathering', $cate_slugs)) {
$shop = 'gathering';
// $pickup_category_id = 1106;
$pickup_category_id = $this->shopGatheringId;
}
endwhile;
if ($blog['recommend']) {
log_info("getlog" . $blog['recommend']);
$recomend = explode(',', $blog['recommend']);
$category_recommend_products = $this->getProductsInRecommend($recomend, 3);
} else {
$category_recommend_products = $this->getProductsInCategory($pickup_category_id, 3);
}
// $pickupCategory = $this->categoryRepository->find($pickup_category_id);
// $category_recommend_products = $this->categoryRecommendProductRepository->getCategoryRecommendProduct($pickupCategory);
// addCart form
$forms = [];
$ids = [];
foreach ($category_recommend_products as $item) {
$ids[] = $item->getId();
}
$ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
foreach ($category_recommend_products as $item) {
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */
$builder = $this->formFactory->createNamedBuilder(
'',
AddCartType::class,
null,
[
'product' => $ProductsAndClassCategories[$item->getId()],
'allow_extra_fields' => true,
]
);
$addCartForm = $builder->getForm();
$forms[$item->getId()] = $addCartForm->createView();
}
return [
'shop' => $shop,
'Blog' => $blog,
'Pickup' => $category_recommend_products,
'forms' => $forms,
];
}
/**
* @Method("GET")
* @Route("/dialogue/concept", name="shop_dialogue_concept")
* @Template("Shop/dialogue_concept.twig")
*/
public function shop_dialogue_concept()
{
return [];
}
/**
* @Method("GET")
* @Route("/ensemble", name="shop_ensemble_top")
* @Template("Shop/ensemble_top.twig")
*/
public function shop_ensemble()
{
$info = $this->getShopInfo($this->shopEnsembleId, 'ensemble', 2);
return $info;
}
/**
* @Method("GET")
* @Route("/ensemble/concept", name="shop_ensemble_concept")
* @Template("Shop/ensemble_concept.twig")
*/
public function shop_ensemble_concept()
{
return [];
}
/**
* @Method("GET")
* @Route("/ensembledeux", name="shop_ensembledeux_top")
* @Template("Shop/ensembledeux_top.twig")
*/
public function shop_ensembledeux()
{
$info = $this->getShopInfo($this->shopEnsembledeuxId, 'ensembledeux', 1521);
return $info;
}
/**
* @Method("GET")
* @Route("/ensembledeux/concept", name="shop_ensembledeux_concept")
* @Template("Shop/ensembledeux_concept.twig")
*/
public function shop_ensembledeux_concept()
{
return [];
}
/**
* @Method("GET")
* @Route("/flair", name="shop_flair_top")
* @Template("Shop/flair_top.twig")
*/
public function shop_flair()
{
$info = $this->getShopInfo($this->shopFlairId, 'flair', 55);
return $info;
}
/**
* @Method("GET")
* @Route("/flair/concept", name="shop_flair_concept")
* @Template("Shop/flair_concept.twig")
*/
public function shop_flair_concept()
{
return [];
}
/**
* @Method("GET")
* @Route("/gathering", name="shop_gathering_top")
* @Template("Shop/gathering_top.twig")
*/
public function shop_gathering()
{
$info = $this->getShopInfo($this->shopGatheringId, 'gathering', 397);
return $info;
}
/**
* @Method("GET")
* @Route("/gathering/concept", name="shop_gathering_concept")
* @Template("Shop/gathering_concept.twig")
*/
public function shop_gathering_concept()
{
return [];
}
/**
* @Method("GET")
* @Route("/brand/list", name="brand_list")
* @Template("Brand/list.twig")
*/
public function brand_list(Request $request)
{
$brandParantCategory = $this->categoryRepository->find('3');
$qb = $this->categoryRepository->createQueryBuilder('ca');
$qb->andWhere('ca.Parent = :parent')
->setParameter('parent', $brandParantCategory)
->orderBy("ca.sort_no", "DESC");
$shop = $request->query->get('shop');
if ($shop) {
if ($shop == 'dialogue') {
/*$or = $qb->expr()->orx();
$or->add($qb->expr()->eq('ca.shop_dialogue', 1));
$or->add($qb->expr()->eq('ca.shop_ensemble', 1));
$qb->andWhere($or);*/
$qb->andWhere('ca.shop_dialogue = :flg')
->setParameter('flg', '1');
} else if ($shop == 'ensemble') {
$qb->andWhere('ca.shop_ensemble = :flg')
->setParameter('flg', '1');
} else if ($shop == 'ensembledeux') {
$qb->andWhere('ca.shop_ensembledeux = :flg')
->setParameter('flg', '1');
} else if ($shop == 'flair') {
$qb->andWhere('ca.shop_flair = :flg')
->setParameter('flg', '1');
} else if ($shop == 'gathering') {
$qb->andWhere('ca.shop_gathering = :flg')
->setParameter('flg', '1');
}
}
$cat = $request->query->get('cat');
if ($cat) {
if ($cat == 'mens') {
$qb->andWhere('ca.men = :flg')
->setParameter('flg', '1');
} else if ($cat == 'womens') {
$qb->andWhere('ca.women = :flg')
->setParameter('flg', '1');
}
}
$brandCategories = $qb->getQuery()
->getResult();
return [
'shop' => $shop,
'BrandCategories' => $brandCategories,
];
}
/**
* @Method("GET")
* @Route("/brand", name="all_brand_list")
* @Template("Other/brand.twig")
*/
public function all_brand_list(Request $request)
{
$brandParantCategory = $this->categoryRepository->find('3');
$qb = $this->categoryRepository->createQueryBuilder('ca');
$qb->andWhere('ca.Parent = :parent')
->andWhere('ca.shop_dialogue = :shopEnable OR ca.shop_ensemble = :shopEnable OR ca.shop_ensembledeux = :shopEnable OR ca.shop_flair = :shopEnable OR ca.shop_gathering = :shopEnable')
->setParameter('parent', $brandParantCategory)
->setParameter('shopEnable', 1)
->orderBy("ca.sort_no", "DESC");
$cat = $request->query->get('cat');
if ($cat) {
if ($cat == 'mens') {
$qb->andWhere('ca.men = :flg')
->setParameter('flg', '1');
} else if ($cat == 'womens') {
$qb->andWhere('ca.women = :flg')
->setParameter('flg', '1');
}
}
// $qb->andWhere('ca.name != ?1')
// ->setParameter(1, 'Aeta');
$brandCategories = $qb->getQuery()
->getResult();
return [
'BrandCategories' => $brandCategories,
];
}
/**
* @Method("GET")
* @Route("/item/list", name="item_list")
* @Template("Item/list.twig")
*/
public function item_list(Request $request)
{
$brandParantCategory = $this->categoryRepository->find('4');
$qb = $this->categoryRepository->createQueryBuilder('ca');
$qb->andWhere('ca.Parent = :parent')
->setParameter('parent', $brandParantCategory)
->orderBy("ca.sort_no", "DESC");
$shop = $request->query->get('shop');
$brandCategories = $qb->getQuery()
->getResult();
return [
'shop' => $shop,
'BrandCategories' => $brandCategories,
];
}
/**
* @Method("GET")
* @Route("/item", name="all_item_list")
* @Template("Other/item.twig")
*/
public function all_item_list(Request $request)
{
$brandParantCategory = $this->categoryRepository->find('4');
$qb = $this->categoryRepository->createQueryBuilder('ca');
$qb->andWhere('ca.Parent = :parent')
->setParameter('parent', $brandParantCategory)
->orderBy("ca.sort_no", "DESC");
$brandCategories = $qb->getQuery()
->getResult();
return [
'BrandCategories' => $brandCategories,
];
}
/**
* @Method("GET")
* @Route("/brand/{id}", name="brand_detail", requirements={"id" = "\d+"})
* @Template("Brand/detail.twig")
*/
public function brand_detail(Request $request, $id, Paginator $paginator)
{
$brand = $this->categoryRepository->find($id);
$pagination = $paginator->paginate(
$this->getProductsInCategoryQuery($id),
$request->query->getInt('pageno', 1),
12
);
// 店舗設定
if ($brand->getShopDialogue() ===true) {
$shop = 'dialogue';
} else if ($brand->getShopEnsemble() ===true) {
$shop = 'ensemble';
} else if ($brand->getShopEnsembledeux() ===true) {
$shop = 'ensembledeux';
} else if ($brand->getShopFlair() ===true) {
$shop = 'flair';
} else if ($brand->getShopGathering() ===true) {
$shop = 'gathering';
}else{
$shop = 'dialogue';
}
log_info("brand_detail_log shop:" . $shop);
return [
'shop' => $shop,
'Brand' => $brand,
'pagination' => $pagination,
];
}
/**
* @Method("GET")
* @Route("/item/{id}", name="item_detail", requirements={"id" = "\d+"})
* @Template("Item/detail.twig")
*/
public function item_detail(Request $request, $id, Paginator $paginator)
{
$brand = $this->categoryRepository->find($id);
$pagination = $paginator->paginate(
$this->getProductsInCategoryQuery($id),
$request->query->getInt('pageno', 1),
12
);
return [
'Brand' => $brand,
'pagination' => $pagination,
];
}
private function getProductsInCategoryQuery($categry_id)
{
$category = $this->categoryRepository->find($categry_id);
$qb = $this->productRepository->createQueryBuilder('p')
->addSelect("(CASE WHEN (CASE WHEN MAX(pc.stock) IS NULL THEN 0 ELSE MAX(pc.stock) END) + (CASE WHEN MAX(pc.stock_unlimited) IS NULL THEN 0 ELSE MAX(pc.stock_unlimited) END) = 0 THEN '0000-00-00 00:00:00_0000-00-00 00:00:00' ELSE CONCAT(CASE WHEN p.published_at IS NULL THEN '0000-00-00 00:00:00' ELSE p.published_at END, '_', p.create_date) END) AS HIDDEN sort_field")
->andWhere('p.Status = 1')
->andWhere('p.published_at is NULL OR p.published_at <= :now')
->setParameter('now', new \DateTime())
->andWhere('pc.visible = :visible')
->setParameter('visible', true);
$qb->innerJoin('p.ProductClasses', 'pc')
->innerJoin('p.ProductCategories', 'pct')
->innerJoin('pct.Category', 'c')
->andWhere($qb->expr()->in('pct.Category', ':Categories'))
->setParameter('Categories', [$category])
->groupBy('p.id')
->addOrderBy('sort_field', 'DESC')
->addOrderBy('p.id', 'DESC');
return $qb;
}
private function getProductsInCategory($categry_id, $count)
{
$category = $this->categoryRepository->find($categry_id);
$categories = [$category];
$qb = $this->productRepository->createQueryBuilder('p')
->addSelect("(CASE WHEN (CASE WHEN MAX(pc.stock) IS NULL THEN 0 ELSE MAX(pc.stock) END) + (CASE WHEN MAX(pc.stock_unlimited) IS NULL THEN 0 ELSE MAX(pc.stock_unlimited) END) = 0 THEN '0000-00-00 00:00:00_0000-00-00 00:00:00' ELSE CONCAT(CASE WHEN p.published_at IS NULL THEN '0000-00-00 00:00:00' ELSE p.published_at END, '_', p.create_date) END) AS HIDDEN sort_field")
->andWhere('p.Status = 1')
->andWhere('p.published_at is NULL OR p.published_at <= :now')
->setParameter('now', new \DateTime())
->andWhere('pc.visible = :visible')
->setParameter('visible', true);
$qb->innerJoin('p.ProductClasses', 'pc')
->innerJoin('p.ProductCategories', 'pct')
->innerJoin('pct.Category', 'c')
->andWhere($qb->expr()->in('pct.Category', ':Categories'))
->setParameter('Categories', $categories)
->groupBy('p.id')
->addOrderBy('sort_field', 'DESC')
->addOrderBy('p.id', 'DESC');
$products = $qb
->setMaxResults($count)
->getQuery()
->getResult();
// print("<pre>".print_r($products,true)."</pre>");die;
return $products;
}
private function getProductsInRecommend($recommend, $count)
{
log_info("getlog" . print_r($recommend, true));
$qb = $this->productRepository->createQueryBuilder('p')
->andWhere('p.Status = 1')
->andWhere('p.published_at is NULL OR p.published_at <= :now')
->setParameter('now', new \DateTime())
;
$qb->andWhere($qb->expr()->in('p.id', $recommend));
$qb->innerJoin('p.ProductCategories', 'pct')
->innerJoin('pct.Category', 'c')
->addOrderBy('p.id', 'DESC')
->groupBy('p.id');
$products = $qb
->setMaxResults($count)
->getQuery()
->getResult();
return $products;
}
private function getProductsInCategories($shopCategory, $brandCategory)
{
$qb = $this->productRepository->createQueryBuilder('p')
->andWhere('p.Status = 1')
->andWhere('p.published_at is NULL OR p.published_at <= :now')
->setParameter('now', new \DateTime());
$qb->innerJoin('p.ProductCategories', 'pct')
->innerJoin('pct.Category', 'c')
->andWhere($qb->expr()->in('pct.Category', ':Categories1'))
->setParameter('Categories1', [$shopCategory])
->innerJoin('p.ProductCategories', 'pct2')
->innerJoin('pct.Category', 'c2')
->andWhere($qb->expr()->in('pct2.Category', ':Categories2'))
->setParameter('Categories2', [$brandCategory]);
$products = $qb->getQuery()
->getResult();
if (count($products)) {
return $brandCategory;
}
return false;
}
private function getShopInfo($p_cat_id, $s_name, $b_cat_id)
{
$brands = [];
$products = $this->getProductsInCategory($p_cat_id, 20);
$shopCategory = $this->categoryRepository->find($p_cat_id);
foreach ($this->brandCategories as $brandCategory) {
$result = $this->getProductsInCategories($shopCategory, $brandCategory);
if ($result) {
$brands[] = $result;
}
}
$newsList = $this->newsRepository->getListForShop(strtoupper($s_name));
$blogs = [];
$args = array(
'cat' => $b_cat_id,
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
);
$the_query = new \WP_Query($args);
while ($the_query->have_posts()): $the_query->the_post();
$blogs[] = [
'id' => get_the_ID(),
'title' => get_the_title(),
'content' => get_the_content(),
'date' => get_the_date('Y.m.d'),
'thumbnail' => get_the_post_thumbnail_url(),
];
endwhile;
return [
'shop' => strtolower($s_name),
'Products' => $products,
'Brands' => $brands,
'NewsList' => $newsList,
'Blogs' => $blogs,
];
}
}