src/Eccube/Controller/Mypage/MypageController.php line 146

Open in your IDE?
  1. <?php
  2. namespace Eccube\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 phpseclib3\Tests\Functional\Net\SFTPLargeFileTest;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  27. use Plugin\ProductListSaleType\Repository\PlazaRepository;
  28. class MypageController extends AbstractController
  29. {
  30.   /**
  31.    * @var ProductRepository
  32.    */
  33.   protected $productRepository;
  34.   /**
  35.    * @var CustomerFavoriteProductRepository
  36.    */
  37.   protected $customerFavoriteProductRepository;
  38.   /**
  39.    * @var BaseInfo
  40.    */
  41.   protected $BaseInfo;
  42.   /**
  43.    * @var CartService
  44.    */
  45.   protected $cartService;
  46.   /**
  47.    * @var OrderRepository
  48.    */
  49.   protected $orderRepository;
  50.   /**
  51.    * @var PurchaseFlow
  52.    */
  53.   protected $purchaseFlow;
  54.   /**
  55.    * @var PlazaRepository
  56.    */
  57.   private $plazaRepository;
  58.   /**
  59.    * MypageController constructor.
  60.    *
  61.    * @param OrderRepository $orderRepository
  62.    * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  63.    * @param CartService $cartService
  64.    * @param BaseInfoRepository $baseInfoRepository
  65.    * @param PurchaseFlow $purchaseFlow
  66.    * @param PlazaRepository $plazaRepository
  67.    */
  68.   public function __construct(
  69.     OrderRepository                   $orderRepository,
  70.     CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  71.     CartService                       $cartService,
  72.     BaseInfoRepository                $baseInfoRepository,
  73.     PurchaseFlow                      $purchaseFlow,
  74.     PlazaRepository                   $plazaRepository
  75.   )
  76.   {
  77.     $this->orderRepository $orderRepository;
  78.     $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  79.     $this->BaseInfo $baseInfoRepository->get();
  80.     $this->cartService $cartService;
  81.     $this->purchaseFlow $purchaseFlow;
  82.     $this->plazaRepository $plazaRepository;
  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('mypage');
  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.     $Customer $this->getUser();
  129.     // 購入処理中/決済処理中ステータスの受注を非表示にする.
  130.     $this->entityManager
  131.       ->getFilters()
  132.       ->enable('incomplete_order_status_hidden');
  133.     // プラザ/パレス判定
  134.     $session $request->getSession();
  135.     $plaza_flg $session->get('header_footer_layout');
  136.     // paginator
  137.     $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  138.     // sale_typeでフィルタリング
  139.     $qb->innerJoin('o.OrderItems''oi')
  140.        ->innerJoin('oi.ProductClass''pc')
  141.        ->innerJoin('pc.SaleType''st');
  142.     if ($plaza_flg == 'plaza') {
  143.       // プラザ: sale_type_id = 2
  144.       $qb->andWhere('st.id = :sale_type_id')
  145.          ->setParameter('sale_type_id'2);
  146.     } else {
  147.       // パレス: sale_type_id = 1 or 3
  148.       $qb->andWhere('st.id IN (:sale_type_ids)')
  149.          ->setParameter('sale_type_ids', [13]);
  150.     }
  151.     $event = new EventArgs(
  152.       [
  153.         'qb' => $qb,
  154.         'Customer' => $Customer,
  155.       ],
  156.       $request
  157.     );
  158.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  159.     $pagination $paginator->paginate(
  160.       $qb,
  161.       $request->get('pageno'1),
  162.       $this->eccubeConfig['eccube_search_pmax']
  163.     );
  164.     return [
  165.       'pagination' => $pagination,
  166.       'plaza_flg' => $plaza_flg,
  167.     ];
  168.   }
  169.   /**
  170.    * 購入履歴詳細を表示する.
  171.    *
  172.    * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  173.    * @Template("Mypage/history.twig")
  174.    */
  175.   public function history(Request $request$order_no)
  176.   {
  177.     $this->entityManager->getFilters()
  178.       ->enable('incomplete_order_status_hidden');
  179.     $Order $this->orderRepository->findOneBy(
  180.       [
  181.         'order_no' => $order_no,
  182.         'Customer' => $this->getUser(),
  183.       ]
  184.     );
  185.     $event = new EventArgs(
  186.       [
  187.         'Order' => $Order,
  188.       ],
  189.       $request
  190.     );
  191.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  192.     /** @var Order $Order */
  193.     $Order $event->getArgument('Order');
  194.     if (!$Order) {
  195.       throw new NotFoundHttpException();
  196.     }
  197.     $stockOrder true;
  198.     foreach ($Order->getOrderItems() as $orderItem) {
  199.       if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  200.         $stockOrder false;
  201.         break;
  202.       }
  203.     }
  204.     return [
  205.       'Order' => $Order,
  206.       'stockOrder' => $stockOrder,
  207.     ];
  208.   }
  209.   /**
  210.    * 再購入を行う.
  211.    *
  212.    * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  213.    */
  214.   public function order(Request $request$order_no)
  215.   {
  216.     $this->isTokenValid();
  217.     log_info('再注文開始', [$order_no]);
  218.     $Customer $this->getUser();
  219.     /* @var $Order \Eccube\Entity\Order */
  220.     $Order $this->orderRepository->findOneBy(
  221.       [
  222.         'order_no' => $order_no,
  223.         'Customer' => $Customer,
  224.       ]
  225.     );
  226.     $event = new EventArgs(
  227.       [
  228.         'Order' => $Order,
  229.         'Customer' => $Customer,
  230.       ],
  231.       $request
  232.     );
  233.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  234.     if (!$Order) {
  235.       log_info('対象の注文が見つかりません', [$order_no]);
  236.       throw new NotFoundHttpException();
  237.     }
  238.     // エラーメッセージの配列
  239.     $errorMessages = [];
  240.     foreach ($Order->getOrderItems() as $OrderItem) {
  241.       try {
  242.         if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  243.           $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  244.           // 明細の正規化
  245.           $Carts $this->cartService->getCarts();
  246.           foreach ($Carts as $Cart) {
  247.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  248.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  249.             if ($result->hasError()) {
  250.               $this->cartService->removeProduct($OrderItem->getProductClass());
  251.               foreach ($result->getErrors() as $error) {
  252.                 $errorMessages[] = $error->getMessage();
  253.               }
  254.             }
  255.             foreach ($result->getWarning() as $warning) {
  256.               $errorMessages[] = $warning->getMessage();
  257.             }
  258.           }
  259.           $this->cartService->save();
  260.         }
  261.       } catch (CartException $e) {
  262.         log_info($e->getMessage(), [$order_no]);
  263.         $this->addRequestError($e->getMessage());
  264.       }
  265.     }
  266.     foreach ($errorMessages as $errorMessage) {
  267.       $this->addRequestError($errorMessage);
  268.     }
  269.     $event = new EventArgs(
  270.       [
  271.         'Order' => $Order,
  272.         'Customer' => $Customer,
  273.       ],
  274.       $request
  275.     );
  276.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  277.     if ($event->getResponse() !== null) {
  278.       return $event->getResponse();
  279.     }
  280.     log_info('再注文完了', [$order_no]);
  281.     return $this->redirect($this->generateUrl('cart'));
  282.   }
  283.   /**
  284.    * お気に入り商品を表示する.
  285.    *
  286.    * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  287.    * @Template("Mypage/favorite.twig")
  288.    */
  289.   public function favorite(Request $requestPaginatorInterface $paginator)
  290.   {
  291.     if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  292.       throw new NotFoundHttpException();
  293.     }
  294.     $Customer $this->getUser();
  295.     // paginator
  296.     $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  297.     $event = new EventArgs(
  298.       [
  299.         'qb' => $qb,
  300.         'Customer' => $Customer,
  301.       ],
  302.       $request
  303.     );
  304.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  305.     // パレスとプラザのお気に入りを分ける
  306.     $session $request->getSession();
  307.     $plaza_flg $session->get('header_footer_layout');
  308.     if($plaza_flg == "plaza"){
  309.       $saleType "2";
  310.     }else{
  311.       $saleType "1";
  312.     }
  313.     $this->plazaRepository->addSaleTypeCondition($qb,$saleType);
  314.     $pagination $paginator->paginate(
  315.       $qb,
  316.       $request->get('pageno'1),
  317.       $this->eccubeConfig['eccube_search_pmax'],
  318.       ['wrap-queries' => true]
  319.     );
  320.     return [
  321.       'pagination' => $pagination,
  322.     ];
  323.   }
  324.   /**
  325.    * お気に入り商品を削除する.
  326.    *
  327.    * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  328.    */
  329.   public function delete(Request $requestProduct $Product)
  330.   {
  331.     $this->isTokenValid();
  332.     $Customer $this->getUser();
  333.     log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  334.     $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  335.     if ($CustomerFavoriteProduct) {
  336.       $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  337.     } else {
  338.       throw new BadRequestHttpException();
  339.     }
  340.     $event = new EventArgs(
  341.       [
  342.         'Customer' => $Customer,
  343.         'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  344.       ], $request
  345.     );
  346.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  347.     log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  348.     return $this->redirect($this->generateUrl('mypage_favorite'));
  349.   }
  350. }