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