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

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