app/Plugin/ScheduleDx/EventSubscriber/ProductEventSubscriber.php line 125

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