src/Eccube/Controller/TopController.php line 55

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 Customize\Service\WordPressService;
  14. use Eccube\Entity\Category;
  15. use Eccube\Repository\CategoryRepository;
  16. use Eccube\Repository\ProductRepository;
  17. use Plugin\TabaCMS2\Entity\Post;
  18. use Plugin\TabaCMS2\Repository\PostRepository;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class TopController extends AbstractController
  22. {
  23.     /**
  24.      * @var WordPressService
  25.      */
  26.     private $wordPressService;
  27.     public function __construct(
  28.         WordPressService $wordPressService,
  29.         ProductRepository $productRepository,
  30.         CategoryRepository $categoryRepository,
  31.         PostRepository $postRepository,
  32.     ) {
  33.         $this->wordPressService $wordPressService;
  34.         $this->productRepository $productRepository;
  35.         $this->categoryRepository $categoryRepository;
  36.         $this->postRepository $postRepository;
  37.     }
  38.     /**
  39.      * @Route("/", name="homepage", methods={"GET"})
  40.      * @Template("index.twig")
  41.      */
  42.     public function index()
  43.     {
  44.         $condition = [
  45.             'type_id' => 1,
  46.             'category_id' => Post::NEWS_CATEGORY_ID,
  47.             'public_div' => true,
  48.         ];
  49.         $PostLists $this->postRepository->getList$conditionnull );
  50.         $categoryIds = [
  51.             Category::OIHAI,
  52.             Category::OBUTSUDAN_KUYOUDAI,
  53.             Category::BUTSUGU_SET,
  54.             Category::BUTSUGU_OTHER,
  55.             Category::TEMOTO_KUYOU,
  56.         ];
  57.         $ProductImages = [];
  58.         // $categoryIdに応じた商品を$categoryIdsだけ取得してくる
  59.         foreach ($categoryIds as $key => $categoryId) {
  60.             $searchData = [];
  61.             $Category $this->categoryRepository->find($categoryId);
  62.             $searchData["category_id"] = $Category;
  63.             $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  64.             $query $qb->getQuery();
  65.             $Products $query->getResult();
  66.             // MEMO: categoryProductsはkeyにカテゴリーID、valueにそのカテゴリーIDに対応した商品の配列を持つ
  67.             $CategoryProducts[$categoryId] = $Products;
  68.             // top_sortがnullの要素を配列から除外
  69.             $Products array_filter($Products, function($Product) {
  70.                 return !is_null($Product['top_sort']);
  71.             });
  72.             // top_sortを基準に昇順にソート
  73.             usort($Products, function($a$b) {
  74.                 return $a['top_sort'] <=> $b['top_sort'];
  75.             });
  76.             foreach ($Products as $product) {
  77.                 $ProductImages[$Category->getName()][$product->getId()]["product_id"] = $product->getId();
  78.                 $ProductImages[$Category->getName()][$product->getId()]["category_id"] = $Category->getId();
  79.                 $ProductImages[$Category->getName()][$product->getId()]["category_name"] = $Category->getName();
  80.                 $ProductImages[$Category->getName()][$product->getId()]["name"] = $product->getName();
  81.                 $ProductImages[$Category->getName()][$product->getId()]["file_name"] = $product->getProductImage()->first()->getFileName();
  82.             }
  83.         }
  84.         $newPosts $this->wordPressService->getNewPosts([
  85.             'limit' => 4,
  86.             'pageno' => 1,
  87.         ]);
  88.         return [
  89.             'Products' => $CategoryProducts,
  90.             'newPosts' => $newPosts,
  91.             'ProductImages' => $ProductImages,
  92.              'PostLists' => $PostLists,
  93.         ];
  94.     }
  95. }