app/Customize/Controller/ProductHomeController.php line 134

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Customize\Enum\SeasonCode;
  4. use Customize\Repository\OrderOptionRepository;
  5. use Customize\Repository\PriceRepository;
  6. use Customize\Repository\SeasonRepository;
  7. use Customize\Repository\SendDataMethodCategoryRepository;
  8. use Customize\Repository\SimulatorLinkRepository;
  9. use Customize\Repository\StepOptionRepository;
  10. use Customize\Repository\WorkingTimeRepository;
  11. use DateTime;
  12. use Eccube\Repository\NewsRepository;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Eccube\Repository\ProductRepository;
  18. use Eccube\Controller\AbstractController;
  19. use Eccube\Entity\Product;
  20. use Eccube\Entity\Master\ProductStatus;
  21. use Eccube\Entity\ProductCategory;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\ProductCategoryRepository;
  25. use Eccube\Service\OrderHelper;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Eccube\Event\EccubeEvents;
  28. use Eccube\Event\EventArgs;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. class ProductHomeController extends AbstractController
  31. {
  32.     /**
  33.      * @var ProductRepository
  34.      */
  35.     protected $productRepository;
  36.     /**
  37.      * @var ProductCategoryRepository
  38.      */
  39.     protected $productCategoryRepository;
  40.     /**
  41.      * @var CustomerFavoriteProductRepository
  42.      */
  43.     protected $customerFavoriteProductRepository;
  44.     /**
  45.      * @var StepOptionRepository
  46.      */
  47.     protected $stepOptionRepository;
  48.     protected $priceRepository;
  49.     protected $orderHelper;
  50.     protected $sendDataMethodCategoryRepository;
  51.     protected $orderOptionRepository;
  52.     protected $seasonRepository;
  53.     protected $simulatorLinkRepository;
  54.     protected $workingTimeRepository;
  55.     public function __construct(
  56.         ProductRepository $productRepository,
  57.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  58.         ProductCategoryRepository $productCategoryRepository,
  59.         StepOptionRepository $stepOptionRepository,
  60.         PriceRepository $priceRepository,
  61.         OrderHelper $orderHelper,
  62.         SendDataMethodCategoryRepository $sendDataMethodCategoryRepository,
  63.         OrderOptionRepository $orderOptionRepository,
  64.         SeasonRepository $seasonRepository,
  65.         SimulatorLinkRepository $simulatorLinkRepository,
  66.         WorkingTimeRepository $workingTimeRepository
  67.     ) {
  68.         $this->productRepository $productRepository;
  69.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  70.         $this->productCategoryRepository $productCategoryRepository;
  71.         $this->stepOptionRepository $stepOptionRepository;
  72.         $this->priceRepository $priceRepository;
  73.         $this->orderHelper $orderHelper;
  74.         $this->sendDataMethodCategoryRepository $sendDataMethodCategoryRepository;
  75.         $this->orderOptionRepository $orderOptionRepository;
  76.         $this->seasonRepository $seasonRepository;
  77.         $this->simulatorLinkRepository $simulatorLinkRepository;
  78.         $this->workingTimeRepository $workingTimeRepository;
  79.     }
  80.     /**
  81.      * @Method("GET")
  82.      * @Route("/product/{id}", name="detail_product")
  83.      * @Template("@user_data/keyholder-size_s.twig")
  84.      */
  85.     public function keyHolderSizeS($id)
  86.     {
  87.         $Product $this->productRepository->findWithSortedClassCategories($id);
  88.         if (!$this->checkVisibility($Product) || $Product->getIsSample()) {
  89.             throw new NotFoundHttpException();
  90.         }
  91.         $is_favorite false;
  92.         if ($this->isGranted('ROLE_USER')) {
  93.             $Customer $this->getUser();
  94.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  95.         }
  96.         $Category $Product->getProductCategories()[0];
  97.         $categoryName $Category $Category->getCategory()->getName() : '';
  98.         $Steps $this->stepOptionRepository->getStepOptionProduct($id);
  99.         $option_quantity $this->priceRepository->getQuantityOptions($id);
  100.         $workingTime $this->workingTimeRepository->getTopWorkingTime();
  101.         return [
  102.             'subtitle' => $Product->getName(),
  103.             'category_name' => $categoryName,
  104.             'category_id' => $Category ?  $Category->getCategory()->getId() : '',
  105.             'Product' => $Product,
  106.             'Steps' => $Steps,
  107.             'option_quantity' => $option_quantity,
  108.             'is_favorite' => $is_favorite,
  109.             'WorkingTime' => $workingTime
  110.         ];
  111.     }
  112.     protected function checkVisibility(Product $Product)
  113.     {
  114.         $is_admin $this->session->has('_security_admin');
  115.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  116.         if (!$is_admin) {
  117.             // 在庫なし商品の非表示オプションが有効な場合.
  118.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  119.             //     if (!$Product->getStockFind()) {
  120.             //         return false;
  121.             //     }
  122.             // }
  123.             // 公開ステータスでない商品は表示しない.
  124.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  125.                 return false;
  126.             }
  127.         }
  128.         return true;
  129.     }
  130.     /**
  131.      * @Method("GET")
  132.      * @Route("/product/{productID}/order/{optionCode}", name="order_option_page")
  133.      * @Template("@user_data/product_order.twig")
  134.      */
  135.     public function badgeCanHookOrder($productID$optionCode)
  136.     {
  137.         $options $this->handleOptionCode($optionCode$productID);
  138.         $Product $this->productRepository->findWithSortedClassCategories($productID);
  139.         if (!$this->checkVisibility($Product)) {
  140.             throw new NotFoundHttpException();
  141.         }
  142.         $Category $Product->getProductCategories()[0];
  143.         if (!$Category) {
  144.             throw new NotFoundHttpException();
  145.         }
  146.         $orderOptions $this->orderOptionRepository->getOrderOptionCategory($Category->getCateGoryId(), $productID);
  147.         $sendDataMethods $this->sendDataMethodCategoryRepository->getSendDataMethodsProduct($Category->getCateGoryId(), $productID);
  148.         $remove_send_data_method_ids = [];
  149.         $remove_order_option_ids = [];
  150.         foreach($options['options'] as $item){
  151.             if($item['remove_send_data_method_id'] && !in_array($item['remove_send_data_method_id'], $remove_send_data_method_ids)){
  152.                 $remove_send_data_method_ids[] = $item['remove_send_data_method_id'];
  153.             }
  154.             if($item['remove_order_option_ids']){
  155.                 $arr_remove_order_ids =  explode(','$item['remove_order_option_ids']);
  156.                 $remove_order_option_ids = [...$arr_remove_order_ids];
  157.             }
  158.         }
  159.         $list_send_data_methods = [];
  160.         foreach([...$sendDataMethods] as $item){
  161.             if(!in_array($item['id'], $remove_send_data_method_ids)){
  162.                 $list_send_data_methods[] = $item;
  163.             }
  164.         }
  165.         $workingTime $this->workingTimeRepository->getTopWorkingTime();
  166.         $filter_orderOptions array_map(function ($step) use ($remove_order_option_ids) {
  167.             $step['options'] = array_values(array_filter($step['options'], function ($option) use ($remove_order_option_ids) {
  168.                 return !in_array($option['id'], $remove_order_option_ids);
  169.             }));
  170.             return $step;
  171.         }, $orderOptions);
  172.         return [
  173.             'OrderOptions' => $filter_orderOptions,
  174.             'SendDataMethods' => [...$list_send_data_methods],
  175.             'CategoryName' => $Category->getCategory()->getName(),
  176.             'CategoryID' => $Category->getCategory()->getId(),
  177.             "Product" => $Product,
  178.             "Options" => $options,
  179.             'WorkingTime' => $workingTime,
  180.             'optionCode' => $optionCode
  181.         ];
  182.     }
  183.     private function handleOptionCode($optionCode$productID)
  184.     {
  185.         $options base64_decode($optionCode);
  186.         $options json_decode($optionstrue);
  187.         if (!$options || !isset($options['product_id']) || !isset($options['price_id']) || !isset($options['options'])) {
  188.             throw new NotFoundHttpException();
  189.         }
  190.         if($options['product_id'] !== $productID){
  191.             throw new NotFoundHttpException();
  192.         }
  193.         $price $this->priceRepository->queryOrder($options['price_id']);
  194.         // $option_list = $this->stepOptionRepository->whereIn($price->getOptionIdArray());
  195.         $option_list $this->stepOptionRepository->whereIn($options['options']);
  196.         if($price->getProductId() != $productID){
  197.             throw new NotFoundHttpException();
  198.         }
  199.         $simulator_link $this->simulatorLinkRepository->getProductSimulatorLink($productID$price->getOptionIds());
  200.         $delivery_time null;
  201.         $season $this->seasonRepository->findActiveSeason()->getCode() ?? SeasonCode::Normal;
  202.         if ($season == SeasonCode::Normal) {
  203.             $delivery_time $price->getDeliveryTimeNormalSession();
  204.         }
  205.         if ($season == SeasonCode::Peak) {
  206.             $delivery_time $price->getDeliveryTimePeakSession();
  207.         }
  208.         if ($season == SeasonCode::Low) {
  209.             $delivery_time $price->getDeliveryTimeLowSession();
  210.         }
  211.       
  212.         $plans = [];
  213.         $plan_selected $price->getPlan();
  214.         $price_list $this->priceRepository->queryOrder($options['price_id'], true);
  215.         foreach($price_list as $price_plan){
  216.             if($price_plan['plan'] != $plan_selected){
  217.                 $delivery_time_plan 0;
  218.                 if ($season == SeasonCode::Normal) {
  219.                     $delivery_time_plan $price_plan['delivery_time_normal_session'];
  220.                 }
  221.                 if ($season == SeasonCode::Peak) {
  222.                     $delivery_time_plan $price_plan['delivery_time_peak_session'];
  223.                 }
  224.                 if ($season == SeasonCode::Low) {
  225.                     $delivery_time_plan $price_plan['delivery_time_low_session'];
  226.                 }
  227.                 if($delivery_time_plan != 0){
  228.                     $plans[] = [
  229.                         'plan' => $price_plan['plan'],
  230.                         'delivery_time' => $delivery_time_plan
  231.                     ];
  232.                 }
  233.             }
  234.         }
  235.         return [
  236.             'quantity_from' => $price->getFromQuantity(),
  237.             'quantity_to' => $price->getToQuantity(),
  238.             'quantity_view' => $price->getQuantityView(),
  239.             'plan' => $plan_selected,
  240.             'plans' =>  $plans,
  241.             'price' => $price->getPrice(),
  242.             'delivery_time' => $delivery_time,
  243.             'options' => $option_list,
  244.             'price_list' => $price_list,
  245.             'simulator_link' => $simulator_link
  246.         ];
  247.     }
  248.     /**
  249.      * お気に入り追加.
  250.      *
  251.      * @Route("/product/add_favorite/{id}", name="add_product_favorite", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  252.      */
  253.     public function addFavorite(Request $requestProduct $Product)
  254.     {
  255.         $this->checkVisibility($Product);
  256.         $event = new EventArgs(
  257.             [
  258.                 'Product' => $Product,
  259.             ],
  260.             $request
  261.         );
  262.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE);
  263.         if ($this->isGranted('ROLE_USER')) {
  264.             $Customer $this->getUser();
  265.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product);
  266.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  267.             $event = new EventArgs(
  268.                 [
  269.                     'Product' => $Product,
  270.                 ],
  271.                 $request
  272.             );
  273.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  274.             return $this->redirectToRoute('detail_product', ['id' => $Product->getId()]);
  275.         } else {
  276.             // 非会員の場合、ログイン画面を表示
  277.             //  ログイン後の画面遷移先を設定
  278.             $this->setLoginTargetPath($this->generateUrl('detail_product', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  279.             $this->session->getFlashBag()->set('eccube.add.favorite'true);
  280.             $event = new EventArgs(
  281.                 [
  282.                     'Product' => $Product,
  283.                 ],
  284.                 $request
  285.             );
  286.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  287.             return $this->redirectToRoute('mypage-login');
  288.         }
  289.     }
  290. }