app/Plugin/Recommend42/RecommendEvent.php line 63

Open in your IDE?
  1. <?php
  2. namespace Plugin\Recommend42;
  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 Plugin\Recommend42\Repository\RecommendProductRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. class RecommendEvent implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var FormFactoryInterface
  16.      */
  17.     protected $formFactory;
  18.     /**
  19.      * @var ProductRepository
  20.      */
  21.     protected $productRepository;
  22.     /**
  23.      * @var RecommendProductRepository
  24.      */
  25.     protected $recommendProductRepository;
  26.     protected CartService $cartService;
  27.     protected Security $security;
  28.     protected CustomerFavoriteProductRepository $customerFavoriteProductRepository;
  29.     public function __construct(
  30.         FormFactoryInterface $formFactory,
  31.         ProductRepository $productRepository,
  32.         RecommendProductRepository $recommendProductRepository,
  33.         CartService $cartService,
  34.         Security $security,
  35.         CustomerFavoriteProductRepository $customerFavoriteProductRepository
  36.     ) {
  37.         $this->formFactory $formFactory;
  38.         $this->productRepository $productRepository;
  39.         $this->cartService $cartService;
  40.         $this->recommendProductRepository $recommendProductRepository;
  41.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  42.         $this->security $security;
  43.     
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             'Block/recommend_product_block.twig' => 'onRecommendProduct',
  49.             '@Recommend42/Product/list.twig' => 'onProductList',
  50.         ];
  51.     }
  52.     public function onProductList(TemplateEvent $event)
  53.     {
  54.         $parameters $event->getParameters();
  55.         /** @var  \Eccube\Entity\Customer $Customer */
  56.         $Customer $this->security->getUser();
  57.         $isFavorite = [];
  58.         $itemInCart = [];
  59.         $isShowProductImage true;
  60.         if ($Customer) {
  61.             /** @var SlidingPagination $pagination */
  62.             $pagination $parameters['pagination'];
  63.             foreach ($pagination as $key => $Product) {
  64.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  65.             }
  66.             $isShowProductImage $Customer->isShowProductImage();
  67.         }
  68.         $Carts $this->cartService->getCarts();
  69.         foreach ($Carts as $key => $Cart) {
  70.             $CartItems $Cart->getCartItems();
  71.             foreach ($CartItems as $key => $CartItem) {
  72.                 $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  73.             }
  74.         }
  75.         $parameters['isFavorite'] = $isFavorite;
  76.         $parameters['itemInCart'] = $itemInCart;
  77.         $parameters['isShowProductImage'] = $isShowProductImage;
  78.         $event->setParameters($parameters);
  79.     }
  80.     public function onRecommendProduct(TemplateEvent $event)
  81.     {
  82.         $parameters $event->getParameters();
  83.         $Products $this->recommendProductRepository->getRecommendProduct();
  84.         $ids = [];
  85.         $itemInCart = [];
  86.         $isFavorite = [];
  87.         $Customer $this->security->getUser();
  88.         foreach ($Products as $RecommendProduct) {
  89.             $Product $RecommendProduct->getProduct();
  90.             $ids[] = $Product->getId();
  91.         }
  92.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  93.         
  94.         $forms = [];
  95.         foreach ($Products as $key => $RecommendProduct) {
  96.             $Product $RecommendProduct->getProduct();
  97.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  98.             $builder $this->formFactory->createNamedBuilder(
  99.                 '',
  100.                 AddCartType::class,
  101.                 null,
  102.                 [
  103.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  104.                     'allow_extra_fields' => true,
  105.                 ]
  106.             );
  107.             $addCartForm $builder->getForm();
  108.             $forms[$Product->getId()] = $addCartForm->createView();
  109.             if ($Customer) {
  110.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  111.             }
  112.         }
  113.                 
  114.         $Carts $this->cartService->getCarts();
  115.         foreach ($Carts as $key => $Cart) {
  116.             $CartItems $Cart->getCartItems();
  117.             foreach ($CartItems as $key => $CartItem) {
  118.                 $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  119.             }
  120.         }
  121.         $parameters['forms'] = $forms;
  122.         $parameters['Products'] = $Products;
  123.         $parameters['recommendItemInCart'] = $itemInCart;
  124.         $parameters['isRecommendFavorite'] = $isFavorite;
  125.         $event->setParameters($parameters);
  126.     }
  127. }