app/Customize/Controller/Mypage/MypageController.php line 115

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