<?php
namespace Customize\Controller;
use Customize\Enum\SeasonCode;
use Customize\Repository\OrderOptionRepository;
use Customize\Repository\PriceRepository;
use Customize\Repository\SeasonRepository;
use Customize\Repository\SendDataMethodCategoryRepository;
use Customize\Repository\SimulatorLinkRepository;
use Customize\Repository\StepOptionRepository;
use Customize\Repository\WorkingTimeRepository;
use DateTime;
use Eccube\Repository\NewsRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Eccube\Repository\ProductRepository;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Product;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\ProductCategory;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Eccube\Repository\ProductCategoryRepository;
use Eccube\Service\OrderHelper;
use Symfony\Component\HttpFoundation\Request;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ProductHomeController extends AbstractController
{
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var ProductCategoryRepository
*/
protected $productCategoryRepository;
/**
* @var CustomerFavoriteProductRepository
*/
protected $customerFavoriteProductRepository;
/**
* @var StepOptionRepository
*/
protected $stepOptionRepository;
protected $priceRepository;
protected $orderHelper;
protected $sendDataMethodCategoryRepository;
protected $orderOptionRepository;
protected $seasonRepository;
protected $simulatorLinkRepository;
protected $workingTimeRepository;
public function __construct(
ProductRepository $productRepository,
CustomerFavoriteProductRepository $customerFavoriteProductRepository,
ProductCategoryRepository $productCategoryRepository,
StepOptionRepository $stepOptionRepository,
PriceRepository $priceRepository,
OrderHelper $orderHelper,
SendDataMethodCategoryRepository $sendDataMethodCategoryRepository,
OrderOptionRepository $orderOptionRepository,
SeasonRepository $seasonRepository,
SimulatorLinkRepository $simulatorLinkRepository,
WorkingTimeRepository $workingTimeRepository
) {
$this->productRepository = $productRepository;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
$this->productCategoryRepository = $productCategoryRepository;
$this->stepOptionRepository = $stepOptionRepository;
$this->priceRepository = $priceRepository;
$this->orderHelper = $orderHelper;
$this->sendDataMethodCategoryRepository = $sendDataMethodCategoryRepository;
$this->orderOptionRepository = $orderOptionRepository;
$this->seasonRepository = $seasonRepository;
$this->simulatorLinkRepository = $simulatorLinkRepository;
$this->workingTimeRepository = $workingTimeRepository;
}
/**
* @Method("GET")
* @Route("/product/{id}", name="detail_product")
* @Template("@user_data/keyholder-size_s.twig")
*/
public function keyHolderSizeS($id)
{
$Product = $this->productRepository->findWithSortedClassCategories($id);
if (!$this->checkVisibility($Product) || $Product->getIsSample()) {
throw new NotFoundHttpException();
}
$is_favorite = false;
if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
$is_favorite = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product);
}
$Category = $Product->getProductCategories()[0];
$categoryName = $Category ? $Category->getCategory()->getName() : '';
$Steps = $this->stepOptionRepository->getStepOptionProduct($id);
$option_quantity = $this->priceRepository->getQuantityOptions($id);
$workingTime = $this->workingTimeRepository->getTopWorkingTime();
return [
'subtitle' => $Product->getName(),
'category_name' => $categoryName,
'category_id' => $Category ? $Category->getCategory()->getId() : '',
'Product' => $Product,
'Steps' => $Steps,
'option_quantity' => $option_quantity,
'is_favorite' => $is_favorite,
'WorkingTime' => $workingTime
];
}
protected function checkVisibility(Product $Product)
{
$is_admin = $this->session->has('_security_admin');
// 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
if (!$is_admin) {
// 在庫なし商品の非表示オプションが有効な場合.
// if ($this->BaseInfo->isOptionNostockHidden()) {
// if (!$Product->getStockFind()) {
// return false;
// }
// }
// 公開ステータスでない商品は表示しない.
if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
return false;
}
}
return true;
}
/**
* @Method("GET")
* @Route("/product/{productID}/order/{optionCode}", name="order_option_page")
* @Template("@user_data/product_order.twig")
*/
public function badgeCanHookOrder($productID, $optionCode)
{
$options = $this->handleOptionCode($optionCode, $productID);
$Product = $this->productRepository->findWithSortedClassCategories($productID);
if (!$this->checkVisibility($Product)) {
throw new NotFoundHttpException();
}
$Category = $Product->getProductCategories()[0];
if (!$Category) {
throw new NotFoundHttpException();
}
$orderOptions = $this->orderOptionRepository->getOrderOptionCategory($Category->getCateGoryId(), $productID);
$sendDataMethods = $this->sendDataMethodCategoryRepository->getSendDataMethodsProduct($Category->getCateGoryId(), $productID);
$remove_send_data_method_ids = [];
$remove_order_option_ids = [];
foreach($options['options'] as $item){
if($item['remove_send_data_method_id'] && !in_array($item['remove_send_data_method_id'], $remove_send_data_method_ids)){
$remove_send_data_method_ids[] = $item['remove_send_data_method_id'];
}
if($item['remove_order_option_ids']){
$arr_remove_order_ids = explode(',', $item['remove_order_option_ids']);
$remove_order_option_ids = [...$arr_remove_order_ids];
}
}
$list_send_data_methods = [];
foreach([...$sendDataMethods] as $item){
if(!in_array($item['id'], $remove_send_data_method_ids)){
$list_send_data_methods[] = $item;
}
}
$workingTime = $this->workingTimeRepository->getTopWorkingTime();
$filter_orderOptions = array_map(function ($step) use ($remove_order_option_ids) {
$step['options'] = array_values(array_filter($step['options'], function ($option) use ($remove_order_option_ids) {
return !in_array($option['id'], $remove_order_option_ids);
}));
return $step;
}, $orderOptions);
return [
'OrderOptions' => $filter_orderOptions,
'SendDataMethods' => [...$list_send_data_methods],
'CategoryName' => $Category->getCategory()->getName(),
'CategoryID' => $Category->getCategory()->getId(),
"Product" => $Product,
"Options" => $options,
'WorkingTime' => $workingTime,
'optionCode' => $optionCode
];
}
private function handleOptionCode($optionCode, $productID)
{
$options = base64_decode($optionCode);
$options = json_decode($options, true);
if (!$options || !isset($options['product_id']) || !isset($options['price_id']) || !isset($options['options'])) {
throw new NotFoundHttpException();
}
if($options['product_id'] !== $productID){
throw new NotFoundHttpException();
}
$price = $this->priceRepository->queryOrder($options['price_id']);
// $option_list = $this->stepOptionRepository->whereIn($price->getOptionIdArray());
$option_list = $this->stepOptionRepository->whereIn($options['options']);
if($price->getProductId() != $productID){
throw new NotFoundHttpException();
}
$simulator_link = $this->simulatorLinkRepository->getProductSimulatorLink($productID, $price->getOptionIds());
$delivery_time = null;
$season = $this->seasonRepository->findActiveSeason()->getCode() ?? SeasonCode::Normal;
if ($season == SeasonCode::Normal) {
$delivery_time = $price->getDeliveryTimeNormalSession();
}
if ($season == SeasonCode::Peak) {
$delivery_time = $price->getDeliveryTimePeakSession();
}
if ($season == SeasonCode::Low) {
$delivery_time = $price->getDeliveryTimeLowSession();
}
$plans = [];
$plan_selected = $price->getPlan();
$price_list = $this->priceRepository->queryOrder($options['price_id'], true);
foreach($price_list as $price_plan){
if($price_plan['plan'] != $plan_selected){
$delivery_time_plan = 0;
if ($season == SeasonCode::Normal) {
$delivery_time_plan = $price_plan['delivery_time_normal_session'];
}
if ($season == SeasonCode::Peak) {
$delivery_time_plan = $price_plan['delivery_time_peak_session'];
}
if ($season == SeasonCode::Low) {
$delivery_time_plan = $price_plan['delivery_time_low_session'];
}
if($delivery_time_plan != 0){
$plans[] = [
'plan' => $price_plan['plan'],
'delivery_time' => $delivery_time_plan
];
}
}
}
return [
'quantity_from' => $price->getFromQuantity(),
'quantity_to' => $price->getToQuantity(),
'quantity_view' => $price->getQuantityView(),
'plan' => $plan_selected,
'plans' => $plans,
'price' => $price->getPrice(),
'delivery_time' => $delivery_time,
'options' => $option_list,
'price_list' => $price_list,
'simulator_link' => $simulator_link
];
}
/**
* お気に入り追加.
*
* @Route("/product/add_favorite/{id}", name="add_product_favorite", requirements={"id" = "\d+"}, methods={"GET", "POST"})
*/
public function addFavorite(Request $request, Product $Product)
{
$this->checkVisibility($Product);
$event = new EventArgs(
[
'Product' => $Product,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE);
if ($this->isGranted('ROLE_USER')) {
$Customer = $this->getUser();
$this->customerFavoriteProductRepository->addFavorite($Customer, $Product);
$this->session->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
$event = new EventArgs(
[
'Product' => $Product,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
return $this->redirectToRoute('detail_product', ['id' => $Product->getId()]);
} else {
// 非会員の場合、ログイン画面を表示
// ログイン後の画面遷移先を設定
$this->setLoginTargetPath($this->generateUrl('detail_product', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
$this->session->getFlashBag()->set('eccube.add.favorite', true);
$event = new EventArgs(
[
'Product' => $Product,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
return $this->redirectToRoute('mypage-login');
}
}
}