app/Customize/Controller/CustomizeMypageController.php line 116

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