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

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