app/Plugin/RefineCheckItem42/Controller/RefineCheckItemController.php line 67

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Refine
  4.  *
  5.  * Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
  6.  *
  7.  * https://www.re-fine.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 Plugin\RefineCheckItem42\Controller;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Eccube\Controller\AbstractController;
  15. use Plugin\RefineCheckItem42\Repository\ProductRepository;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Eccube\Repository\CustomerFavoriteProductRepository;
  20. /**
  21.  * Class RefineCheckItemController.
  22.  */
  23. class RefineCheckItemController extends AbstractController
  24. {
  25.     /**
  26.      * @var ProductRepository
  27.      */
  28.     private $productRepository;
  29.     
  30.     /**
  31.      * @var CustomerFavoriteProductRepository
  32.      */
  33.     private $customerFavoriteProductRepository;
  34.     /**
  35.      * RefineCheckItemController constructor.
  36.      *
  37.      * @param ProductRepository $productRepository
  38.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  39.      * @throws \Exception
  40.      */
  41.     public function __construct(
  42.       ProductRepository $productRepository,
  43.       CustomerFavoriteProductRepository $customerFavoriteProductRepository)
  44.     {
  45.         $this->productRepository $productRepository;
  46.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  47.     }
  48.     /**
  49.      * アクセス履歴
  50.      *
  51.      * @param Request $request
  52.      *
  53.      * @return array
  54.      *
  55.      * @Route("/block/refine_check_item42", name="block_refine_check_item")
  56.      * @Template("Block/refine_check_item.twig")
  57.      */
  58.     public function index(Request $request)
  59.     {
  60.         // Cookie 取得
  61.         $productIds = (new ArrayCollection($this->getProductIdsFromCookie($request)))->toArray();
  62.         // 表示用に順序を逆にする
  63.         $productIds array_reverse($productIds);
  64.         
  65.         $Products $this->productRepository->findProductsWithSortedClassCategories($productIds'p.id');
  66.         // 追加処理
  67.         foreach ($Products as $Product) {
  68.           if ($this->isGranted('ROLE_USER')) {
  69.               $Customer $this->getUser();
  70.               $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer$Product));
  71.           }
  72.           else {
  73.               $Product->setSearchWord('');
  74.           }
  75.         }
  76.         return [
  77.             'Products' => $Products,
  78.         ];
  79.     }
  80.     /**
  81.      * Cookie の取得
  82.      *
  83.      * @param Request $request
  84.      * @return array|mixed
  85.      */
  86.     private function getProductIdsFromCookie(Request $request)
  87.     {
  88.         $cookie $request->cookies->get(\Plugin\RefineCheckItem42\Event::COOKIE_NAME);
  89.         return json_decode($cookietrue) ?? [];
  90.     }
  91. }