src/Eccube/Controller/Mypage/MypageController.php line 136

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller\Mypage;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\Customer;
  16. use Eccube\Entity\Order;
  17. use Eccube\Entity\Product;
  18. use Eccube\Event\EccubeEvents;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Exception\CartException;
  21. use Eccube\Form\Type\Front\CustomerLoginType;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\OrderRepository;
  25. use Eccube\Repository\ProductRepository;
  26. use Eccube\Service\CartService;
  27. use Eccube\Service\PurchaseFlow\PurchaseContext;
  28. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  29. use Knp\Component\Pager\PaginatorInterface;
  30. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  33. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  36. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  37. class MypageController extends AbstractController
  38. {
  39.     /**
  40.      * @var ProductRepository
  41.      */
  42.     protected $productRepository;
  43.     /**
  44.      * @var CustomerFavoriteProductRepository
  45.      */
  46.     protected $customerFavoriteProductRepository;
  47.     /**
  48.      * @var BaseInfo
  49.      */
  50.     protected $BaseInfo;
  51.     /**
  52.      * @var CartService
  53.      */
  54.     protected $cartService;
  55.     /**
  56.      * @var OrderRepository
  57.      */
  58.     protected $orderRepository;
  59.     /**
  60.      * @var PurchaseFlow
  61.      */
  62.     protected $purchaseFlow;
  63.     protected $params;
  64.     /**
  65.      * MypageController constructor.
  66.      *
  67.      * @param OrderRepository $orderRepository
  68.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  69.      * @param CartService $cartService
  70.      * @param BaseInfoRepository $baseInfoRepository
  71.      * @param PurchaseFlow $purchaseFlow
  72.      */
  73.     public function __construct(
  74.         OrderRepository $orderRepository,
  75.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  76.         CartService $cartService,
  77.         BaseInfoRepository $baseInfoRepository,
  78.         PurchaseFlow $purchaseFlow,
  79.         ParameterBagInterface $params
  80.     ) {
  81.         $this->orderRepository $orderRepository;
  82.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  83.         $this->BaseInfo $baseInfoRepository->get();
  84.         $this->cartService $cartService;
  85.         $this->purchaseFlow $purchaseFlow;
  86.         $this->order_prefix $params->get('order_number_prefix');
  87.     }
  88.     /**
  89.      * ログイン画面.
  90.      *
  91.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  92.      * @Template("Mypage/login.twig")
  93.      */
  94.     public function login(Request $requestAuthenticationUtils $utils)
  95.     {
  96.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  97.             log_info('認証済のためログイン処理をスキップ');
  98.             return $this->redirectToRoute('mypage');
  99.         }
  100.         /* @var $form \Symfony\Component\Form\FormInterface */
  101.         $builder $this->formFactory
  102.             ->createNamedBuilder(''CustomerLoginType::class);
  103.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  104.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  105.             $Customer $this->getUser();
  106.             if ($Customer instanceof Customer) {
  107.                 $builder->get('login_email')
  108.                     ->setData($Customer->getEmail());
  109.             }
  110.         }
  111.         $event = new EventArgs(
  112.             [
  113.                 'builder' => $builder,
  114.             ],
  115.             $request
  116.         );
  117.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  118.         $form $builder->getForm();
  119.         $line_linkage 0;
  120.         $session $request->getSession();
  121.         $targetPath $session->get('_security.customer.target_path');
  122.         if (preg_match("/line_linkage/",$targetPath)) {
  123.             $line_linkage 1;
  124.         }
  125.         return [
  126.             'error' => $utils->getLastAuthenticationError(),
  127.             'form' => $form->createView(),
  128.             'line_linkage' => $line_linkage,
  129.         ];
  130.     }
  131.     /**
  132.      * マイページ.
  133.      *
  134.      * @Route("/mypage/", name="mypage", methods={"GET"})
  135.      * @Template("Mypage/index.twig")
  136.      */
  137.     public function index(Request $requestPaginatorInterface $paginator)
  138.     {
  139.         $Customer $this->getUser();
  140.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  141.         $this->entityManager
  142.             ->getFilters()
  143.             ->enable('incomplete_order_status_hidden');
  144.         // paginator
  145.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  146.         $event = new EventArgs(
  147.             [
  148.                 'qb' => $qb,
  149.                 'Customer' => $Customer,
  150.             ],
  151.             $request
  152.         );
  153.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  154.         $pagination $paginator->paginate(
  155.             $qb,
  156.             $request->get('pageno'1),
  157.             $this->eccubeConfig['eccube_search_pmax']
  158.         );
  159.         return [
  160.             'pagination' => $pagination,
  161.             'order_prefix' => $this->order_prefix
  162.         ];
  163.     }
  164.     /**
  165.      * 購入履歴詳細を表示する.
  166.      *
  167.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  168.      * @Template("Mypage/history.twig")
  169.      */
  170.     public function history(Request $request$order_no)
  171.     {
  172.         $this->entityManager->getFilters()
  173.             ->enable('incomplete_order_status_hidden');
  174.         $Order $this->orderRepository->findOneBy(
  175.             [
  176.                 'order_no' => $order_no,
  177.                 'Customer' => $this->getUser(),
  178.             ]
  179.         );
  180.         $event = new EventArgs(
  181.             [
  182.                 'Order' => $Order,
  183.             ],
  184.             $request
  185.         );
  186.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  187.         /** @var Order $Order */
  188.         $Order $event->getArgument('Order');
  189.         if (!$Order) {
  190.             throw new NotFoundHttpException();
  191.         }
  192.         $stockOrder true;
  193.         foreach ($Order->getOrderItems() as $orderItem) {
  194.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  195.                 $stockOrder false;
  196.                 break;
  197.             }
  198.         }
  199.         return [
  200.             'Order' => $Order,
  201.             'stockOrder' => $stockOrder,
  202.             'order_prefix' => $this->order_prefix
  203.         ];
  204.     }
  205.     /**
  206.      * 再購入を行う.
  207.      *
  208.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  209.      */
  210.     public function order(Request $request$order_no)
  211.     {
  212.         $this->isTokenValid();
  213.         log_info('再注文開始', [$order_no]);
  214.         $Customer $this->getUser();
  215.         /* @var $Order \Eccube\Entity\Order */
  216.         $Order $this->orderRepository->findOneBy(
  217.             [
  218.                 'order_no' => $order_no,
  219.                 'Customer' => $Customer,
  220.             ]
  221.         );
  222.         $event = new EventArgs(
  223.             [
  224.                 'Order' => $Order,
  225.                 'Customer' => $Customer,
  226.             ],
  227.             $request
  228.         );
  229.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  230.         if (!$Order) {
  231.             log_info('対象の注文が見つかりません', [$order_no]);
  232.             throw new NotFoundHttpException();
  233.         }
  234.         // エラーメッセージの配列
  235.         $errorMessages = [];
  236.         foreach ($Order->getOrderItems() as $OrderItem) {
  237.             try {
  238.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  239.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  240.                     // 明細の正規化
  241.                     $Carts $this->cartService->getCarts();
  242.                     foreach ($Carts as $Cart) {
  243.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  244.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  245.                         if ($result->hasError()) {
  246.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  247.                             foreach ($result->getErrors() as $error) {
  248.                                 $errorMessages[] = $error->getMessage();
  249.                             }
  250.                         }
  251.                         foreach ($result->getWarning() as $warning) {
  252.                             $errorMessages[] = $warning->getMessage();
  253.                         }
  254.                     }
  255.                     $this->cartService->save();
  256.                 }
  257.             } catch (CartException $e) {
  258.                 log_info($e->getMessage(), [$order_no]);
  259.                 $this->addRequestError($e->getMessage());
  260.             }
  261.         }
  262.         foreach ($errorMessages as $errorMessage) {
  263.             $this->addRequestError($errorMessage);
  264.         }
  265.         $event = new EventArgs(
  266.             [
  267.                 'Order' => $Order,
  268.                 'Customer' => $Customer,
  269.             ],
  270.             $request
  271.         );
  272.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  273.         if ($event->getResponse() !== null) {
  274.             return $event->getResponse();
  275.         }
  276.         log_info('再注文完了', [$order_no]);
  277.         return $this->redirect($this->generateUrl('cart'));
  278.     }
  279.     /**
  280.      * お気に入り商品を表示する.
  281.      *
  282.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  283.      * @Template("Mypage/favorite.twig")
  284.      */
  285.     public function favorite(Request $requestPaginatorInterface $paginator)
  286.     {
  287.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  288.             throw new NotFoundHttpException();
  289.         }
  290.         $Customer $this->getUser();
  291.         // paginator
  292.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  293.         $event = new EventArgs(
  294.             [
  295.                 'qb' => $qb,
  296.                 'Customer' => $Customer,
  297.             ],
  298.             $request
  299.         );
  300.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  301.         $pagination $paginator->paginate(
  302.             $qb,
  303.             $request->get('pageno'1),
  304.             $this->eccubeConfig['eccube_search_pmax'],
  305.             ['wrap-queries' => true]
  306.         );
  307.         return [
  308.             'pagination' => $pagination,
  309.         ];
  310.     }
  311.     /**
  312.      * お気に入り商品を削除する.
  313.      *
  314.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  315.      */
  316.     public function delete(Request $requestProduct $Product)
  317.     {
  318.         $this->isTokenValid();
  319.         $Customer $this->getUser();
  320.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  321.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  322.         if ($CustomerFavoriteProduct) {
  323.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  324.         } else {
  325.             throw new BadRequestHttpException();
  326.         }
  327.         $event = new EventArgs(
  328.             [
  329.                 'Customer' => $Customer,
  330.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  331.             ], $request
  332.         );
  333.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  334.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  335.         return $this->redirect($this->generateUrl('mypage_favorite'));
  336.     }
  337. }