app/Plugin/Schedule/EventSubscriber/ProductEventSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2018 SYSTEM_KD
  4.  * Date: 2018/09/30
  5.  */
  6. namespace Plugin\Schedule\EventSubscriber;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Eccube\Entity\Product;
  9. use Eccube\Event\TemplateEvent;
  10. use Eccube\Request\Context;
  11. use Plugin\Schedule\Service\ScheduleHelper;
  12. use Plugin\Schedule\Service\ScheduleService;
  13. use Plugin\Schedule\Utils\ScheduleUtility;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Translation\TranslatorInterface;
  20. /**
  21.  * [フロント]商品
  22.  *
  23.  * Class ProductEventSubscriber
  24.  * @package Plugin\Schedule\EventSubscriber
  25.  */
  26. class ProductEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
  27. {
  28.     /** @var ScheduleService */
  29.     protected $scheduleService;
  30.     /** @var ScheduleUtility  */
  31.     protected $scheduleUtility;
  32.     /** @var ScheduleHelper */
  33.     protected $scheduleHelper;
  34.     /** @var TranslatorInterface */
  35.     protected $translator;
  36.     /** @var EntityManagerInterface */
  37.     protected $entityManager;
  38.     /** @var Context */
  39.     private $context;
  40.     public function __construct(
  41.         ScheduleService $scheduleService,
  42.         ScheduleUtility $scheduleUtility,
  43.         ScheduleHelper $scheduleHelper,
  44.         TranslatorInterface $translator,
  45.         EntityManagerInterface $entityManager,
  46.         SessionInterface $session,
  47.         Context $context
  48.     )
  49.     {
  50.         parent::__construct($session);
  51.         $this->scheduleService $scheduleService;
  52.         $this->scheduleUtility $scheduleUtility;
  53.         $this->scheduleHelper $scheduleHelper;
  54.         $this->translator $translator;
  55.         $this->entityManager $entityManager;
  56.         $this->context $context;
  57.     }
  58.     /**
  59.      * 商品詳細画面コントロール前
  60.      *
  61.      * @param FilterControllerEvent $event
  62.      * @throws \Exception
  63.      */
  64.     public function onProductDetailController(FilterControllerEvent $event)
  65.     {
  66.         $request $event->getRequest();
  67.         $route $request->attributes->get('_route');
  68.         if ($this->context->isAdmin()) {
  69.             // 管理画面へのアクセス
  70.             return;
  71.         }
  72.         if ($this->isAdminUser()) {
  73.             return;
  74.         }
  75.         if ($route == "product_detail") {
  76.             $this->entityManager->getFilters()->disable('category_schedule');
  77.             $product_id $request->attributes->get('id');
  78.             // 商品の有効性チェック
  79.             if (!$this->scheduleService->isActiveTimeProduct($product_id)
  80.                 && !$this->scheduleService->isViewProduct($product_id)) {
  81.                 // 商品詳細画面の場合
  82.                 throw new NotFoundHttpException();
  83.             }
  84.             $filter $this->entityManager->getFilters()->enable('category_schedule');
  85.             $filter->setParameter('now_time'$this->scheduleUtility->getNowUTCTime());
  86.         }
  87.     }
  88.     /**
  89.      * 商品一覧テンプレート
  90.      *
  91.      * @param TemplateEvent $event
  92.      * @throws \Exception
  93.      */
  94.     public function onTemplateProductList(TemplateEvent $event)
  95.     {
  96.         // 表示制御追加
  97.         $event->addSnippet('@Schedule/default/Product/list_ex.twig');
  98.         $filter $this->entityManager->getFilters()->enable('category_schedule');
  99.         $filter->setParameter('now_time'$this->scheduleUtility->getNowUTCTime());
  100.     }
  101.     /**
  102.      * 商品詳細テンプレート
  103.      *
  104.      * @param TemplateEvent $event
  105.      * @throws \Exception
  106.      */
  107.     public function onTemplateProductDetail(TemplateEvent $event)
  108.     {
  109.         // 表示制御追加
  110.         $event->addSnippet('@Schedule/default/Product/detail_ex.twig');
  111.         /** @var Product $Product */
  112.         $Product $event->getParameter('Product');
  113.         $schedule_wait_end false;
  114.         if ($this->scheduleService->isViewProduct($Product)
  115.             && !$this->scheduleService->isActiveTimeProduct($Product)) {
  116.             // 公開待ちor販売終了状態
  117.             $schedule_wait_end true;
  118.             if ($this->scheduleService->isActiveTimeProductEnd($Product)) {
  119.                 // 販売終了
  120.                 if($this->scheduleService->isScheduleTypeTimeOnly($Product)) {
  121.                     // 時間設定
  122.                     $wait_message $this->translator->trans('schedule.product_time_only_end');
  123.                 } else {
  124.                     // 期間設定
  125.                     $wait_message $this->translator->trans('schedule.product_time_end');
  126.                 }
  127.             } else {
  128.                 // 公開待ち用メッセージ
  129.                 if($this->scheduleService->isScheduleTypeTimeOnly($Product)) {
  130.                     // 時間設定
  131.                     $wait_message $this->translator->trans('schedule.product_time_only_wait', [
  132.                         '%time_from%' => $this->scheduleHelper->getTimeFormat($Product->getScheduleTime()->getTimeOnlyFrom())
  133.                     ]);
  134.                 } else {
  135.                     // 期間設定
  136.                     $wait_message $this->translator->trans('schedule.product_time_wait');
  137.                 }
  138.             }
  139.             $event->setParameter('wait_end_message'$wait_message);
  140.         }
  141.         $event->setParameter('schedule_wait_end'$schedule_wait_end);
  142.         $activeScheduleTime $this->scheduleService->getActiveScheduleTime($Product);
  143.         if ($activeScheduleTime->isTimeViewFlg()) {
  144.             // 表示追加
  145.             $event->addSnippet('@Schedule/default/Product/detail_add.twig');
  146.         }
  147.     }
  148.     /**
  149.      * Returns an array of event names this subscriber wants to listen to.
  150.      *
  151.      * The array keys are event names and the value can be:
  152.      *
  153.      *  * The method name to call (priority defaults to 0)
  154.      *  * An array composed of the method name to call and the priority
  155.      *  * An array of arrays composed of the method names to call and respective
  156.      *    priorities, or 0 if unset
  157.      *
  158.      * For instance:
  159.      *
  160.      *  * array('eventName' => 'methodName')
  161.      *  * array('eventName' => array('methodName', $priority))
  162.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  163.      *
  164.      * @return array The event names to listen to
  165.      */
  166.     public static function getSubscribedEvents()
  167.     {
  168.         return [
  169.             KernelEvents::CONTROLLER => [
  170.                 'onProductDetailController',
  171.             ],
  172.             'Product/list.twig' => [
  173.                 ['onTemplateProductList'10]
  174.             ],
  175.             'Product/detail.twig' => [
  176.                 'onTemplateProductDetail'
  177.             ],
  178.         ];
  179.     }
  180. }