<?php
/**
* Copyright(c) 2018 SYSTEM_KD
* Date: 2018/09/30
*/
namespace Plugin\Schedule\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Product;
use Eccube\Event\TemplateEvent;
use Eccube\Request\Context;
use Plugin\Schedule\Service\ScheduleHelper;
use Plugin\Schedule\Service\ScheduleService;
use Plugin\Schedule\Utils\ScheduleUtility;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Translation\TranslatorInterface;
/**
* [フロント]商品
*
* Class ProductEventSubscriber
* @package Plugin\Schedule\EventSubscriber
*/
class ProductEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
{
/** @var ScheduleService */
protected $scheduleService;
/** @var ScheduleUtility */
protected $scheduleUtility;
/** @var ScheduleHelper */
protected $scheduleHelper;
/** @var TranslatorInterface */
protected $translator;
/** @var EntityManagerInterface */
protected $entityManager;
/** @var Context */
private $context;
public function __construct(
ScheduleService $scheduleService,
ScheduleUtility $scheduleUtility,
ScheduleHelper $scheduleHelper,
TranslatorInterface $translator,
EntityManagerInterface $entityManager,
SessionInterface $session,
Context $context
)
{
parent::__construct($session);
$this->scheduleService = $scheduleService;
$this->scheduleUtility = $scheduleUtility;
$this->scheduleHelper = $scheduleHelper;
$this->translator = $translator;
$this->entityManager = $entityManager;
$this->context = $context;
}
/**
* 商品詳細画面コントロール前
*
* @param FilterControllerEvent $event
* @throws \Exception
*/
public function onProductDetailController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if ($this->context->isAdmin()) {
// 管理画面へのアクセス
return;
}
if ($this->isAdminUser()) {
return;
}
if ($route == "product_detail") {
$this->entityManager->getFilters()->disable('category_schedule');
$product_id = $request->attributes->get('id');
// 商品の有効性チェック
if (!$this->scheduleService->isActiveTimeProduct($product_id)
&& !$this->scheduleService->isViewProduct($product_id)) {
// 商品詳細画面の場合
throw new NotFoundHttpException();
}
$filter = $this->entityManager->getFilters()->enable('category_schedule');
$filter->setParameter('now_time', $this->scheduleUtility->getNowUTCTime());
}
}
/**
* 商品一覧テンプレート
*
* @param TemplateEvent $event
* @throws \Exception
*/
public function onTemplateProductList(TemplateEvent $event)
{
// 表示制御追加
$event->addSnippet('@Schedule/default/Product/list_ex.twig');
$filter = $this->entityManager->getFilters()->enable('category_schedule');
$filter->setParameter('now_time', $this->scheduleUtility->getNowUTCTime());
}
/**
* 商品詳細テンプレート
*
* @param TemplateEvent $event
* @throws \Exception
*/
public function onTemplateProductDetail(TemplateEvent $event)
{
// 表示制御追加
$event->addSnippet('@Schedule/default/Product/detail_ex.twig');
/** @var Product $Product */
$Product = $event->getParameter('Product');
$schedule_wait_end = false;
if ($this->scheduleService->isViewProduct($Product)
&& !$this->scheduleService->isActiveTimeProduct($Product)) {
// 公開待ちor販売終了状態
$schedule_wait_end = true;
if ($this->scheduleService->isActiveTimeProductEnd($Product)) {
// 販売終了
if($this->scheduleService->isScheduleTypeTimeOnly($Product)) {
// 時間設定
$wait_message = $this->translator->trans('schedule.product_time_only_end');
} else {
// 期間設定
$wait_message = $this->translator->trans('schedule.product_time_end');
}
} else {
// 公開待ち用メッセージ
if($this->scheduleService->isScheduleTypeTimeOnly($Product)) {
// 時間設定
$wait_message = $this->translator->trans('schedule.product_time_only_wait', [
'%time_from%' => $this->scheduleHelper->getTimeFormat($Product->getScheduleTime()->getTimeOnlyFrom())
]);
} else {
// 期間設定
$wait_message = $this->translator->trans('schedule.product_time_wait');
}
}
$event->setParameter('wait_end_message', $wait_message);
}
$event->setParameter('schedule_wait_end', $schedule_wait_end);
$activeScheduleTime = $this->scheduleService->getActiveScheduleTime($Product);
if ($activeScheduleTime->isTimeViewFlg()) {
// 表示追加
$event->addSnippet('@Schedule/default/Product/detail_add.twig');
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => [
'onProductDetailController',
],
'Product/list.twig' => [
['onTemplateProductList', 10]
],
'Product/detail.twig' => [
'onTemplateProductDetail'
],
];
}
}