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

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller\Mypage;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\BaseInfo;
  5. use Eccube\Entity\Customer;
  6. use Eccube\Entity\Order;
  7. use Eccube\Entity\Product;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Exception\CartException;
  11. use Eccube\Form\Type\Front\CustomerLoginType;
  12. use Eccube\Repository\BaseInfoRepository;
  13. use Eccube\Repository\CustomerFavoriteProductRepository;
  14. use Eccube\Repository\OrderRepository;
  15. use Eccube\Repository\ProductRepository;
  16. use Eccube\Service\CartService;
  17. use Eccube\Service\PurchaseFlow\PurchaseContext;
  18. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  26. // 追加
  27. use Customize\Repository\CategoryRepository;
  28. class MypageController extends AbstractController
  29. {
  30.     protected $categoryRepository;
  31.     protected $productRepository;
  32.     protected $customerFavoriteProductRepository;
  33.     protected $BaseInfo;
  34.     protected $cartService;
  35.     protected $orderRepository;
  36.     protected $purchaseFlow;
  37.     public function __construct(
  38.         CategoryRepository $categoryRepository,
  39.         OrderRepository $orderRepository,
  40.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  41.         CartService $cartService,
  42.         BaseInfoRepository $baseInfoRepository,
  43.         PurchaseFlow $purchaseFlow
  44.     ) {
  45.         $this->categoryRepository $categoryRepository;
  46.         $this->orderRepository $orderRepository;
  47.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  48.         $this->BaseInfo $baseInfoRepository->get();
  49.         $this->cartService $cartService;
  50.         $this->purchaseFlow $purchaseFlow;
  51.     }
  52.     /**
  53.      * ログイン画面.
  54.      *
  55.      * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  56.      * @Template("Mypage/login.twig")
  57.      */
  58.     public function login(Request $requestAuthenticationUtils $utils)
  59.     {
  60.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  61.             log_info('認証済のためログイン処理をスキップ');
  62.             return $this->redirectToRoute('mypage');
  63.         }
  64.         // カートサービスを使用してカート情報を取得
  65.         $cart $this->cartService->getCart();
  66.         // 数量と合計金額を取得
  67.         $cartTotalQuantity 0;
  68.         $cartTotalPrice 0;
  69.         if(!empty($cart)){
  70.             $cartTotalQuantity $cart->getTotalQuantity();
  71.             $cartTotalPrice $cart->getTotalPrice();
  72.         }
  73.         /* @var $form \Symfony\Component\Form\FormInterface */
  74.         $builder $this->formFactory
  75.             ->createNamedBuilder(''CustomerLoginType::class);
  76.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  77.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  78.             $Customer $this->getUser();
  79.             if ($Customer instanceof Customer) {
  80.                 $builder->get('login_email')
  81.                     ->setData($Customer->getEmail());
  82.             }
  83.         }
  84.         $event = new EventArgs(
  85.             [
  86.                 'builder' => $builder,
  87.             ],
  88.             $request
  89.         );
  90.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  91.         $form $builder->getForm();
  92.         // メニュー用カテゴリー一覧
  93.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  94.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  95.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  96.             return $a->getSortNo() <=> $b->getSortNo();
  97.         });
  98.         return [
  99.             'error' => $utils->getLastAuthenticationError(),
  100.             'form' => $form->createView(),
  101.             'cartTotalQuantity' => $cartTotalQuantity,
  102.             'cartTotalPrice' => $cartTotalPrice,
  103.             'sortedChildren' => $sortedChildren,
  104.         ];
  105.     }
  106.     /**
  107.      * マイページ.
  108.      *
  109.      * @Route("/mypage/", name="mypage", methods={"GET"})
  110.      * @Template("Mypage/index.twig")
  111.      */
  112.     public function index(Request $requestPaginatorInterface $paginator)
  113.     {
  114.         // カートサービスを使用してカート情報を取得
  115.         $cart $this->cartService->getCart();
  116.         // 数量と合計金額を取得
  117.         $cartTotalQuantity 0;
  118.         $cartTotalPrice 0;
  119.         if(!empty($cart)){
  120.             $cartTotalQuantity $cart->getTotalQuantity();
  121.             $cartTotalPrice $cart->getTotalPrice();
  122.         }
  123.         $Customer $this->getUser();
  124.         // 購入処理中/決済処理中ステータスの受注を非表示にする.
  125.         $this->entityManager
  126.             ->getFilters()
  127.             ->enable('incomplete_order_status_hidden');
  128.         // paginator
  129.         $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  130.         $event = new EventArgs(
  131.             [
  132.                 'qb' => $qb,
  133.                 'Customer' => $Customer,
  134.             ],
  135.             $request
  136.         );
  137.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  138.         $pagination $paginator->paginate(
  139.             $qb,
  140.             $request->get('pageno'1),
  141.             $this->eccubeConfig['eccube_search_pmax'],
  142.         );
  143.         // メニュー用カテゴリー一覧
  144.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  145.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  146.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  147.             return $a->getSortNo() <=> $b->getSortNo();
  148.         });
  149.         return [
  150.             'pagination' => $pagination,
  151.             'cartTotalQuantity' => $cartTotalQuantity,
  152.             'cartTotalPrice' => $cartTotalPrice,
  153.             'sortedChildren' => $sortedChildren,
  154.         ];
  155.     }
  156.     /**
  157.      * 購入履歴詳細を表示する.
  158.      *
  159.      * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  160.      * @Template("Mypage/history.twig")
  161.      */
  162.     public function history(Request $request$order_no)
  163.     {
  164.         // カートサービスを使用してカート情報を取得
  165.         $cart $this->cartService->getCart();
  166.         // 数量と合計金額を取得
  167.         $cartTotalQuantity 0;
  168.         $cartTotalPrice 0;
  169.         if(!empty($cart)){
  170.             $cartTotalQuantity $cart->getTotalQuantity();
  171.             $cartTotalPrice $cart->getTotalPrice();
  172.         }
  173.         $this->entityManager->getFilters()
  174.             ->enable('incomplete_order_status_hidden');
  175.         $Order $this->orderRepository->findOneBy(
  176.             [
  177.                 'order_no' => $order_no,
  178.                 'Customer' => $this->getUser(),
  179.             ]
  180.         );
  181.         $event = new EventArgs(
  182.             [
  183.                 'Order' => $Order,
  184.             ],
  185.             $request
  186.         );
  187.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  188.         /** @var Order $Order */
  189.         $Order $event->getArgument('Order');
  190.         if (!$Order) {
  191.             throw new NotFoundHttpException();
  192.         }
  193.         $stockOrder true;
  194.         foreach ($Order->getOrderItems() as $orderItem) {
  195.             if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  196.                 $stockOrder false;
  197.                 break;
  198.             }
  199.         }
  200.         // メニュー用カテゴリー一覧
  201.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  202.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  203.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  204.             return $a->getSortNo() <=> $b->getSortNo();
  205.         });
  206.         return [
  207.             'Order' => $Order,
  208.             'stockOrder' => $stockOrder,
  209.             'cartTotalQuantity' => $cartTotalQuantity,
  210.             'cartTotalPrice' => $cartTotalPrice,
  211.             'sortedChildren' => $sortedChildren,
  212.         ];
  213.     }
  214.     /**
  215.      * 再購入を行う.
  216.      *
  217.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  218.      */
  219.     public function order(Request $request$order_no)
  220.     {
  221.         $this->isTokenValid();
  222.         log_info('再注文開始', [$order_no]);
  223.         $Customer $this->getUser();
  224.         /* @var $Order \Eccube\Entity\Order */
  225.         $Order $this->orderRepository->findOneBy(
  226.             [
  227.                 'order_no' => $order_no,
  228.                 'Customer' => $Customer,
  229.             ]
  230.         );
  231.         $event = new EventArgs(
  232.             [
  233.                 'Order' => $Order,
  234.                 'Customer' => $Customer,
  235.             ],
  236.             $request
  237.         );
  238.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  239.         if (!$Order) {
  240.             log_info('対象の注文が見つかりません', [$order_no]);
  241.             throw new NotFoundHttpException();
  242.         }
  243.         // エラーメッセージの配列
  244.         $errorMessages = [];
  245.         foreach ($Order->getOrderItems() as $OrderItem) {
  246.             try {
  247.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  248.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  249.                     // 明細の正規化
  250.                     $Carts $this->cartService->getCarts();
  251.                     foreach ($Carts as $Cart) {
  252.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  253.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  254.                         if ($result->hasError()) {
  255.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  256.                             foreach ($result->getErrors() as $error) {
  257.                                 $errorMessages[] = $error->getMessage();
  258.                             }
  259.                         }
  260.                         foreach ($result->getWarning() as $warning) {
  261.                             $errorMessages[] = $warning->getMessage();
  262.                         }
  263.                     }
  264.                     $this->cartService->save();
  265.                 }
  266.             } catch (CartException $e) {
  267.                 log_info($e->getMessage(), [$order_no]);
  268.                 $this->addRequestError($e->getMessage());
  269.             }
  270.         }
  271.         foreach ($errorMessages as $errorMessage) {
  272.             $this->addRequestError($errorMessage);
  273.         }
  274.         $event = new EventArgs(
  275.             [
  276.                 'Order' => $Order,
  277.                 'Customer' => $Customer,
  278.             ],
  279.             $request
  280.         );
  281.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  282.         if ($event->getResponse() !== null) {
  283.             return $event->getResponse();
  284.         }
  285.         log_info('再注文完了', [$order_no]);
  286.         return $this->redirect($this->generateUrl('cart'));
  287.     }
  288.     /**
  289.      * お気に入り商品を表示する.
  290.      *
  291.      * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  292.      * @Template("Mypage/favorite.twig")
  293.      */
  294.     public function favorite(Request $requestPaginatorInterface $paginator)
  295.     {
  296.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  297.             throw new NotFoundHttpException();
  298.         }
  299.         $Customer $this->getUser();
  300.         // paginator
  301.         $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  302.         $event = new EventArgs(
  303.             [
  304.                 'qb' => $qb,
  305.                 'Customer' => $Customer,
  306.             ],
  307.             $request
  308.         );
  309.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  310.         $pagination $paginator->paginate(
  311.             $qb,
  312.             $request->get('pageno'1),
  313.             $this->eccubeConfig['eccube_search_pmax'],
  314.             ['wrap-queries' => true]
  315.         );
  316.         return [
  317.             'pagination' => $pagination,
  318.         ];
  319.     }
  320.     /**
  321.      * お気に入り商品を削除する.
  322.      *
  323.      * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  324.      */
  325.     public function delete(Request $requestProduct $Product)
  326.     {
  327.         $this->isTokenValid();
  328.         $Customer $this->getUser();
  329.         log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  330.         $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  331.         if ($CustomerFavoriteProduct) {
  332.             $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  333.         } else {
  334.             throw new BadRequestHttpException();
  335.         }
  336.         $event = new EventArgs(
  337.             [
  338.                 'Customer' => $Customer,
  339.                 'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  340.             ], $request
  341.         );
  342.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  343.         log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  344.         return $this->redirect($this->generateUrl('mypage_favorite'));
  345.     }
  346. }