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

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\Mypage\MypageController as BaseMypageController;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\BaseInfo;
  16. use Eccube\Entity\Customer;
  17. use Eccube\Entity\Order;
  18. use Eccube\Entity\Product;
  19. use Eccube\Event\EccubeEvents;
  20. use Eccube\Event\EventArgs;
  21. use Eccube\Exception\CartException;
  22. use Eccube\Form\Type\Front\CustomerLoginType;
  23. use Eccube\Repository\BaseInfoRepository;
  24. use Eccube\Repository\CustomerFavoriteProductRepository;
  25. use Eccube\Repository\OrderRepository;
  26. use Eccube\Repository\ProductRepository;
  27. use Eccube\Service\CartService;
  28. use Eccube\Service\PurchaseFlow\PurchaseContext;
  29. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  30. use Knp\Component\Pager\PaginatorInterface;
  31. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  34. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  35. use Symfony\Component\Routing\Annotation\Route;
  36. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  37. class MypageController extends BaseMypageController
  38. {
  39.     /**
  40.      * @var ProductRepository
  41.      */
  42.     protected $productRepository;
  43.     /**
  44.      * @var CustomerFavoriteProductRepository
  45.      */
  46.     protected $customerFavoriteProductRepository;
  47.     /**
  48.      * @var BaseInfo
  49.      */
  50.     protected $BaseInfo;
  51.     /**
  52.      * @var CartService
  53.      */
  54.     protected $cartService;
  55.     /**
  56.      * @var OrderRepository
  57.      */
  58.     protected $orderRepository;
  59.     /**
  60.      * @var PurchaseFlow
  61.      */
  62.     protected $purchaseFlow;
  63.     /**
  64.      * MypageController constructor.
  65.      *
  66.      * @param OrderRepository $orderRepository
  67.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  68.      * @param CartService $cartService
  69.      * @param BaseInfoRepository $baseInfoRepository
  70.      * @param PurchaseFlow $purchaseFlow
  71.      */
  72.     public function __construct(
  73.         OrderRepository $orderRepository,
  74.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  75.         CartService $cartService,
  76.         BaseInfoRepository $baseInfoRepository,
  77.         PurchaseFlow $purchaseFlow
  78.     ) {
  79.         $this->orderRepository $orderRepository;
  80.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  81.         $this->BaseInfo $baseInfoRepository->get();
  82.         $this->cartService $cartService;
  83.         $this->purchaseFlow $purchaseFlow;
  84.     }
  85.     /**
  86.      * ログイン画面.
  87.      *
  88.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  89.      * @Template("Mypage/login.twig")
  90.      */
  91.     public function login(Request $requestAuthenticationUtils $utils)
  92.     {
  93.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  94.             log_info('認証済のためログイン処理をスキップ');
  95.             return $this->redirectToRoute('mypage');
  96.         }
  97.         /* @var $form \Symfony\Component\Form\FormInterface */
  98.         $builder $this->formFactory
  99.             ->createNamedBuilder(''CustomerLoginType::class);
  100.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  101.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  102.             $Customer $this->getUser();
  103.             if ($Customer instanceof Customer) {
  104.                 $builder->get('login_email')
  105.                     ->setData($Customer->getEmail());
  106.             }
  107.         }
  108.         $event = new EventArgs(
  109.             [
  110.                 'builder' => $builder,
  111.             ],
  112.             $request
  113.         );
  114.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  115.         $form $builder->getForm();
  116.         return [
  117.             'error' => $utils->getLastAuthenticationError(),
  118.             'form' => $form->createView(),
  119.         ];
  120.     }
  121.     /**
  122.      * マイページ.
  123.      *
  124.      * @Route("/mypage/", name="mypage", methods={"GET"})
  125.      * @Template("Mypage/index.twig")
  126.      */
  127.     public function index(Request $requestPaginatorInterface $paginator)
  128.     {
  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($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  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", methods={"GET"})
  157.      * @Template("Mypage/history.twig")
  158.      */
  159.     public function history(Request $request$order_no)
  160.     {
  161.         $this->entityManager->getFilters()
  162.             ->enable('incomplete_order_status_hidden');
  163.         $Order $this->orderRepository->findOneBy(
  164.             [
  165.                 'order_no' => $order_no,
  166.                 'Customer' => $this->getUser(),
  167.             ]
  168.         );
  169.         $event = new EventArgs(
  170.             [
  171.                 'Order' => $Order,
  172.             ],
  173.             $request
  174.         );
  175.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  176.         /** @var Order $Order */
  177.         $Order $event->getArgument('Order');
  178.         if (!$Order) {
  179.             throw new NotFoundHttpException();
  180.         }
  181.         $stockOrder true;
  182.         foreach ($Order->getOrderItems() as $orderItem) {
  183.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  184.                 $stockOrder false;
  185.                 break;
  186.             }
  187.         }
  188.         return [
  189.             'Order' => $Order,
  190.             'stockOrder' => $stockOrder,
  191.         ];
  192.     }
  193.     /**
  194.      * 再購入を行う.
  195.      *
  196.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  197.      */
  198.     public function order(Request $request$order_no)
  199.     {
  200.         $this->isTokenValid();
  201.         log_info('再注文開始', [$order_no]);
  202.         $Customer $this->getUser();
  203.         /* @var $Order \Eccube\Entity\Order */
  204.         $Order $this->orderRepository->findOneBy(
  205.             [
  206.                 'order_no' => $order_no,
  207.                 'Customer' => $Customer,
  208.             ]
  209.         );
  210.         $event = new EventArgs(
  211.             [
  212.                 'Order' => $Order,
  213.                 'Customer' => $Customer,
  214.             ],
  215.             $request
  216.         );
  217.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  218.         if (!$Order) {
  219.             log_info('対象の注文が見つかりません', [$order_no]);
  220.             throw new NotFoundHttpException();
  221.         }
  222.         // カートをクリア
  223.         $this->cartService->clear();
  224.         // エラーメッセージの配列
  225.         $errorMessages = [];
  226.         foreach ($Order->getOrderItems() as $OrderItem) {
  227.             try {
  228.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  229.                     // SESSION に追加
  230.                     // $_SESSION['cart_ex']['product_custom'] = $OrderItem->getProductName();
  231.                     // $_SESSION['cart_ex']['product_subtotal'] = $OrderItem->getPrice();
  232.                     $parm_data = array('product_custom'=>$OrderItem->getProductName(), 'product_subtotal'=>$OrderItem->getPrice());
  233.                     // カートに追加
  234.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity(), $parm_data);
  235.                     // 明細の正規化
  236.                     $Carts $this->cartService->getCarts();
  237.                     foreach ($Carts as $Cart) {
  238.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  239.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  240.                         if ($result->hasError()) {
  241.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  242.                             foreach ($result->getErrors() as $error) {
  243.                                 $errorMessages[] = $error->getMessage();
  244.                             }
  245.                         }
  246.                         foreach ($result->getWarning() as $warning) {
  247.                             $errorMessages[] = $warning->getMessage();
  248.                         }
  249.                     }
  250.                     $this->cartService->save();
  251.                 }
  252.             } catch (CartException $e) {
  253.                 log_info($e->getMessage(), [$order_no]);
  254.                 $this->addRequestError($e->getMessage());
  255.             }
  256.         }
  257.         foreach ($errorMessages as $errorMessage) {
  258.             $this->addRequestError($errorMessage);
  259.         }
  260.         $event = new EventArgs(
  261.             [
  262.                 'Order' => $Order,
  263.                 'Customer' => $Customer,
  264.             ],
  265.             $request
  266.         );
  267.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  268.         if ($event->getResponse() !== null) {
  269.             return $event->getResponse();
  270.         }
  271.         log_info('再注文完了', [$order_no]);
  272.         return $this->redirect($this->generateUrl('cart'));
  273.     }
  274.     /**
  275.      * お気に入り商品を表示する.
  276.      *
  277.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  278.      * @Template("Mypage/favorite.twig")
  279.      */
  280.     public function favorite(Request $requestPaginatorInterface $paginator)
  281.     {
  282.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  283.             throw new NotFoundHttpException();
  284.         }
  285.         $Customer $this->getUser();
  286.         // paginator
  287.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  288.         $event = new EventArgs(
  289.             [
  290.                 'qb' => $qb,
  291.                 'Customer' => $Customer,
  292.             ],
  293.             $request
  294.         );
  295.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  296.         $pagination $paginator->paginate(
  297.             $qb,
  298.             $request->get('pageno'1),
  299.             $this->eccubeConfig['eccube_search_pmax'],
  300.             ['wrap-queries' => true]
  301.         );
  302.         return [
  303.             'pagination' => $pagination,
  304.         ];
  305.     }
  306.     /**
  307.      * お気に入り商品を削除する.
  308.      *
  309.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  310.      */
  311.     public function delete(Request $requestProduct $Product)
  312.     {
  313.         $this->isTokenValid();
  314.         $Customer $this->getUser();
  315.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  316.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  317.         if ($CustomerFavoriteProduct) {
  318.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  319.         } else {
  320.             throw new BadRequestHttpException();
  321.         }
  322.         $event = new EventArgs(
  323.             [
  324.                 'Customer' => $Customer,
  325.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  326.             ], $request
  327.         );
  328.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  329.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  330.         return $this->redirect($this->generateUrl('mypage_favorite'));
  331.     }
  332. }