app/Customize/Controller/Mypage/CustomMypageController.php line 142

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 Customize\Service\CustomOrderHelper;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\BaseInfo;
  16. use Eccube\Entity\Customer;
  17. use Eccube\Entity\CustomerFavoriteProduct;
  18. use Eccube\Entity\Order;
  19. use Eccube\Entity\Product;
  20. use Eccube\Event\EccubeEvents;
  21. use Eccube\Event\EventArgs;
  22. use Eccube\Exception\CartException;
  23. use Eccube\Form\Type\Front\CustomerLoginType;
  24. use Eccube\Repository\BaseInfoRepository;
  25. use Eccube\Repository\CustomerFavoriteProductRepository;
  26. use Eccube\Repository\OrderItemRepository;
  27. use Eccube\Repository\OrderRepository;
  28. use Eccube\Repository\ProductRepository;
  29. use Eccube\Service\CartService;
  30. use Eccube\Service\OrderHelper;
  31. use Eccube\Service\PurchaseFlow\PurchaseContext;
  32. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  33. use Knp\Component\Pager\PaginatorInterface;
  34. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\RequestStack;
  37. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  38. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  39. use Symfony\Component\Routing\Annotation\Route;
  40. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  41. class CustomMypageController extends AbstractController
  42. {
  43.     /**
  44.      * @var ProductRepository
  45.      */
  46.     protected $productRepository;
  47.     /**
  48.      * @var CustomerFavoriteProductRepository
  49.      */
  50.     protected $customerFavoriteProductRepository;
  51.     /**
  52.      * @var BaseInfo
  53.      */
  54.     protected $BaseInfo;
  55.     /**
  56.      * @var CartService
  57.      */
  58.     protected $cartService;
  59.     /**
  60.      * @var OrderRepository
  61.      */
  62.     protected $orderRepository;
  63.     /**
  64.      * @var PurchaseFlow
  65.      */
  66.     protected $purchaseFlow;
  67.     /**
  68.      * @var CustomOrderHelper
  69.      */
  70.     protected $orderHelper;
  71.     /**
  72.      * @var OrderItemRepository
  73.      */
  74.     protected $orderItemRepository;
  75.     /**
  76.      * @var RequestStack
  77.      */
  78.     protected $requestStack;
  79.     /**
  80.      * MypageController constructor.
  81.      *
  82.      * @param OrderRepository $orderRepository
  83.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  84.      * @param CartService $cartService
  85.      * @param BaseInfoRepository $baseInfoRepository
  86.      * @param PurchaseFlow $purchaseFlow
  87.      * @param CustomOrderHelper $orderHelper
  88.      * @param OrderItemRepository $orderItemRepository
  89.      * @param RequestStack $requestStack
  90.      */
  91.     public function __construct(
  92.         OrderRepository $orderRepository,
  93.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  94.         CartService $cartService,
  95.         BaseInfoRepository $baseInfoRepository,
  96.         PurchaseFlow $purchaseFlow,
  97.         CustomOrderHelper $orderHelper,
  98.         OrderItemRepository $orderItemRepository,
  99.         RequestStack $requestStack
  100.     ) {
  101.         $this->orderRepository $orderRepository;
  102.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  103.         $this->BaseInfo $baseInfoRepository->get();
  104.         $this->cartService $cartService;
  105.         $this->purchaseFlow $purchaseFlow;
  106.         $this->orderHelper $orderHelper;
  107.         $this->orderItemRepository $orderItemRepository;
  108.         $this->requestStack $requestStack;
  109.     }
  110.     /**
  111.      * ログイン画面.
  112.      *
  113.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  114.      * @Template("Mypage/login.twig")
  115.      */
  116.     public function login(Request $requestAuthenticationUtils $utils)
  117.     {
  118.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  119.             log_info('認証済のためログイン処理をスキップ');
  120.             return $this->redirectToRoute('mypage');
  121.         }
  122.         /* @var $form \Symfony\Component\Form\FormInterface */
  123.         $builder $this->formFactory
  124.             ->createNamedBuilder(''CustomerLoginType::class);
  125.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  126.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  127.             $Customer $this->getUser();
  128.             if ($Customer instanceof Customer) {
  129.                 $builder->get('login_email')
  130.                     ->setData($Customer->getEmail());
  131.             }
  132.         }
  133.         $event = new EventArgs(
  134.             [
  135.                 'builder' => $builder,
  136.             ],
  137.             $request
  138.         );
  139.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  140.         $form $builder->getForm();
  141.         // ログイン前の画面へ戻す
  142.         $error $utils->getLastAuthenticationError();
  143.         if (!$error) {
  144.             // ログインエラーがなければ戻り先をセット
  145.             $referer $request->headers->get('referer');
  146.             if ($referer) {
  147.                 // refererチェック
  148.                 $referers parse_url($referer);
  149.                 if ($referers['host'] == $request->getHost()) {
  150.                     // ホストが同一であればrefererをセット
  151.                     $requestStackUri $this->requestStack->getMasterRequest()->getUri();
  152.                     if ($request->getUri() == $requestStackUri) {
  153.                         // ログイン画面遷移直前のuriをセット
  154.                         $this->setLoginTargetPath($referer);
  155.                     } else {
  156.                         // ログイン必須画面のuriをセット
  157.                         $this->setLoginTargetPath($requestStackUri);
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.         return [
  163.             'error' => $error,
  164.             'form' => $form->createView(),
  165.         ];
  166.     }
  167.     /**
  168.      * マイページ.
  169.      *
  170.      * @Route("/mypage/", name="mypage", methods={"GET"})
  171.      * @Template("Mypage/index.twig")
  172.      */
  173.     public function index(Request $requestPaginatorInterface $paginator)
  174.     {
  175.         $Customer $this->getUser();
  176.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  177.         $this->entityManager
  178.             ->getFilters()
  179.             ->enable('incomplete_order_status_hidden');
  180.         // paginator
  181.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  182.         $event = new EventArgs(
  183.             [
  184.                 'qb' => $qb,
  185.                 'Customer' => $Customer,
  186.             ],
  187.             $request
  188.         );
  189.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  190.         $pagination $paginator->paginate(
  191.             $qb,
  192.             $request->get('pageno'1),
  193.             $this->eccubeConfig['eccube_search_pmax']
  194.         );
  195.         $itemImageArr = [];
  196.         foreach ( $pagination->getItems() as $Order ) {
  197.             foreach ($Order->getItems() as $OrderItem) {
  198.                 if ( !empty($OrderItem['design_image_path']) ) {
  199.                     $itemImageArr[$Order['id']][$OrderItem['id']] = $this->generateUrl('image_load', [
  200.                         'source' => $OrderItem['design_image_path'],
  201.                         'type' => 'design_image',
  202.                         'order_id' => $Order->getId()
  203.                     ]);
  204.                 }
  205.             }
  206.         }
  207.         return [
  208.             'pagination' => $pagination,
  209.             'itemImageArr' => $itemImageArr,
  210.         ];
  211.     }
  212.     /**
  213.      * 購入履歴詳細を表示する.
  214.      *
  215.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  216.      * @Template("Mypage/history.twig")
  217.      */
  218.     public function history(Request $request$order_no)
  219.     {
  220.         $this->entityManager->getFilters()
  221.             ->enable('incomplete_order_status_hidden');
  222.         $Order $this->orderRepository->findOneBy(
  223.             [
  224.                 'order_no' => $order_no,
  225.                 'Customer' => $this->getUser(),
  226.             ]
  227.         );
  228.         $event = new EventArgs(
  229.             [
  230.                 'Order' => $Order,
  231.             ],
  232.             $request
  233.         );
  234.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  235.         /** @var Order $Order */
  236.         $Order $event->getArgument('Order');
  237.         if (!$Order) {
  238.             throw new NotFoundHttpException();
  239.         }
  240.         $stockOrder true;
  241.         foreach ($Order->getOrderItems() as $orderItem) {
  242.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  243.                 $stockOrder false;
  244.                 break;
  245.             }
  246.         }
  247.         return [
  248.             'Order' => $Order,
  249.             'stockOrder' => $stockOrder,
  250.         ];
  251.     }
  252.     /**
  253.      * 再購入を行う.
  254.      *
  255.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  256.      */
  257.     public function order(Request $request$order_no)
  258.     {
  259.         $this->isTokenValid();
  260.         log_info('再注文開始', [$order_no]);
  261.         $Customer $this->getUser();
  262.         /* @var $Order \Eccube\Entity\Order */
  263.         $Order $this->orderRepository->findOneBy(
  264.             [
  265.                 'order_no' => $order_no,
  266.                 'Customer' => $Customer,
  267.             ]
  268.         );
  269.         $event = new EventArgs(
  270.             [
  271.                 'Order' => $Order,
  272.                 'Customer' => $Customer,
  273.             ],
  274.             $request
  275.         );
  276.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  277.         if (!$Order) {
  278.             log_info('対象の注文が見つかりません', [$order_no]);
  279.             throw new NotFoundHttpException();
  280.         }
  281.         // エラーメッセージの配列
  282.         $errorMessages = [];
  283.         foreach ($Order->getOrderItems() as $OrderItem) {
  284.             try {
  285.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  286.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  287.                     // 明細の正規化
  288.                     $Carts $this->cartService->getCarts();
  289.                     foreach ($Carts as $Cart) {
  290.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  291.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  292.                         if ($result->hasError()) {
  293.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  294.                             foreach ($result->getErrors() as $error) {
  295.                                 $errorMessages[] = $error->getMessage();
  296.                             }
  297.                         }
  298.                         foreach ($result->getWarning() as $warning) {
  299.                             $errorMessages[] = $warning->getMessage();
  300.                         }
  301.                     }
  302.                     $this->cartService->save();
  303.                 }
  304.             } catch (CartException $e) {
  305.                 log_info($e->getMessage(), [$order_no]);
  306.                 $this->addRequestError($e->getMessage());
  307.             }
  308.         }
  309.         foreach ($errorMessages as $errorMessage) {
  310.             $this->addRequestError($errorMessage);
  311.         }
  312.         $event = new EventArgs(
  313.             [
  314.                 'Order' => $Order,
  315.                 'Customer' => $Customer,
  316.             ],
  317.             $request
  318.         );
  319.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  320.         if ($event->getResponse() !== null) {
  321.             return $event->getResponse();
  322.         }
  323.         log_info('再注文完了', [$order_no]);
  324.         return $this->redirect($this->generateUrl('cart'));
  325.     }
  326.     /**
  327.      * 再購入を行う.
  328.      *
  329.      * @Route("/mypage/order/{id}", name="mypage_order_item", methods={"GET"})
  330.      */
  331.     public function orderItem(Request $request$id)
  332.     {
  333.         $this->orderHelper->removeSession();
  334.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->find($id);
  335.         if (!$CustomerFavoriteProduct) {
  336.             log_info('対象の注文商品が見つかりません', [$id]);
  337.             throw new NotFoundHttpException();
  338.         }
  339.         if ( empty($CustomerFavoriteProduct->getPreOrderId()) ) {
  340.             $CustomerFavoriteProduct->setPreOrderId($this->orderHelper->createPreOrderId());
  341.             $this->entityManager->persist($CustomerFavoriteProduct);
  342.             $this->entityManager->flush();
  343.         }
  344.         // セッションをセット
  345.         $this->session->set(CustomOrderHelper::SESSION_FAVORITE_ID$id);
  346.         return $this->redirect($this->generateUrl('shopping'));
  347.     }
  348.     /**
  349.      * 注文履歴から再購入を行う.
  350.      *
  351.      * @Route("/mypage/reorder/{order_item_id}", name="mypage_reorder_item", methods={"GET"})
  352.      */
  353.     public function reorderItem(Request $request$order_item_id)
  354.     {
  355.         $this->orderHelper->removeSession();
  356.         $OrderItem $this->orderItemRepository->find($order_item_id);
  357.         if (!$OrderItem) {
  358.             log_info('対象の注文商品が見つかりません', [$order_item_id]);
  359.             throw new NotFoundHttpException();
  360.         }
  361.         // セッションをセット
  362.         $this->session->set(CustomOrderHelper::SESSION_REORDER_ORDER_ITEM_ID$order_item_id);
  363.         return $this->redirect($this->generateUrl('shopping'));
  364.     }
  365.     /**
  366.      * お気に入り商品を表示する.
  367.      *
  368.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  369.      * @Template("Mypage/favorite.twig")
  370.      */
  371.     public function favorite(Request $requestPaginatorInterface $paginator)
  372.     {
  373.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  374.             throw new NotFoundHttpException();
  375.         }
  376.         $Customer $this->getUser();
  377.         // paginator
  378.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  379.         $event = new EventArgs(
  380.             [
  381.                 'qb' => $qb,
  382.                 'Customer' => $Customer,
  383.             ],
  384.             $request
  385.         );
  386.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  387.         $pagination $paginator->paginate(
  388.             $qb,
  389.             $request->get('pageno'1),
  390.             $this->eccubeConfig['eccube_search_pmax'],
  391.             ['wrap-queries' => true]
  392.         );
  393.         return [
  394.             'pagination' => $pagination,
  395.         ];
  396.     }
  397.     /**
  398.      * お気に入り商品を削除する.
  399.      *
  400.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  401.      */
  402.     public function delete(Request $requestProduct $Product)
  403.     {
  404.         $this->isTokenValid();
  405.         $Customer $this->getUser();
  406.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  407.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  408.         if ($CustomerFavoriteProduct) {
  409.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  410.         } else {
  411.             throw new BadRequestHttpException();
  412.         }
  413.         $event = new EventArgs(
  414.             [
  415.                 'Customer' => $Customer,
  416.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  417.             ], $request
  418.         );
  419.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  420.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  421.         return $this->redirect($this->generateUrl('mypage_favorite'));
  422.     }
  423.     /**
  424.      * お気に入りアイテムを削除する.
  425.      *
  426.      * @Route("/mypage/favorite_item/{id}/delete", name="mypage_favorite_item_delete", methods={"GET"}, requirements={"id" = "\d+"})
  427.      */
  428.     public function favoriteItemDelete($id)
  429.     {
  430.         $Customer $this->getUser();
  431.         log_info('お気に入りアイテム削除開始', [$Customer->getId(), $id]);
  432.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->find($id);
  433.         if ($CustomerFavoriteProduct) {
  434.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  435.         } else {
  436.             throw new BadRequestHttpException();
  437.         }
  438.         log_info('お気に入りアイテム削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  439.         $this->addSuccess('お気に入りを削除しました。');
  440.         return $this->redirect($this->generateUrl('mypage_favorite'));
  441.     }
  442. }