<?php
/**
* 共通で使うカテゴリーなどを各ページに設定
*/
namespace Plugin\PiaFavorite;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Class Event.
*/
class Event implements EventSubscriberInterface {
private $authorizationChecker;
private $tokenStorage;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var CustomerFavoriteProductRepository
*/
protected $customerFavoriteProductRepository;
/**
* Event constructor.
*
* @param EntityManagerInterface $entityManager
* @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
* @param \Twig\Environment $twig
*/
public function __construct(
AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage, EntityManagerInterface $entityManager, CustomerFavoriteProductRepository $customerFavoriteProductRepository) {
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
$this->entityManager = $entityManager;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
}
/**
* Todo: admin.order.delete.complete has been deleted.
*
* @return array
*/
public static function getSubscribedEvents() {
return [
'Product/list.twig' => 'onProductListRender',
];
}
/**
* 絞込み検索結果を商品一覧ページに設定
* @param TemplateEvent $event
*/
public function onProductListRender(TemplateEvent $event) {
$parameters = $event->getParameters();
$htFavorite = array();
$Customer = $this->getLoginCustomer();
if (!empty($Customer)) {
$qb = $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
$rows = $qb->getQuery()->getResult();
if(is_array($rows) and count($rows) > 0){
foreach($rows as $row){
$Product = $row->getProduct();
if(empty($Product)){
continue;
}
$product_id = $Product->getId();
$htFavorite[$product_id] = true;
}
}
}
$parameters["htFavorite"] = $htFavorite;
$event->setParameters($parameters);
}
public function getLoginCustomer() {
try {
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
$Customer = $this->tokenStorage->getToken()->getUser();
return $Customer;
}
} catch (AuthenticationCredentialsNotFoundException $e) {
}
return null;
}
}