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

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