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. foreach($options['options'] as $item){
  150. if($item['remove_send_data_method_id'] && !in_array($item['remove_send_data_method_id'], $remove_send_data_method_ids)){
  151. $remove_send_data_method_ids[] = $item['remove_send_data_method_id'];
  152. }
  153. }
  154. $list_send_data_methods = [];
  155. foreach([...$sendDataMethods] as $item){
  156. if(!in_array($item['id'], $remove_send_data_method_ids)){
  157. $list_send_data_methods[] = $item;
  158. }
  159. }
  160. $workingTime = $this->workingTimeRepository->getTopWorkingTime();
  161. return [
  162. 'OrderOptions' => $orderOptions,
  163. 'SendDataMethods' => [...$list_send_data_methods],
  164. 'CategoryName' => $Category->getCategory()->getName(),
  165. 'CategoryID' => $Category->getCategory()->getId(),
  166. "Product" => $Product,
  167. "Options" => $options,
  168. 'WorkingTime' => $workingTime,
  169. 'optionCode' => $optionCode
  170. ];
  171. }
  172. private function handleOptionCode($optionCode, $productID)
  173. {
  174. $options = base64_decode($optionCode);
  175. $options = json_decode($options, true);
  176. if (!$options || !isset($options['product_id']) || !isset($options['price_id']) || !isset($options['options'])) {
  177. throw new NotFoundHttpException();
  178. }
  179. if($options['product_id'] !== $productID){
  180. throw new NotFoundHttpException();
  181. }
  182. $price = $this->priceRepository->queryOrder($options['price_id']);
  183. // $option_list = $this->stepOptionRepository->whereIn($price->getOptionIdArray());
  184. $option_list = $this->stepOptionRepository->whereIn($options['options']);
  185. if($price->getProductId() != $productID){
  186. throw new NotFoundHttpException();
  187. }
  188. $simulator_link = $this->simulatorLinkRepository->getProductSimulatorLink($productID, $price->getOptionIds());
  189. $delivery_time = null;
  190. $season = $this->seasonRepository->findActiveSeason()->getCode() ?? SeasonCode::Normal;
  191. if ($season == SeasonCode::Normal) {
  192. $delivery_time = $price->getDeliveryTimeNormalSession();
  193. }
  194. if ($season == SeasonCode::Peak) {
  195. $delivery_time = $price->getDeliveryTimePeakSession();
  196. }
  197. if ($season == SeasonCode::Low) {
  198. $delivery_time = $price->getDeliveryTimeLowSession();
  199. }
  200. $plans = [];
  201. $plan_selected = $price->getPlan();
  202. $price_list = $this->priceRepository->queryOrder($options['price_id'], true);
  203. foreach($price_list as $price_plan){
  204. if($price_plan['plan'] != $plan_selected){
  205. $delivery_time_plan = 0;
  206. if ($season == SeasonCode::Normal) {
  207. $delivery_time_plan = $price_plan['delivery_time_normal_session'];
  208. }
  209. if ($season == SeasonCode::Peak) {
  210. $delivery_time_plan = $price_plan['delivery_time_peak_session'];
  211. }
  212. if ($season == SeasonCode::Low) {
  213. $delivery_time_plan = $price_plan['delivery_time_low_session'];
  214. }
  215. if($delivery_time_plan != 0){
  216. $plans[] = [
  217. 'plan' => $price_plan['plan'],
  218. 'delivery_time' => $delivery_time_plan
  219. ];
  220. }
  221. }
  222. }
  223. return [
  224. 'quantity_from' => $price->getFromQuantity(),
  225. 'quantity_to' => $price->getToQuantity(),
  226. 'quantity_view' => $price->getQuantityView(),
  227. 'plan' => $plan_selected,
  228. 'plans' => $plans,
  229. 'price' => $price->getPrice(),
  230. 'delivery_time' => $delivery_time,
  231. 'options' => $option_list,
  232. 'price_list' => $price_list,
  233. 'simulator_link' => $simulator_link
  234. ];
  235. }
  236. /**
  237. * お気に入り追加.
  238. *
  239. * @Route("/product/add_favorite/{id}", name="add_product_favorite", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  240. */
  241. public function addFavorite(Request $request, Product $Product)
  242. {
  243. $this->checkVisibility($Product);
  244. $event = new EventArgs(
  245. [
  246. 'Product' => $Product,
  247. ],
  248. $request
  249. );
  250. $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE);
  251. if ($this->isGranted('ROLE_USER')) {
  252. $Customer = $this->getUser();
  253. $this->customerFavoriteProductRepository->addFavorite($Customer, $Product);
  254. $this->session->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
  255. $event = new EventArgs(
  256. [
  257. 'Product' => $Product,
  258. ],
  259. $request
  260. );
  261. $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  262. return $this->redirectToRoute('detail_product', ['id' => $Product->getId()]);
  263. } else {
  264. // 非会員の場合、ログイン画面を表示
  265. // ログイン後の画面遷移先を設定
  266. $this->setLoginTargetPath($this->generateUrl('detail_product', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  267. $this->session->getFlashBag()->set('eccube.add.favorite', true);
  268. $event = new EventArgs(
  269. [
  270. 'Product' => $Product,
  271. ],
  272. $request
  273. );
  274. $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE);
  275. return $this->redirectToRoute('mypage-login');
  276. }
  277. }
  278. }