<?php
namespace Plugin\tbsTrialProduct42;
use Eccube\Entity\Customer;
use Eccube\Entity\ProductClass;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\tbsTrialProduct42\Repository\tbsTrialProductRepository;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class tbsTrialProductEvent implements EventSubscriberInterface
{
/** @var tbsTrialProductRepository */
private $tbsTrialProductRepository;
/** @var ContainerInterface */
private $container;
/** @var Customer */
private $user;
/**
* tbsTrialProductEvent constructor.
*
* @param tbsTrialProductRepository $tbsTrialProductRepository
* @param ContainerInterface $container
*/
public function __construct(tbsTrialProductRepository $tbsTrialProductRepository, ContainerInterface $container)
{
$this->tbsTrialProductRepository = $tbsTrialProductRepository;
$this->container = $container;
$token = $this->container->get('security.token_storage')->getToken();
if ($token) {
$this->user = $token->getUser();
}
}
/**
* 商品一覧でお試し商品を非表示にする.
*
* @param EventArgs $event
*/
public function onRenderProductIndexSearch(EventArgs $event)
{
$qb = $event->getArgument('qb');
// DBから設定情報を取得
$template = $this->tbsTrialProductRepository->findOne();
// ログイン済みかつ購入履歴がある場合は対象商品を非表示
if ($template && $template['first_only'] == 1 && $this->user && $this->user != 'anon.' && $this->user->getBuyTimes() > 0) {
// 商品規格がjoinされていない場合はここでjoin
$isJoin = false;
$joins = $qb->getDQLPart('join');
if ($joins && !empty($joins['p'])) {
foreach ($joins['p'] as $join) {
if ($join->getAlias() == 'pc') {
$isJoin = true;
break;
}
}
}
if (!$isJoin) {
$qb->innerJoin('p.ProductClasses', 'pc');
}
// お試し商品以外を表示
$qb
->leftJoin('pc.SaleType', 'pt')
->andWhere('pt.id!=:id')
->setParameter('id', $template['ProductSaleType']->getId());
}
}
/**
* 商品一覧のカートボタンを変更する.
*
* @param TemplateEvent $event
*/
public function onRenderProductList(TemplateEvent $event)
{
$parameters = $event->getParameters();
$pagination = $parameters['pagination'];
if (empty($pagination)) {
return;
}
// DBから設定情報を取得
$template = $this->tbsTrialProductRepository->findOne();
// 未ログインの場合と購入履歴がある場合は対象販売種別のカートボタンを非アクティブ化
if ($template && $template['first_only'] == 1) {
$cartButtonName = null;
// 未ログインの場合
if (!$this->user || $this->user == 'anon.') {
$cartButtonName = trans('tbs_trial_product.front.cart.button.login');
// 購入履歴がある場合
} elseif ($this->user->getBuyTimes() > 0) {
$cartButtonName = trans('tbs_trial_product.front.cart.button.first_only');
}
if ($cartButtonName) {
$parameters = $event->getParameters();
$parameters['ProductSaleTypeId'] = $template['ProductSaleType']->getId();
$parameters['cartButtonName'] = $cartButtonName;
$event->setParameters($parameters);
$event->addSnippet('@tbsTrialProduct42/default/product_trial_list.twig');
}
}
}
/**
* 商品詳細のカートボタンを変更する.
*
* @param TemplateEvent $event
*/
public function onRenderProductDetail(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Product = $parameters['Product'];
if (is_null($Product)) {
return;
}
// DBから設定情報を取得
$template = $this->tbsTrialProductRepository->findOne();
// 未ログインの場合と購入履歴がある場合は対象販売種別のカートボタンを非アクティブ化
if ($template && $template['first_only'] == 1) {
$cartButtonName = null;
// 未ログインの場合
if (!$this->user || $this->user == 'anon.') {
$cartButtonName = trans('tbs_trial_product.front.cart.button.login');
// 購入履歴がある場合
} elseif ($this->user->getBuyTimes() > 0) {
$cartButtonName = trans('tbs_trial_product.front.cart.button.first_only');
}
if ($cartButtonName) {
// 規格なし商品の場合
if (!$Product->hasProductClass()) {
$ProductClasses = $Product->getProductClasses();
/** @var ProductClass $ProductClass */
$ProductClass = $ProductClasses[0];
if ($template['ProductSaleType']->getId() == $ProductClass->getSaleType()->getId()) {
$parameters = $event->getParameters();
$parameters['cartButtonName'] = $cartButtonName;
$event->setParameters($parameters);
$event->addSnippet('@tbsTrialProduct42/default/product_trial_detail.twig');
}
} // 規格あり商品の場合
else {
$parameters = $event->getParameters();
$parameters['ProductSaleTypeId'] = $template['ProductSaleType']->getId();
$parameters['cartButtonName'] = $cartButtonName;
$event->setParameters($parameters);
$event->addSnippet('@tbsTrialProduct42/default/product_trial_detail_class.twig');
}
}
}
}
/**
* 注文フローでゲスト購入を非表示にする.
*
* @param TemplateEvent $event
*/
public function onRenderShoppingLogin(TemplateEvent $event)
{
// DBから設定情報を取得
$template = $this->tbsTrialProductRepository->findOne();
// 会員登録必須の場合はゲスト購入を非表示
if ($template && $template['registration_required'] == 1) {
$event->addSnippet('@tbsTrialProduct42/default/product_trial_login.twig');
}
}
/**
* 再注文を不可にする.
*
* @param TemplateEvent $event
*/
public function onRenderMypageHistory(TemplateEvent $event)
{
$event->addSnippet('@tbsTrialProduct42/default/product_trial_mypage_history.twig');
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
// 'front.product.index.search' => 'onRenderProductIndexSearch',
'Product/list.twig' => 'onRenderProductList',
'Product/detail.twig' => 'onRenderProductDetail',
'Shopping/login.twig' => 'onRenderShoppingLogin',
'Mypage/history.twig' => 'onRenderMypageHistory',
];
}
}