app/Plugin/TopCategory42/Controller/TopCategoryController.php line 56

Open in your IDE?
  1. <?php
  2. namespace Plugin\TopCategory42\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Eccube\Repository\CategoryRepository;
  8. use Eccube\Repository\ProductRepository;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Plugin\TopCategory42\Service\TopCategoryService;
  11. /**
  12.  * Class CampaignController.
  13.  */
  14. class TopCategoryController extends AbstractController
  15. {
  16.    /**
  17.      * @var CategoryRepository
  18.      */
  19.     protected $categoryRepository;
  20.     /**
  21.      * @var ProductRepository
  22.      */
  23.     protected $productRepository;
  24.     /**
  25.      * @var TopCategoryService
  26.      */
  27.     protected $topCategoryService;
  28.     /**
  29.      * NewsController constructor.
  30.      *
  31.      * @param CategoryRepository $categoryRepository
  32.      * @param ProductRepository $productRepository
  33.      * @param TopCategoryService $topCategoryService
  34.      */
  35.     public function __construct(
  36.         CategoryRepository $categoryRepository,
  37.         ProductRepository $productRepository,
  38.         TopCategoryService $topCategoryService
  39.     ){
  40.         $this->categoryRepository $categoryRepository;
  41.         $this->productRepository $productRepository;
  42.         $this->topCategoryService $topCategoryService;
  43.     }
  44.     /**
  45.      * @Route("/block/top_category42", name="block_top_category42", methods={"GET"})
  46.      * @Template("Block/top_category42.twig")
  47.      */
  48.     public function index(Request $requestPaginatorInterface $paginator)
  49.     {
  50.         $Categories null;
  51.         $CatProducts null;
  52.         $NewProducts null;
  53.         $Products null;
  54.         $Cids = [,2876];//カテゴリーが重複する場合、表示優先順位は、8 > 7 > 6とする。
  55.         $Pids = [];
  56.         foreach($Cids as $key) {
  57.             //表示は最大10件
  58.             if($key == 1) {
  59.                 $ikey 0;
  60.             }elseif($key == 2) {
  61.                 $ikey 1;
  62.             }elseif($key == 8) {
  63.                 $ikey 4;
  64.             }elseif($key == 7) {
  65.                 $ikey 2;
  66.             }elseif($key == 6) {
  67.                 $ikey 3;
  68.             }
  69.             $Categories[$ikey] = $this->categoryRepository->find($key);
  70.             $Products $this->topCategoryService->getData($this->categoryRepository->find($key), $Pids);
  71.             // オススメのフラグだけ取得
  72.             foreach($Products as $pkey => $product) {
  73.                 if($product->getTags()){
  74.                     foreach($product->getTags() as $tag) {
  75.                         if($tag->getId() == 4) {
  76.                             $NewProducts[$ikey][$pkey] = $product;
  77.                             break;
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.             $CatProducts[$ikey] = array_slice($NewProducts[$ikey], 010);
  83.             // 除外する商品IDを設定(先のカテゴリーで取得した商品は、後続のカテゴリーでは取得しない)
  84.             foreach($Products as $Product) {
  85.                 $Pids[] = $Product->getId();
  86.             }
  87.         }
  88.         ksort($Categories);
  89.         ksort($CatProducts);
  90.         return [
  91.             'Categories' => $Categories,
  92.             'CatProducts' => $CatProducts,
  93.         ];
  94.     }
  95. }