<?php
/*
* This file is part of Refine
*
* Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
*
* https://www.re-fine.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\RefineCheckItem42\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Eccube\Controller\AbstractController;
use Plugin\RefineCheckItem42\Repository\ProductRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Repository\CustomerFavoriteProductRepository;
/**
* Class RefineCheckItemController.
*/
class RefineCheckItemController extends AbstractController
{
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var CustomerFavoriteProductRepository
*/
private $customerFavoriteProductRepository;
/**
* RefineCheckItemController constructor.
*
* @param ProductRepository $productRepository
* @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
* @throws \Exception
*/
public function __construct(
ProductRepository $productRepository,
CustomerFavoriteProductRepository $customerFavoriteProductRepository)
{
$this->productRepository = $productRepository;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
}
/**
* アクセス履歴
*
* @param Request $request
*
* @return array
*
* @Route("/block/refine_check_item42", name="block_refine_check_item")
* @Template("Block/refine_check_item.twig")
*/
public function index(Request $request)
{
// Cookie 取得
$productIds = (new ArrayCollection($this->getProductIdsFromCookie($request)))->toArray();
// 表示用に順序を逆にする
$productIds = array_reverse($productIds);
$Products = $this->productRepository->findProductsWithSortedClassCategories($productIds, 'p.id');
// 追加処理
foreach ($Products as $Product) {
if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
$Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer, $Product));
}
else {
$Product->setSearchWord('');
}
}
return [
'Products' => $Products,
];
}
/**
* Cookie の取得
*
* @param Request $request
* @return array|mixed
*/
private function getProductIdsFromCookie(Request $request)
{
$cookie = $request->cookies->get(\Plugin\RefineCheckItem42\Event::COOKIE_NAME);
return json_decode($cookie, true) ?? [];
}
}