app/Customize/Event/HistoryProductEvent.php line 52

Open in your IDE?
  1. <?php
  2. namespace Customize\Event;
  3. use Eccube\Event\TemplateEvent;
  4. use Eccube\Form\Type\AddCartType;
  5. use Eccube\Repository\CustomerFavoriteProductRepository;
  6. use Eccube\Repository\ProductRepository;
  7. use Eccube\Service\CartService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormFactoryInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class HistoryProductEvent implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var FormFactoryInterface
  15.      */
  16.     protected $formFactory;
  17.     /**
  18.      * @var ProductRepository
  19.      */
  20.     protected $productRepository;
  21.     protected Security $security;
  22.     protected CartService $cartService;
  23.     protected CustomerFavoriteProductRepository $customerFavoriteProductRepository;
  24.     public function __construct(
  25.         FormFactoryInterface $formFactory,
  26.         ProductRepository $productRepository,
  27.         Security $security,
  28.         CartService $cartService,
  29.         CustomerFavoriteProductRepository $customerFavoriteProductRepository
  30.     ) {
  31.         $this->formFactory $formFactory;
  32.         $this->productRepository $productRepository;
  33.         $this->security $security;
  34.         $this->cartService $cartService;
  35.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             'Block/history_product.twig' => 'onHistoryProduct',
  41.         ];
  42.     }
  43.     public function onHistoryProduct(TemplateEvent $event)
  44.     {
  45.         $Customer $this->security->getUser();
  46.         $isFavorite = [];
  47.         if ($Customer) {
  48.             $parameters $event->getParameters();
  49.             $Products $this->productRepository->getHistoryProduct($Customer);
  50.             $ids = [];
  51.             $itemInCart = [];
  52.     
  53.             foreach ($Products as $Product) {
  54.                 $ids[] = $Product->getId();
  55.             }
  56.     
  57.             $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  58.             
  59.             $forms = [];
  60.             foreach ($Products as $key => $Product) {
  61.                 /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  62.                 $builder $this->formFactory->createNamedBuilder(
  63.                     '',
  64.                     AddCartType::class,
  65.                     null,
  66.                     [
  67.                         'product' => $ProductsAndClassCategories[$Product->getId()],
  68.                         'allow_extra_fields' => true,
  69.                     ]
  70.                 );
  71.                 $addCartForm $builder->getForm();
  72.     
  73.                 $forms[$Product->getId()] = $addCartForm->createView();
  74.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  75.             }
  76.             $Carts $this->cartService->getCarts();
  77.             foreach ($Carts as $key => $Cart) {
  78.                 $CartItems $Cart->getCartItems();
  79.                 foreach ($CartItems as $key => $CartItem) {
  80.                     $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  81.                 }
  82.             }
  83.             
  84.             $parameters['history_forms'] = $forms;
  85.             $parameters['Products'] = $Products;
  86.             $parameters['historyItemInCart'] = $itemInCart;
  87.             $parameters['isHistoryFavorite'] = $isFavorite;
  88.             $event->setParameters($parameters);
  89.         }
  90.     }
  91. }