app/Plugin/Collection/Event.php line 43

Open in your IDE?
  1. <?php
  2. namespace Plugin\Collection;
  3. use Eccube\Event\EccubeEvents;
  4. use Eccube\Event\EventArgs;
  5. use Plugin\Collection\Repository\CollectionRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Eccube\Event\TemplateEvent;
  8. class Event implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var CollectionRepository
  12.      */
  13.     protected $collectionRepository;
  14.     /**
  15.      * Event constructor.
  16.      *
  17.      * @param CollectionRepository $collectionRepository
  18.      */
  19.     public function __construct(CollectionRepository $collectionRepository) {
  20.         $this->collectionRepository $collectionRepository;
  21.     }
  22.     /**
  23.      * @return array
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH => 'onFrontProductIndexSearch',
  29.             'Product/list.twig' => 'collectionList',
  30.         ];
  31.     }
  32.     public function collectionList(TemplateEvent  $event)
  33.     {
  34.         $event->addSnippet('@Collection/collection_list.twig');
  35.     }
  36.     public function onFrontProductIndexSearch(EventArgs $event)
  37.     {
  38.         $qb $event->getArgument('qb');
  39.         $collectionCode $event->getRequest()->get('collection');
  40.         if ($collectionCode !== null) {
  41.             $cqb $this->collectionRepository->createQueryBuilder('c');
  42.             $today = (new \DateTime('today'))->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
  43.             // display_from and display_to
  44.             $cqb
  45.                 ->andWhere(
  46.                     $cqb->expr()->orX(
  47.                         $cqb->expr()->andX(
  48.                             $cqb->expr()->isNotNull('c.display_from'),
  49.                             $cqb->expr()->isNotNull('c.display_to'),
  50.                             $cqb->expr()->andX(
  51.                                 $cqb->expr()->lte('c.display_from'"'{$today}'"),
  52.                                 $cqb->expr()->lte("'{$today}'"'c.display_to')
  53.                             )
  54.                         ),
  55.                         $cqb->expr()->andX(
  56.                             $cqb->expr()->isNotNull('c.display_from'),
  57.                             $cqb->expr()->isNull('c.display_to'),
  58.                             $cqb->expr()->lte('c.display_from'"'{$today}'")
  59.                         ),
  60.                         $cqb->expr()->andX(
  61.                             $cqb->expr()->isNull('c.display_from'),
  62.                             $cqb->expr()->isNotNull('c.display_to'),
  63.                             $cqb->expr()->lte("'{$today}'"'c.display_to')
  64.                         ),
  65.                         $cqb->expr()->andX(
  66.                             $cqb->expr()->isNull('c.display_from'),
  67.                             $cqb->expr()->isNull('c.display_to')
  68.                         )
  69.                     )
  70.                 );
  71.             $cqb->andWhere('c.collection_code = :collection_code')
  72.                 ->andWhere('c.visible = true')
  73.                 ->andWhere('c.deleted = false')
  74.                 ->setParameter('collection_code'$collectionCode);
  75.             $Collection $cqb->getQuery()->getOneOrNullResult();
  76.             if ($Collection === null) {
  77.                 // wrong collection_code
  78.                 return;
  79.             }
  80.             $productIds $Collection->getCollectionProducts()->map(function ($CollectionProduct) {
  81.                 return $CollectionProduct->getProduct()->getId();
  82.             })->toArray();
  83.             $qb
  84.                 ->andWhere($qb->expr()->in('p.id'$productIds));
  85.         }
  86.     }
  87. }