<?php
namespace Customize\Controller;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Product;
use Customize\Entity\FavoriteKey;
use Customize\Entity\FavoriteProduct;
use Eccube\Event\EventArgs;
use Eccube\Event\EccubeEvents;
use Eccube\Controller\AbstractController;
use Eccube\Repository\BaseInfoRepository;
use Customize\Repository\FavoriteKeyRepository;
use Customize\Repository\FavoriteProductRepository;
use Eccube\Service\CalculatePriceService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Cookie;
class FavoriteController extends AbstractController
{
/**
* @var BaseInfo
*/
protected $BaseInfo;
/**
* @var FavoriteKeyRepository
*/
protected $favoriteKeyRepository;
/**
* @var FavoriteProductRepository
*/
protected $favoriteProductRepository;
/**
* @var CalculatePriceService
*/
protected $calculatePriceService;
public function __construct(
BaseInfoRepository $baseInfoRepository,
FavoriteKeyRepository $favoriteKeyRepository,
FavoriteProductRepository $favoriteProductRepository,
CalculatePriceService $calculatePriceService
) {
$this->BaseInfo = $baseInfoRepository->get();
$this->favoriteKeyRepository = $favoriteKeyRepository;
$this->favoriteProductRepository = $favoriteProductRepository;
$this->calculatePriceService = $calculatePriceService;
}
/**
* お気に入り商品一覧
*
* @Route("/favorite", name="favorite", priority=10)
* @Template("Favorite/index.twig")
*/
public function index(Request $request, PaginatorInterface $paginator)
{
if (!$this->BaseInfo->isOptionFavoriteProduct()) {
throw new NotFoundHttpException();
}
// ログイン済みの場合、マイページ/お気に入り一覧へ
if($this->isGranted('ROLE_USER')) {
return $this->redirect($this->generateUrl('mypage_favorite'));
}
// お気に入りキー
$FavoriteKey = null;
$cookieKey = $request->cookies->get('eccube_favorite');
if($cookieKey) {
$FavoriteKey = $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
}
if(!$FavoriteKey) {
$FavoriteKey = new FavoriteKey();
log_warning('Show Favorite without cookie_key');
} else {
log_warning('Show Favorite with id=[' . $FavoriteKey->getId() . ']');
}
// paginator
$qb = $this->favoriteProductRepository->getQueryBuilderByFavoriteKey($FavoriteKey);
$event = new EventArgs(
[
'qb' => $qb,
// 'Customer' => $Customer,
],
$request
);
// $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
$pagination = $paginator->paginate(
$qb,
$request->get('pageno', 1),
$this->eccubeConfig['eccube_search_pmax'],
['wrap-queries' => true]
);
//シミュレーション料金を計算
foreach($pagination as $FavoriteProduct) {
$ShelvingOption = $FavoriteProduct->getShelvingOption();
if($ShelvingOption != null) {
$shelvingPrice = $this->calculatePriceService->calculate($ShelvingOption, true);
$FavoriteProduct->setShelvingPrice($shelvingPrice);
}
}
return [
'pagination' => $pagination,
];
}
/**
* お気に入り商品を削除する.
*
* @Route("/favorite/{id}/delete", name="favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"}, priority=10)
*/
public function delete(Request $request, Product $Product)
{
$this->isTokenValid();
//お気に入りキー
$FavoriteKey = null;
$cookieKey = $request->cookies->get('eccube_favorite');
if($cookieKey) {
$FavoriteKey = $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
}
if(!$FavoriteKey) {
$FavoriteKey = new FavoriteKey();
}
// FavoriteProduct ID
$fp = $request->query->get('fp');
log_info('お気に入り商品削除開始', [$cookieKey, $Product->getId(), $fp]);
if($fp) {
$FavoriteProduct = $this->favoriteProductRepository->findOneBy(['FavoriteKey' => $FavoriteKey, 'id' => $fp]);
} else {
$FavoriteProduct = $this->favoriteProductRepository->findOneBy(['FavoriteKey' => $FavoriteKey, 'Product' => $Product]);
}
if ($FavoriteProduct) {
$this->favoriteProductRepository->delete($FavoriteProduct);
} else {
throw new BadRequestHttpException();
}
$event = new EventArgs(
[
// 'Customer' => $Customer,
'FavoriteProduct' => $FavoriteProduct,
], $request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
log_info('お気に入り商品削除完了', [$cookieKey, $FavoriteProduct->getId()]);
return $this->redirect($this->generateUrl('favorite'));
}
}