app/Plugin/BannerManagement42/Event.php line 62

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of BannerManagement42
  4.  *
  5.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  6.  *
  7.  * https://umebius.com/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\BannerManagement42;
  13. use Detection\MobileDetect;
  14. use Eccube\Event\TemplateEvent;
  15. use Plugin\BannerManagement42\Repository\BannerRepository;
  16. use Plugin\BannerManagement42\Repository\ConfigRepository;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class Event implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var BannerRepository
  22.      */
  23.     protected $bannerRepository;
  24.     /**
  25.      * @var MobileDetect
  26.      */
  27.     protected $mobileDetector;
  28.     /**
  29.      * @var ConfigRepository
  30.      */
  31.     private $configRepository;
  32.     /**
  33.      * コンストラクタ
  34.      */
  35.     public function __construct(
  36.         BannerRepository $bannerRepository,
  37.         ConfigRepository $configRepository,
  38.         MobileDetect $mobileDetector
  39.     ) {
  40.         $this->bannerRepository $bannerRepository;
  41.         $this->configRepository $configRepository;
  42.         $this->mobileDetector $mobileDetector;
  43.     }
  44.     /**
  45.      * @return array
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             'index.twig' => ['onIndexTwig'],
  51.         ];
  52.     }
  53.     public function onIndexTwig(TemplateEvent $event)
  54.     {
  55.         $PcBanners $this->bannerRepository->getBanners(1true);
  56.         $SpBanners $this->bannerRepository->getBanners(2true);
  57.         $event->setParameter('PcBanners'$PcBanners);
  58.         $event->setParameter('SpBanners'$SpBanners);
  59.         if ($this->mobileDetector->isMobile()) {
  60.             $Banners $SpBanners;
  61.             if (empty($Banners)) {
  62.                 $Banners $PcBanners;
  63.             }
  64.         } else {
  65.             $Banners $PcBanners;
  66.         }
  67.         $event->setParameter('TopBanners'$Banners);
  68.         if (count($Banners)) {
  69.             $Config $this->configRepository->get();
  70.             if ($Config && $Config->getReplaceAutomatically()) {
  71.                 $event->addAsset('@BannerManagement42/index_css.twig');
  72.                 $event->addSnippet('@BannerManagement42/index_slider.twig');
  73.             }
  74.         }
  75.     }
  76. }