<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) Takashi Otaki All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\EtunaCheckedItem;
use Eccube\Event\EventArgs;
use Plugin\EtunaCheckedItem\Repository\EtunaCheckedItemConfigRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class EtunaCheckedItemEvent implements EventSubscriberInterface
{
/**
* @var EtunaCheckedItemConfigRepository
*/
protected $etunaCheckedItemConfigRepository;
/**
* EtunaCheckedItem constructor.
*
* @param EtunaCheckedItemConfigRepository $etunaCheckedItemConfigRepository
*/
public function __construct(
EtunaCheckedItemConfigRepository $etunaCheckedItemConfigRepository)
{
$this->etunaCheckedItemConfigRepository = $etunaCheckedItemConfigRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'front.product.detail.initialize' => 'detail',
];
}
/**
* @param EventArgs $event
*/
public function detail(EventArgs $event)
{
$request = $event->getRequest();
/** @var Product $Product */
$Product = $event->getArgument('Product');
$productId = $Product->getId();
$Config = $this->etunaCheckedItemConfigRepository->get();
$checkedItems = $request->cookies->get('CheckedItemIds');
$checkedItemArray = array();
if ($checkedItems !== null) {
$checkedItemArray = explode("_", $checkedItems);
}
$duplicateKeys = array_keys($checkedItemArray, $productId);
if (count($duplicateKeys) > 0) {
foreach ($duplicateKeys as $duplicateKey) {
unset($checkedItemArray[$duplicateKey]);
}
}
$checkedItemArray[] = $productId;
$checkedItemDays = $Config->getCheckedItemDays();
$checkedItemCount = $Config->getCheckedItemCount();
//設定数以上の場合の削除処理
if ( count($checkedItemArray) > $checkedItemCount) {
$checkedItemArrayCount = count($checkedItemArray) - $checkedItemCount;
if ($checkedItemArrayCount == 1) {
unset($checkedItemArray[0]);
} else if ($checkedItemArrayCount > 1) {
foreach(range(0,$checkedItemArrayCount) as $i){
unset($checkedItemArray[$i]);
}
}
}
$checkedItemArray = implode("_", $checkedItemArray);
setcookie('CheckedItemIds', $checkedItemArray, time() + 3600 * 24 * $checkedItemDays, '/');
}
}