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

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\Product;
  17. use Eccube\Entity\Order;
  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\Paginator;
  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")
  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(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE$event);
  114.         $form $builder->getForm();
  115.         return [
  116.             'error' => $utils->getLastAuthenticationError(),
  117.             'form' => $form->createView(),
  118.         ];
  119.     }
  120.     /**
  121.      * マイページ.
  122.      *
  123.      * @Route("/mypage/", name="mypage")
  124.      * @Template("Mypage/index.twig")
  125.      */
  126.     public function index(Request $requestPaginator $paginator)
  127.     {
  128.         // return $this->redirectToRoute('mypage_change');
  129.         $Customer $this->getUser();
  130.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  131.         $this->entityManager
  132.             ->getFilters()
  133.             ->enable('incomplete_order_status_hidden');
  134.         // paginator
  135.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  136.         $event = new EventArgs(
  137.             [
  138.                 'qb' => $qb,
  139.                 'Customer' => $Customer,
  140.             ],
  141.             $request
  142.         );
  143.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH$event);
  144.         $pagination $paginator->paginate(
  145.             $qb,
  146.             $request->get('pageno'1),
  147.             $this->eccubeConfig['eccube_search_pmax']
  148.         );
  149.         return [
  150.             'pagination' => $pagination,
  151.         ];
  152.     }
  153.     /**
  154.      * 購入履歴詳細を表示する.
  155.      *
  156.      * @Route("/mypage/history/{order_no}", name="mypage_history")
  157.      * @Template("Mypage/history.twig")
  158.      */
  159.     public function history(Request $request$order_no)
  160.     {
  161.         // return $this->redirectToRoute('mypage_change');
  162.         
  163.         $this->entityManager->getFilters()
  164.             ->enable('incomplete_order_status_hidden');
  165.         $Order $this->orderRepository->findOneBy(
  166.             [
  167.                 'order_no' => $order_no,
  168.                 'Customer' => $this->getUser(),
  169.             ]
  170.         );
  171.         $event = new EventArgs(
  172.             [
  173.                 'Order' => $Order,
  174.             ],
  175.             $request
  176.         );
  177.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE$event);
  178.         /** @var Order $Order */
  179.         $Order $event->getArgument('Order');
  180.         if (!$Order) {
  181.             throw new NotFoundHttpException();
  182.         }
  183.         $stockOrder true;
  184.         foreach ($Order->getOrderItems() as $orderItem) {
  185.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  186.                 $stockOrder false;
  187.                 break;
  188.             }
  189.         }
  190.         return [
  191.             'Order' => $Order,
  192.             'stockOrder' => $stockOrder,
  193.         ];
  194.     }
  195.     /**
  196.      * 再購入を行う.
  197.      *
  198.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  199.      */
  200.     public function order(Request $request$order_no)
  201.     {
  202.         $this->isTokenValid();
  203.         log_info('再注文開始', [$order_no]);
  204.         $Customer $this->getUser();
  205.         /* @var $Order \Eccube\Entity\Order */
  206.         $Order $this->orderRepository->findOneBy(
  207.             [
  208.                 'order_no' => $order_no,
  209.                 'Customer' => $Customer,
  210.             ]
  211.         );
  212.         $event = new EventArgs(
  213.             [
  214.                 'Order' => $Order,
  215.                 'Customer' => $Customer,
  216.             ],
  217.             $request
  218.         );
  219.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE$event);
  220.         if (!$Order) {
  221.             log_info('対象の注文が見つかりません', [$order_no]);
  222.             throw new NotFoundHttpException();
  223.         }
  224.         // エラーメッセージの配列
  225.         $errorMessages = [];
  226.         foreach ($Order->getOrderItems() as $OrderItem) {
  227.             try {
  228.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  229.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  230.                     // 明細の正規化
  231.                     $Carts $this->cartService->getCarts();
  232.                     foreach ($Carts as $Cart) {
  233.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  234.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  235.                         if ($result->hasError()) {
  236.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  237.                             foreach ($result->getErrors() as $error) {
  238.                                 $errorMessages[] = $error->getMessage();
  239.                             }
  240.                         }
  241.                         foreach ($result->getWarning() as $warning) {
  242.                             $errorMessages[] = $warning->getMessage();
  243.                         }
  244.                     }
  245.                     $this->cartService->save();
  246.                 }
  247.             } catch (CartException $e) {
  248.                 log_info($e->getMessage(), [$order_no]);
  249.                 $this->addRequestError($e->getMessage());
  250.             }
  251.         }
  252.         foreach ($errorMessages as $errorMessage) {
  253.             $this->addRequestError($errorMessage);
  254.         }
  255.         $event = new EventArgs(
  256.             [
  257.                 'Order' => $Order,
  258.                 'Customer' => $Customer,
  259.             ],
  260.             $request
  261.         );
  262.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE$event);
  263.         if ($event->getResponse() !== null) {
  264.             return $event->getResponse();
  265.         }
  266.         log_info('再注文完了', [$order_no]);
  267.         return $this->redirect($this->generateUrl('cart'));
  268.     }
  269.     /**
  270.      * お気に入り商品を表示する.
  271.      *
  272.      * @Route("/mypage/favorite", name="mypage_favorite")
  273.      * @Template("Mypage/favorite.twig")
  274.      */
  275.     public function favorite(Request $requestPaginator $paginator)
  276.     {
  277.         return $this->redirectToRoute('mypage_change');
  278.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  279.             throw new NotFoundHttpException();
  280.         }
  281.         $Customer $this->getUser();
  282.         // paginator
  283.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  284.         $event = new EventArgs(
  285.             [
  286.                 'qb' => $qb,
  287.                 'Customer' => $Customer,
  288.             ],
  289.             $request
  290.         );
  291.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH$event);
  292.         $pagination $paginator->paginate(
  293.             $qb,
  294.             $request->get('pageno'1),
  295.             $this->eccubeConfig['eccube_search_pmax'],
  296.             ['wrap-queries' => true]
  297.         );
  298.         return [
  299.             'pagination' => $pagination,
  300.         ];
  301.     }
  302.     /**
  303.      * お気に入り商品を削除する.
  304.      *
  305.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  306.      */
  307.     public function delete(Request $requestProduct $Product)
  308.     {
  309.         $this->isTokenValid();
  310.         $Customer $this->getUser();
  311.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  312.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  313.         if ($CustomerFavoriteProduct) {
  314.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  315.         } else {
  316.             throw new BadRequestHttpException();
  317.         }
  318.         $event = new EventArgs(
  319.             [
  320.                 'Customer' => $Customer,
  321.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  322.             ], $request
  323.         );
  324.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE$event);
  325.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  326.         return $this->redirect($this->generateUrl('mypage_favorite'));
  327.     }
  328. }