1 | <?php |
||
60 | class ProductController |
||
61 | { |
||
62 | /** |
||
63 | * @Inject("eccube.purchase.flow.cart") |
||
64 | * @var PurchaseFlow |
||
65 | */ |
||
66 | protected $purchaseFlow; |
||
67 | |||
68 | /** |
||
69 | * @Inject("session") |
||
70 | * @var Session |
||
71 | */ |
||
72 | protected $session; |
||
73 | |||
74 | /** |
||
75 | * @Inject(CustomerFavoriteProductRepository::class) |
||
76 | * @var CustomerFavoriteProductRepository |
||
77 | */ |
||
78 | protected $customerFavoriteProductRepository; |
||
79 | |||
80 | /** |
||
81 | * @Inject(CartService::class) |
||
82 | * @var CartService |
||
83 | */ |
||
84 | protected $cartService; |
||
85 | |||
86 | /** |
||
87 | * @Inject(ProductRepository::class) |
||
88 | * @var ProductRepository |
||
89 | */ |
||
90 | protected $productRepository; |
||
91 | |||
92 | /** |
||
93 | * @Inject("eccube.event.dispatcher") |
||
94 | * @var EventDispatcher |
||
95 | */ |
||
96 | protected $eventDispatcher; |
||
97 | |||
98 | /** |
||
99 | * @Inject("form.factory") |
||
100 | * @var FormFactory |
||
101 | */ |
||
102 | protected $formFactory; |
||
103 | |||
104 | /** |
||
105 | * @Inject("orm.em") |
||
106 | * @var EntityManager |
||
107 | */ |
||
108 | protected $entityManager; |
||
109 | |||
110 | /** |
||
111 | * @Inject(BaseInfo::class) |
||
112 | * @var BaseInfo |
||
113 | */ |
||
114 | protected $BaseInfo; |
||
115 | |||
116 | private $title; |
||
117 | |||
118 | public function __construct() |
||
122 | |||
123 | /** |
||
124 | * 商品一覧画面. |
||
125 | * |
||
126 | * @Route("/products/list", name="product_list") |
||
127 | * @Template("Product/list.twig") |
||
128 | */ |
||
129 | 3 | public function index(Application $app, Request $request) |
|
299 | |||
300 | /** |
||
301 | * 商品詳細画面. |
||
302 | * |
||
303 | * @Route("/products/detail/{id}", name="product_detail", requirements={"id" = "\d+"}) |
||
304 | * @Template("Product/detail.twig") |
||
305 | */ |
||
306 | 4 | public function detail(Application $app, Request $request, Product $Product) |
|
307 | { |
||
308 | 4 | $is_admin = $request->getSession()->has('_security_admin'); |
|
309 | |||
310 | // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能. |
||
311 | 4 | if (!$is_admin) { |
|
312 | // 在庫なし商品の非表示オプションが有効な場合. |
||
313 | 4 | if ($this->BaseInfo->getNostockHidden()) { |
|
314 | if (!$Product->getStockFind()) { |
||
315 | throw new NotFoundHttpException(); |
||
316 | } |
||
317 | } |
||
318 | // 公開ステータスでない商品は表示しない. |
||
319 | 4 | if ($Product->getStatus()->getId() !== Disp::DISPLAY_SHOW) { |
|
320 | throw new NotFoundHttpException(); |
||
321 | } |
||
322 | } |
||
323 | |||
324 | $builder = $this->formFactory->createNamedBuilder( |
||
325 | '', |
||
326 | AddCartType::class, |
||
327 | null, |
||
328 | array( |
||
329 | 'product' => $Product, |
||
330 | 'id_add_product_id' => false, |
||
331 | ) |
||
332 | ); |
||
333 | |||
334 | $event = new EventArgs( |
||
335 | array( |
||
336 | 'builder' => $builder, |
||
337 | 'Product' => $Product, |
||
338 | ), |
||
339 | $request |
||
340 | ); |
||
341 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE, $event); |
||
342 | |||
343 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
344 | $form = $builder->getForm(); |
||
345 | |||
346 | if ($request->getMethod() === 'POST') { |
||
347 | $form->handleRequest($request); |
||
348 | |||
349 | if ($form->isValid()) { |
||
350 | $addCartData = $form->getData(); |
||
351 | if ($addCartData['mode'] === 'add_favorite') { |
||
352 | if ($app->isGranted('ROLE_USER')) { |
||
353 | $Customer = $app->user(); |
||
354 | $this->customerFavoriteProductRepository->addFavorite($Customer, $Product); |
||
355 | $this->session->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId()); |
||
356 | |||
357 | $event = new EventArgs( |
||
358 | array( |
||
359 | 'form' => $form, |
||
360 | 'Product' => $Product, |
||
361 | ), |
||
362 | $request |
||
363 | ); |
||
364 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_FAVORITE, $event); |
||
365 | |||
366 | if ($event->getResponse() !== null) { |
||
367 | return $event->getResponse(); |
||
368 | } |
||
369 | |||
370 | return $app->redirect($app->url('product_detail', array('id' => $Product->getId()))); |
||
371 | } else { |
||
372 | // 非会員の場合、ログイン画面を表示 |
||
373 | // ログイン後の画面遷移先を設定 |
||
374 | $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId()))); |
||
375 | $this->session->getFlashBag()->set('eccube.add.favorite', true); |
||
376 | |||
377 | return $app->redirect($app->url('mypage_login')); |
||
378 | } |
||
379 | } elseif ($addCartData['mode'] === 'add_cart') { |
||
380 | |||
381 | log_info( |
||
382 | 'カート追加処理開始', |
||
383 | array( |
||
384 | 'product_id' => $Product->getId(), |
||
385 | 'product_class_id' => $addCartData['product_class_id'], |
||
386 | 'quantity' => $addCartData['quantity'], |
||
387 | ) |
||
388 | ); |
||
389 | |||
390 | // カートを取得 |
||
391 | $Cart = $this->cartService->getCart(); |
||
392 | |||
393 | // カートへ追加 |
||
394 | $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']); |
||
395 | |||
396 | // 明細の正規化 |
||
397 | $flow = $this->purchaseFlow; |
||
398 | $result = $flow->calculate($Cart, $app['eccube.purchase.context']()); |
||
399 | |||
400 | // 復旧不可のエラーが発生した場合は追加した明細を削除. |
||
401 | if ($result->hasError()) { |
||
402 | $Cart->removeCartItemByIdentifier(ProductClass::class, $addCartData['product_class_id']); |
||
403 | foreach ($result->getErrors() as $error) { |
||
404 | $app->addRequestError($error->getMessage()); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | foreach ($result->getWarning() as $warning) { |
||
409 | $app->addRequestError($warning->getMessage()); |
||
410 | } |
||
411 | |||
412 | $this->cartService->save(); |
||
413 | |||
414 | log_info( |
||
415 | 'カート追加処理完了', |
||
416 | array( |
||
417 | 'product_id' => $Product->getId(), |
||
418 | 'product_class_id' => $addCartData['product_class_id'], |
||
419 | 'quantity' => $addCartData['quantity'], |
||
420 | ) |
||
421 | ); |
||
422 | |||
423 | $event = new EventArgs( |
||
424 | array( |
||
425 | 'form' => $form, |
||
426 | 'Product' => $Product, |
||
427 | ), |
||
428 | $request |
||
429 | ); |
||
430 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event); |
||
431 | |||
432 | if ($event->getResponse() !== null) { |
||
433 | return $event->getResponse(); |
||
434 | } |
||
435 | |||
436 | return $app->redirect($app->url('cart')); |
||
437 | } |
||
438 | } |
||
439 | } else { |
||
440 | $addFavorite = $this->session->getFlashBag()->get('eccube.add.favorite'); |
||
441 | if (!empty($addFavorite)) { |
||
442 | // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う |
||
443 | if ($app->isGranted('ROLE_USER')) { |
||
444 | $Customer = $app->user(); |
||
445 | $this->customerFavoriteProductRepository->addFavorite($Customer, $Product); |
||
446 | $this->session->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId()); |
||
447 | } |
||
448 | } |
||
449 | } |
||
450 | |||
451 | $is_favorite = false; |
||
452 | if ($app->isGranted('ROLE_USER')) { |
||
453 | $Customer = $app->user(); |
||
454 | $is_favorite = $this->customerFavoriteProductRepository->isFavorite($Customer, $Product); |
||
455 | } |
||
456 | |||
457 | return [ |
||
458 | 'title' => $this->title, |
||
459 | 'subtitle' => $Product->getName(), |
||
460 | 'form' => $form->createView(), |
||
461 | 'Product' => $Product, |
||
462 | 'is_favorite' => $is_favorite, |
||
463 | ]; |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * ページタイトルの設定 |
||
468 | * |
||
469 | * @param null|array $searchData |
||
470 | * @return str |
||
471 | */ |
||
472 | 3 | private function getPageTitle($searchData) |
|
482 | } |
||
483 |