app/Plugin/PiaFavorite/Event.php line 64

Open in your IDE?
  1. <?php
  2. /**
  3.  * 共通で使うカテゴリーなどを各ページに設定
  4.  */
  5. namespace Plugin\PiaFavorite;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Eccube\Event\TemplateEvent;
  8. use Eccube\Repository\CustomerFavoriteProductRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. /**
  13.  * Class Event.
  14.  */
  15. class Event implements EventSubscriberInterface {
  16.   private $authorizationChecker;
  17.   private $tokenStorage;
  18.   /**
  19.    * @var EntityManagerInterface
  20.    */
  21.   private $entityManager;
  22.   /**
  23.    * @var CustomerFavoriteProductRepository
  24.    */
  25.   protected $customerFavoriteProductRepository;
  26.   /**
  27.    * Event constructor.
  28.    *
  29.    * @param EntityManagerInterface $entityManager
  30.    * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  31.    * @param \Twig\Environment $twig
  32.    */
  33.   public function __construct(
  34.   AuthorizationCheckerInterface $authorizationCheckerTokenStorageInterface $tokenStorageEntityManagerInterface $entityManagerCustomerFavoriteProductRepository $customerFavoriteProductRepository) {
  35.     $this->authorizationChecker $authorizationChecker;
  36.     $this->tokenStorage $tokenStorage;
  37.     $this->entityManager $entityManager;
  38.     $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  39.   }
  40.   /**
  41.    * Todo: admin.order.delete.complete has been deleted.
  42.    *
  43.    * @return array
  44.    */
  45.   public static function getSubscribedEvents() {
  46.     return [
  47.       'Product/list.twig' => 'onProductListRender',
  48.     ];
  49.   }
  50.   /**
  51.    * 絞込み検索結果を商品一覧ページに設定
  52.    * @param TemplateEvent $event
  53.    */
  54.   public function onProductListRender(TemplateEvent $event) {
  55.     $parameters $event->getParameters();
  56.     $htFavorite = array();
  57.     $Customer $this->getLoginCustomer();
  58.     if (!empty($Customer)) {
  59.       $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  60.       $rows $qb->getQuery()->getResult();
  61.       if(is_array($rows) and count($rows) > 0){
  62.         foreach($rows as $row){
  63.           $Product $row->getProduct();
  64.           if(empty($Product)){
  65.             continue;
  66.           }
  67.           $product_id $Product->getId();
  68.           $htFavorite[$product_id] = true;
  69.         }
  70.       }
  71.     }
  72.     $parameters["htFavorite"] = $htFavorite;
  73.     $event->setParameters($parameters);
  74.   }
  75.   
  76.   
  77.   public function getLoginCustomer() {
  78.     try {
  79.       if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  80.         $Customer $this->tokenStorage->getToken()->getUser();
  81.         return $Customer;
  82.       }
  83.     } catch (AuthenticationCredentialsNotFoundException $e) {
  84.       
  85.     }
  86.     return null;
  87.   }
  88. }