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