<?php
namespace Plugin\Recommend42;
use Eccube\Event\TemplateEvent;
use Eccube\Form\Type\AddCartType;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Service\CartService;
use Plugin\Recommend42\Repository\RecommendProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Security\Core\Security;
class RecommendEvent implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface
*/
protected $formFactory;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var RecommendProductRepository
*/
protected $recommendProductRepository;
protected CartService $cartService;
protected Security $security;
protected CustomerFavoriteProductRepository $customerFavoriteProductRepository;
public function __construct(
FormFactoryInterface $formFactory,
ProductRepository $productRepository,
RecommendProductRepository $recommendProductRepository,
CartService $cartService,
Security $security,
CustomerFavoriteProductRepository $customerFavoriteProductRepository
) {
$this->formFactory = $formFactory;
$this->productRepository = $productRepository;
$this->cartService = $cartService;
$this->recommendProductRepository = $recommendProductRepository;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
$this->security = $security;
}
public static function getSubscribedEvents()
{
return [
'Block/recommend_product_block.twig' => 'onRecommendProduct',
'@Recommend42/Product/list.twig' => 'onProductList',
];
}
public function onProductList(TemplateEvent $event)
{
$parameters = $event->getParameters();
/** @var \Eccube\Entity\Customer $Customer */
$Customer = $this->security->getUser();
$isFavorite = [];
$itemInCart = [];
$isShowProductImage = true;
if ($Customer) {
/** @var SlidingPagination $pagination */
$pagination = $parameters['pagination'];
foreach ($pagination as $key => $Product) {
$isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product);
}
$isShowProductImage = $Customer->isShowProductImage();
}
$Carts = $this->cartService->getCarts();
foreach ($Carts as $key => $Cart) {
$CartItems = $Cart->getCartItems();
foreach ($CartItems as $key => $CartItem) {
$itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
}
}
$parameters['isFavorite'] = $isFavorite;
$parameters['itemInCart'] = $itemInCart;
$parameters['isShowProductImage'] = $isShowProductImage;
$event->setParameters($parameters);
}
public function onRecommendProduct(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Products = $this->recommendProductRepository->getRecommendProduct();
$ids = [];
$itemInCart = [];
$isFavorite = [];
$Customer = $this->security->getUser();
foreach ($Products as $RecommendProduct) {
$Product = $RecommendProduct->getProduct();
$ids[] = $Product->getId();
}
$ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
$forms = [];
foreach ($Products as $key => $RecommendProduct) {
$Product = $RecommendProduct->getProduct();
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */
$builder = $this->formFactory->createNamedBuilder(
'',
AddCartType::class,
null,
[
'product' => $ProductsAndClassCategories[$Product->getId()],
'allow_extra_fields' => true,
]
);
$addCartForm = $builder->getForm();
$forms[$Product->getId()] = $addCartForm->createView();
if ($Customer) {
$isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product);
}
}
$Carts = $this->cartService->getCarts();
foreach ($Carts as $key => $Cart) {
$CartItems = $Cart->getCartItems();
foreach ($CartItems as $key => $CartItem) {
$itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
}
}
$parameters['forms'] = $forms;
$parameters['Products'] = $Products;
$parameters['recommendItemInCart'] = $itemInCart;
$parameters['isRecommendFavorite'] = $isFavorite;
$event->setParameters($parameters);
}
}