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