app/Customize/Controller/Mypage/MypageController.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\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 Firebase\JWT\JWT;
  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 AbstractController
  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.     private $secretkeyToken '4UEX8^j8G|CFz]fzV[';
  64.     /**
  65.      * MypageController constructor.
  66.      *
  67.      * @param OrderRepository $orderRepository
  68.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  69.      * @param CartService $cartService
  70.      * @param BaseInfoRepository $baseInfoRepository
  71.      * @param PurchaseFlow $purchaseFlow
  72.      */
  73.     public function __construct(
  74.         OrderRepository $orderRepository,
  75.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  76.         CartService $cartService,
  77.         BaseInfoRepository $baseInfoRepository,
  78.         PurchaseFlow $purchaseFlow
  79.     ) {
  80.         $this->orderRepository $orderRepository;
  81.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  82.         $this->BaseInfo $baseInfoRepository->get();
  83.         $this->cartService $cartService;
  84.         $this->purchaseFlow $purchaseFlow;
  85.     }
  86.     /**
  87.      * ログイン画面.
  88.      *
  89.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  90.      * @Template("Mypage/login.twig")
  91.      */
  92.     public function login(Request $requestAuthenticationUtils $utils//
  93.     {
  94.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  95.             log_info('認証済のためログイン処理をスキップ');
  96.             return $this->redirectToRoute('mypage');
  97.         }
  98.         /* @var $form \Symfony\Component\Form\FormInterface */
  99.         $builder $this->formFactory
  100.             ->createNamedBuilder(''CustomerLoginType::class);
  101.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  102.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  103.             $Customer $this->getUser();
  104.             if ($Customer instanceof Customer) {
  105.                 $builder->get('login_email')
  106.                     ->setData($Customer->getPhoneNumber());
  107.             }
  108.         }
  109.         $event = new EventArgs(
  110.             [
  111.                 'builder' => $builder,
  112.             ],
  113.             $request
  114.         );
  115.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  116.         $form $builder->getForm();
  117.         return [
  118.             'error' => $utils->getLastAuthenticationError(),
  119.             'form' => $form->createView(),
  120.             'customError' => false
  121.         ];
  122.     }
  123.     /**
  124.      * ログイン画面.
  125.      *
  126.      * @Route("/mypage/handleLogin", name="mypage_handleLogin", methods={"POST"})
  127.      */
  128.     public function handleLogin(Request $request//
  129.     {
  130.         dd($request);
  131.     }
  132.     /**
  133.      * マイページ.
  134.      *
  135.      * @Route("/mypage/", name="mypage", methods={"GET"})
  136.      * @Template("Mypage/index.twig")
  137.      */
  138.     public function index(Request $requestPaginatorInterface $paginator)
  139.     {
  140.         $Customer $this->getUser();
  141.         // USE POINT =================================================
  142.         if($Customer && $Customer->getId()) {
  143.             $token JWT::encode(['email' => $Customer->getEmail(), 'phone' => $Customer->getPhoneNumber()], $this->secretkeyToken'HS256');
  144.             $curl curl_init();
  145.             $options = [
  146.                 CURLOPT_RETURNTRANSFER => 1,
  147.                 CURLOPT_URL => env('SYNC_URL')."/api/syncPoint?token=".$token,
  148.                 CURLOPT_POST => false,
  149.                 CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
  150.             ];
  151.             curl_setopt_array($curl$options);
  152.             $output curl_exec($curl);
  153.             $result json_decode($output);
  154.             curl_close($curl);
  155.             if($result) {
  156.                 if ($result->status == 'success') {
  157.                     $point = (int)$result->data->point;
  158.                     $Customer->setPoint($point);
  159.                 } else {
  160.                     $Customer->setPoint(0);
  161.                 }
  162.             } else {
  163.                 $Customer->setPoint(0);
  164.             }
  165.             $this->entityManager->flush();
  166.         }
  167.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  168.         $this->entityManager
  169.             ->getFilters()
  170.             ->enable('incomplete_order_status_hidden');
  171.         // paginator
  172.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  173.         $event = new EventArgs(
  174.             [
  175.                 'qb' => $qb,
  176.                 'Customer' => $Customer,
  177.             ],
  178.             $request
  179.         );
  180.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  181.         $pagination $paginator->paginate(
  182.             $qb,
  183.             $request->get('pageno'1),
  184.             $this->eccubeConfig['eccube_search_pmax']
  185.         );
  186.         return [
  187.             'pagination' => $pagination,
  188.         ];
  189.     }
  190.     /**
  191.      * 購入履歴詳細を表示する.
  192.      *
  193.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  194.      * @Template("Mypage/history.twig")
  195.      */
  196.     public function history(Request $request$order_no)
  197.     {
  198.         $this->entityManager->getFilters()
  199.             ->enable('incomplete_order_status_hidden');
  200.         $Order $this->orderRepository->findOneBy(
  201.             [
  202.                 'order_no' => $order_no,
  203.                 'Customer' => $this->getUser(),
  204.             ]
  205.         );
  206.         $event = new EventArgs(
  207.             [
  208.                 'Order' => $Order,
  209.             ],
  210.             $request
  211.         );
  212.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  213.         /** @var Order $Order */
  214.         $Order $event->getArgument('Order');
  215.         if (!$Order) {
  216.             throw new NotFoundHttpException();
  217.         }
  218.         $stockOrder true;
  219.         foreach ($Order->getOrderItems() as $orderItem) {
  220.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  221.                 $stockOrder false;
  222.                 break;
  223.             }
  224.         }
  225.         return [
  226.             'Order' => $Order,
  227.             'stockOrder' => $stockOrder,
  228.         ];
  229.     }
  230.     /**
  231.      * 再購入を行う.
  232.      *
  233.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  234.      */
  235.     public function order(Request $request$order_no)
  236.     {
  237.         $this->isTokenValid();
  238.         log_info('再注文開始', [$order_no]);
  239.         $Customer $this->getUser();
  240.         /* @var $Order \Eccube\Entity\Order */
  241.         $Order $this->orderRepository->findOneBy(
  242.             [
  243.                 'order_no' => $order_no,
  244.                 'Customer' => $Customer,
  245.             ]
  246.         );
  247.         $event = new EventArgs(
  248.             [
  249.                 'Order' => $Order,
  250.                 'Customer' => $Customer,
  251.             ],
  252.             $request
  253.         );
  254.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  255.         if (!$Order) {
  256.             log_info('対象の注文が見つかりません', [$order_no]);
  257.             throw new NotFoundHttpException();
  258.         }
  259.         // エラーメッセージの配列
  260.         $errorMessages = [];
  261.         foreach ($Order->getOrderItems() as $OrderItem) {
  262.             try {
  263.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  264.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  265.                     // 明細の正規化
  266.                     $Carts $this->cartService->getCarts();
  267.                     foreach ($Carts as $Cart) {
  268.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  269.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  270.                         if ($result->hasError()) {
  271.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  272.                             foreach ($result->getErrors() as $error) {
  273.                                 $errorMessages[] = $error->getMessage();
  274.                             }
  275.                         }
  276.                         foreach ($result->getWarning() as $warning) {
  277.                             $errorMessages[] = $warning->getMessage();
  278.                         }
  279.                     }
  280.                     $this->cartService->save();
  281.                 }
  282.             } catch (CartException $e) {
  283.                 log_info($e->getMessage(), [$order_no]);
  284.                 $this->addRequestError($e->getMessage());
  285.             }
  286.         }
  287.         foreach ($errorMessages as $errorMessage) {
  288.             $this->addRequestError($errorMessage);
  289.         }
  290.         $event = new EventArgs(
  291.             [
  292.                 'Order' => $Order,
  293.                 'Customer' => $Customer,
  294.             ],
  295.             $request
  296.         );
  297.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  298.         if ($event->getResponse() !== null) {
  299.             return $event->getResponse();
  300.         }
  301.         log_info('再注文完了', [$order_no]);
  302.         return $this->redirect($this->generateUrl('cart'));
  303.     }
  304.     /**
  305.      * お気に入り商品を表示する.
  306.      *
  307.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  308.      * @Template("Mypage/favorite.twig")
  309.      */
  310.     public function favorite(Request $requestPaginatorInterface $paginator)
  311.     {
  312.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  313.             throw new NotFoundHttpException();
  314.         }
  315.         $Customer $this->getUser();
  316.         // paginator
  317.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  318.         $event = new EventArgs(
  319.             [
  320.                 'qb' => $qb,
  321.                 'Customer' => $Customer,
  322.             ],
  323.             $request
  324.         );
  325.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  326.         $pagination $paginator->paginate(
  327.             $qb,
  328.             $request->get('pageno'1),
  329.             $this->eccubeConfig['eccube_search_pmax'],
  330.             ['wrap-queries' => true]
  331.         );
  332.         return [
  333.             'pagination' => $pagination,
  334.         ];
  335.     }
  336.     /**
  337.      * お気に入り商品を削除する.
  338.      *
  339.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  340.      */
  341.     public function delete(Request $requestProduct $Product)
  342.     {
  343.         $this->isTokenValid();
  344.         $Customer $this->getUser();
  345.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  346.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  347.         if ($CustomerFavoriteProduct) {
  348.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  349.         } else {
  350.             throw new BadRequestHttpException();
  351.         }
  352.         $event = new EventArgs(
  353.             [
  354.                 'Customer' => $Customer,
  355.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  356.             ], $request
  357.         );
  358.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  359.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  360.         return $this->redirect($this->generateUrl('mypage_favorite'));
  361.     }
  362. }