<?php
namespace Customize\Event;
use Customize\Common\Constant;
use Eccube\Event\TemplateEvent;
use Eccube\Form\Type\AddCartType;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Service\CartService;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Plugin\ProductReview42\Repository\ProductReviewRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Security\Core\Security;
class CustomizeEvent implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface
*/
protected $formFactory;
/**
* @var ProductRepository
*/
protected $productRepository;
protected Security $security;
protected CustomerFavoriteProductRepository $customerFavoriteProductRepository;
protected CartService $cartService;
protected CategoryRepository $categoryRepository;
protected ProductReviewRepository $productReviewRepository;
public function __construct(
FormFactoryInterface $formFactory,
ProductRepository $productRepository,
Security $security,
CustomerFavoriteProductRepository $customerFavoriteProductRepository,
CartService $cartService,
CategoryRepository $categoryRepository,
ProductReviewRepository $productReviewRepository
) {
$this->formFactory = $formFactory;
$this->productRepository = $productRepository;
$this->security = $security;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
$this->cartService = $cartService;
$this->categoryRepository = $categoryRepository;
$this->productReviewRepository = $productReviewRepository;
}
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onProductList',
'Product/new.twig' => 'onProductList',
'Product/sale.twig' => 'onPinpointProductList',
'Product/detail.twig' => 'onProductDetail',
'@user_data/history_product_list.twig' => 'onProductList',
'Mypage/favorite.twig' => 'onFavorite',
'Block/header.twig' => 'onHeader',
'Block/category_nav_sp.twig' => 'onHeader',
'Block/category_menu_sp.twig' => 'onHeader',
'Block/maker.twig' => 'onMaker',
'Shopping/confirm.twig' => 'onShoppingConfirm'
];
}
public function onShoppingConfirm(TemplateEvent $event) {
$event->addSnippet('Shopping/change_quantity_button.twig');
}
public function onMaker(TemplateEvent $event) {
$ids = [Constant::MANUFACTURER_CATEGORY_ID];
$Categories = $this->categoryRepository->findBy(['Parent' => $ids], ['id' => 'ASC']);
$event->setParameter('MakerCategories', $Categories);
}
public function onProductDetail(TemplateEvent $event)
{
$ParentDefaultCategory = $this->categoryRepository->find(Constant::DEFAULT_CATEGORY_ID);
$ParentMakerCategory = $this->categoryRepository->find(Constant::MANUFACTURER_CATEGORY_ID);
$ChildDefaultCategories = $this->categoryRepository->getList($ParentDefaultCategory, true);
$ChildMakerCategories = $this->categoryRepository->getList($ParentMakerCategory, true);
$defaultCategoryIds = [];
$makerCategoryIds = [];
foreach ($ChildDefaultCategories as $key => $Category) {
array_push($defaultCategoryIds, $Category->getId());
}
foreach ($ChildMakerCategories as $key => $Category) {
array_push($makerCategoryIds, $Category->getId());
}
$parameters = $event->getParameters();
/** @var \Eccube\Entity\Product $Product */
$Product = $parameters['Product'];
$BreadcrumbCategory = null;
$DefaultCategory = null;
$MakerCategory = null;
foreach ($Product->getProductCategories() as $key => $ProductCategory) {
$Category = $ProductCategory->getCategory();
if (!$BreadcrumbCategory || count($Category->getPath()) > count($BreadcrumbCategory->getPath())) {
$BreadcrumbCategory = $Category;
}
if (in_array($Category->getId(), $defaultCategoryIds)) {
if (!$DefaultCategory || count($Category->getPath()) > count($DefaultCategory->getPath())) {
$DefaultCategory = $Category;
}
} else if (in_array($Category->getId(), $makerCategoryIds)) {
if (!$MakerCategory || count($Category->getPath()) > count($MakerCategory->getPath())) {
$MakerCategory = $Category;
}
}
}
$parameters['BreadcrumbCategory'] = $BreadcrumbCategory;
$parameters['DefaultCategory'] = $DefaultCategory;
$parameters['MakerCategory'] = $MakerCategory;
$event->setParameters($parameters);
}
public function onHeader(TemplateEvent $event)
{
$ids = [Constant::DEFAULT_CATEGORY_ID, Constant::MANUFACTURER_CATEGORY_ID];
$Categories = $this->categoryRepository->findBy(['id' => $ids], ['id' => 'ASC']);
$event->setParameter("TopCategories", $Categories);
}
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 onPinpointProductList(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 => $PinpointProduct) {
$Product = $PinpointProduct->getProductClass()->getProduct();
$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 onFavorite(TemplateEvent $event)
{
$parameters = $event->getParameters();
$pagination = $parameters['pagination'];
$ids = [];
$itemInCart = [];
$isFavorite = [];
$isShowProductImage = true;
$ids = [Constant::DEFAULT_CATEGORY_ID, Constant::MANUFACTURER_CATEGORY_ID];
$Categories = $this->categoryRepository->findBy(['id' => $ids], ['id' => 'ASC']);
/** @var \Eccube\Entity\Customer $Customer */
$Customer = $this->security->getUser();
if ($Customer) {
$isShowProductImage = $Customer->isShowProductImage();
}
foreach ($pagination as $FavoriteProduct) {
$Product = $FavoriteProduct->getProduct();
$ids[] = $Product->getId();
}
$ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
$forms = [];
foreach ($pagination as $key => $FavoriteProduct) {
$Product = $FavoriteProduct->getProduct();
if ($Customer) {
$isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product);
}
/* @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();
}
$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['forms'] = $forms;
$parameters['itemInCart'] = $itemInCart;
$parameters['isShowProductImage'] = $isShowProductImage;
$parameters['TopCategories'] = $Categories;
$event->setParameters($parameters);
}
}