<?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 Eccube\Controller;
use Customize\Service\WordPressService;
use Eccube\Entity\Category;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\ProductRepository;
use Plugin\TabaCMS2\Entity\Post;
use Plugin\TabaCMS2\Repository\PostRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
class TopController extends AbstractController
{
/**
* @var WordPressService
*/
private $wordPressService;
public function __construct(
WordPressService $wordPressService,
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
PostRepository $postRepository,
) {
$this->wordPressService = $wordPressService;
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->postRepository = $postRepository;
}
/**
* @Route("/", name="homepage", methods={"GET"})
* @Template("index.twig")
*/
public function index()
{
$condition = [
'type_id' => 1,
'category_id' => Post::NEWS_CATEGORY_ID,
'public_div' => true,
];
$PostLists = $this->postRepository->getList( $condition, null );
$categoryIds = [
Category::OIHAI,
Category::OBUTSUDAN_KUYOUDAI,
Category::BUTSUGU_SET,
Category::BUTSUGU_OTHER,
Category::TEMOTO_KUYOU,
];
$ProductImages = [];
// $categoryIdに応じた商品を$categoryIdsだけ取得してくる
foreach ($categoryIds as $key => $categoryId) {
$searchData = [];
$Category = $this->categoryRepository->find($categoryId);
$searchData["category_id"] = $Category;
$qb = $this->productRepository->getQueryBuilderBySearchData($searchData);
$query = $qb->getQuery();
$Products = $query->getResult();
// MEMO: categoryProductsはkeyにカテゴリーID、valueにそのカテゴリーIDに対応した商品の配列を持つ
$CategoryProducts[$categoryId] = $Products;
// top_sortがnullの要素を配列から除外
$Products = array_filter($Products, function($Product) {
return !is_null($Product['top_sort']);
});
// top_sortを基準に昇順にソート
usort($Products, function($a, $b) {
return $a['top_sort'] <=> $b['top_sort'];
});
foreach ($Products as $product) {
$ProductImages[$Category->getName()][$product->getId()]["product_id"] = $product->getId();
$ProductImages[$Category->getName()][$product->getId()]["category_id"] = $Category->getId();
$ProductImages[$Category->getName()][$product->getId()]["category_name"] = $Category->getName();
$ProductImages[$Category->getName()][$product->getId()]["name"] = $product->getName();
$ProductImages[$Category->getName()][$product->getId()]["file_name"] = $product->getProductImage()->first()->getFileName();
}
}
$newPosts = $this->wordPressService->getNewPosts([
'limit' => 4,
'pageno' => 1,
]);
return [
'Products' => $CategoryProducts,
'newPosts' => $newPosts,
'ProductImages' => $ProductImages,
'PostLists' => $PostLists,
];
}
}