app/Customize/Event/CustomizeEvent.php line 172

Open in your IDE?
  1. <?php
  2. namespace Customize\Event;
  3. use Customize\Common\Constant;
  4. use Eccube\Event\TemplateEvent;
  5. use Eccube\Form\Type\AddCartType;
  6. use Eccube\Repository\CategoryRepository;
  7. use Eccube\Repository\CustomerFavoriteProductRepository;
  8. use Eccube\Repository\ProductRepository;
  9. use Eccube\Service\CartService;
  10. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  11. use Plugin\ProductReview42\Repository\ProductReviewRepository;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. class CustomizeEvent implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var FormFactoryInterface
  19.      */
  20.     protected $formFactory;
  21.     /**
  22.      * @var ProductRepository
  23.      */
  24.     protected $productRepository;
  25.     protected Security $security;
  26.     protected CustomerFavoriteProductRepository $customerFavoriteProductRepository;
  27.     protected CartService $cartService;
  28.     protected CategoryRepository $categoryRepository;
  29.     protected ProductReviewRepository $productReviewRepository;
  30.     public function __construct(
  31.         FormFactoryInterface $formFactory,
  32.         ProductRepository $productRepository,
  33.         Security $security,
  34.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  35.         CartService $cartService,
  36.         CategoryRepository $categoryRepository,
  37.         ProductReviewRepository $productReviewRepository
  38.     ) {
  39.         $this->formFactory $formFactory;
  40.         $this->productRepository $productRepository;
  41.         $this->security $security;
  42.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  43.         $this->cartService $cartService;
  44.         $this->categoryRepository $categoryRepository;
  45.         $this->productReviewRepository $productReviewRepository;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             'Product/list.twig' => 'onProductList',
  51.             'Product/new.twig' => 'onProductList',
  52.             'Product/sale.twig' => 'onPinpointProductList',
  53.             'Product/detail.twig' => 'onProductDetail',
  54.             '@user_data/history_product_list.twig' => 'onProductList',
  55.             'Mypage/favorite.twig' => 'onFavorite',
  56.             'Block/header.twig' => 'onHeader',
  57.             'Block/category_nav_sp.twig' => 'onHeader',
  58.             'Block/category_menu_sp.twig' => 'onHeader',
  59.             'Block/maker.twig' => 'onMaker',
  60.             'Shopping/confirm.twig' => 'onShoppingConfirm'
  61.         ];
  62.     }
  63.     public function onShoppingConfirm(TemplateEvent $event) {
  64.         $event->addSnippet('Shopping/change_quantity_button.twig');
  65.     }
  66.     public function onMaker(TemplateEvent $event) {
  67.         $ids = [Constant::MANUFACTURER_CATEGORY_ID];
  68.         $Categories $this->categoryRepository->findBy(['Parent' => $ids], ['id' => 'ASC']);
  69.         $event->setParameter('MakerCategories'$Categories);
  70.     }
  71.     public function onProductDetail(TemplateEvent $event)
  72.     {
  73.         $ParentDefaultCategory $this->categoryRepository->find(Constant::DEFAULT_CATEGORY_ID);
  74.         $ParentMakerCategory $this->categoryRepository->find(Constant::MANUFACTURER_CATEGORY_ID);
  75.         $ChildDefaultCategories $this->categoryRepository->getList($ParentDefaultCategorytrue);
  76.         $ChildMakerCategories $this->categoryRepository->getList($ParentMakerCategorytrue);
  77.         $defaultCategoryIds = [];
  78.         $makerCategoryIds = [];
  79.         foreach ($ChildDefaultCategories as $key => $Category) {
  80.             array_push($defaultCategoryIds$Category->getId());
  81.         }
  82.         foreach ($ChildMakerCategories as $key => $Category) {
  83.             array_push($makerCategoryIds$Category->getId());
  84.         }
  85.         $parameters $event->getParameters();
  86.         /** @var \Eccube\Entity\Product $Product */
  87.         $Product $parameters['Product'];
  88.         $BreadcrumbCategory null;
  89.         $DefaultCategory null;
  90.         $MakerCategory null;
  91.         foreach ($Product->getProductCategories() as $key => $ProductCategory) {
  92.             $Category $ProductCategory->getCategory();
  93.             if (!$BreadcrumbCategory || count($Category->getPath()) > count($BreadcrumbCategory->getPath())) {
  94.                 $BreadcrumbCategory $Category;
  95.             }
  96.             if (in_array($Category->getId(), $defaultCategoryIds)) {
  97.                 if (!$DefaultCategory || count($Category->getPath()) > count($DefaultCategory->getPath())) {
  98.                     $DefaultCategory $Category;
  99.                 }
  100.             } else if (in_array($Category->getId(), $makerCategoryIds)) {
  101.                 if (!$MakerCategory || count($Category->getPath()) > count($MakerCategory->getPath())) {
  102.                     $MakerCategory $Category;
  103.                 }
  104.             }
  105.         }
  106.         $parameters['BreadcrumbCategory'] = $BreadcrumbCategory;
  107.         $parameters['DefaultCategory'] = $DefaultCategory;
  108.         $parameters['MakerCategory'] = $MakerCategory;
  109.         $event->setParameters($parameters);
  110.     }
  111.     
  112.     public function onHeader(TemplateEvent $event)
  113.     {
  114.         $ids = [Constant::DEFAULT_CATEGORY_IDConstant::MANUFACTURER_CATEGORY_ID];
  115.         $Categories $this->categoryRepository->findBy(['id' => $ids], ['id' => 'ASC']);
  116.         $event->setParameter("TopCategories"$Categories);
  117.     }
  118.     public function onProductList(TemplateEvent $event)
  119.     {
  120.         $parameters $event->getParameters();
  121.         /** @var  \Eccube\Entity\Customer $Customer */
  122.         $Customer $this->security->getUser();
  123.         $isFavorite = [];
  124.         $itemInCart = [];
  125.         $isShowProductImage true;
  126.         if ($Customer) {
  127.             /** @var SlidingPagination $pagination */
  128.             $pagination $parameters['pagination'];
  129.             foreach ($pagination as $key => $Product) {
  130.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  131.             }
  132.             $isShowProductImage $Customer->isShowProductImage();
  133.         }
  134.         $Carts $this->cartService->getCarts();
  135.         foreach ($Carts as $key => $Cart) {
  136.             $CartItems $Cart->getCartItems();
  137.             foreach ($CartItems as $key => $CartItem) {
  138.                 $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  139.             }
  140.         }
  141.         $parameters['isFavorite'] = $isFavorite;
  142.         $parameters['itemInCart'] = $itemInCart;
  143.         $parameters['isShowProductImage'] = $isShowProductImage;
  144.         $event->setParameters($parameters);
  145.     }
  146.     public function onPinpointProductList(TemplateEvent $event)
  147.     {
  148.         $parameters $event->getParameters();
  149.         /** @var  \Eccube\Entity\Customer $Customer */
  150.         $Customer $this->security->getUser();
  151.         $isFavorite = [];
  152.         $itemInCart = [];
  153.         $isShowProductImage true;
  154.         if ($Customer) {
  155.             /** @var SlidingPagination $pagination */
  156.             $pagination $parameters['pagination'];
  157.             foreach ($pagination as $key => $PinpointProduct) {
  158.                 $Product $PinpointProduct->getProductClass()->getProduct();
  159.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  160.             }
  161.             $isShowProductImage $Customer->isShowProductImage();
  162.         }
  163.         $Carts $this->cartService->getCarts();
  164.         foreach ($Carts as $key => $Cart) {
  165.             $CartItems $Cart->getCartItems();
  166.             foreach ($CartItems as $key => $CartItem) {
  167.                 $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  168.             }
  169.         }
  170.         $parameters['isFavorite'] = $isFavorite;
  171.         $parameters['itemInCart'] = $itemInCart;
  172.         $parameters['isShowProductImage'] = $isShowProductImage;
  173.         $event->setParameters($parameters);
  174.     }
  175.     public function onFavorite(TemplateEvent $event)
  176.     {
  177.         $parameters $event->getParameters();
  178.         $pagination $parameters['pagination'];
  179.         $ids = [];
  180.         $itemInCart = [];
  181.         $isFavorite = [];
  182.         $isShowProductImage true;
  183.         $ids = [Constant::DEFAULT_CATEGORY_IDConstant::MANUFACTURER_CATEGORY_ID];
  184.         $Categories $this->categoryRepository->findBy(['id' => $ids], ['id' => 'ASC']);
  185.         /** @var  \Eccube\Entity\Customer $Customer */
  186.         $Customer $this->security->getUser();
  187.         if ($Customer) {
  188.             $isShowProductImage $Customer->isShowProductImage();
  189.         }
  190.         foreach ($pagination as $FavoriteProduct) {
  191.             $Product $FavoriteProduct->getProduct();
  192.             $ids[] = $Product->getId();
  193.         }
  194.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  195.         
  196.         $forms = [];
  197.         foreach ($pagination as $key => $FavoriteProduct) {
  198.             $Product $FavoriteProduct->getProduct();
  199.             if ($Customer) {
  200.                 $isFavorite[$Product->getId()] = $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  201.             }
  202.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  203.             $builder $this->formFactory->createNamedBuilder(
  204.                 '',
  205.                 AddCartType::class,
  206.                 null,
  207.                 [
  208.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  209.                     'allow_extra_fields' => true,
  210.                 ]
  211.             );
  212.             $addCartForm $builder->getForm();
  213.             $forms[$Product->getId()] = $addCartForm->createView();
  214.         }
  215.         $Carts $this->cartService->getCarts();
  216.         foreach ($Carts as $key => $Cart) {
  217.             $CartItems $Cart->getCartItems();
  218.             foreach ($CartItems as $key => $CartItem) {
  219.                 $itemInCart[$CartItem->getProductClass()->getProduct()->getId()] = $CartItem->getQuantity();
  220.             }
  221.         }
  222.         $parameters['isFavorite'] = $isFavorite;
  223.         $parameters['forms'] = $forms;
  224.         $parameters['itemInCart'] = $itemInCart;
  225.         $parameters['isShowProductImage'] = $isShowProductImage;
  226.         $parameters['TopCategories'] = $Categories;
  227.         $event->setParameters($parameters);
  228.     }
  229. }