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

Open in your IDE?
  1. <?php
  2. /* ログイン画面コントローラー
  3.  * ログイン後に遷移する画面を切り替えるため新規作成
  4.  * 2023/12/15 Pico鈴木 規約変更の確認画面 Route=help_new_agreementに飛ばすため修正
  5.  * 2024/05/21 Pico鈴木 購入履歴から再購入したときにノベルティまで再購入対象になる問題を修正
  6.  */
  7. namespace Customize\Controller\Mypage;
  8. use Customize\Repository\EventLogRepository;
  9. use Customize\Repository\EigyoRepository;
  10. use Customize\Repository\QRCodeMikiRepository;
  11. use Eccube\Controller\AbstractController;
  12. use Eccube\Entity\Customer;
  13. use Eccube\Entity\Order;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\Front\CustomerLoginType;
  17. use Eccube\Repository\CustomerRepository;
  18. use Eccube\Repository\OrderRepository;
  19. use Eccube\Service\CartService;
  20. use Eccube\Service\PurchaseFlow\PurchaseContext;
  21. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  27. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  28. class MypageController extends AbstractController
  29. {
  30.     /**
  31.      * @var CartService
  32.      */
  33.     protected $cartService;
  34.     /**
  35.      * @var OrderRepository
  36.      */
  37.     protected $orderRepository;
  38.     /**
  39.      * @var PurchaseFlow
  40.      */
  41.     protected $purchaseFlow;
  42.     /**
  43.      * @var RequestStack
  44.      */
  45.     protected $requestStack;
  46.     /**
  47.      * @var CustomerRepository
  48.      */
  49.     protected $customerRepository;
  50.     /**
  51.      * @var EventLogRepository
  52.      */
  53.     protected $eventLogRepository;
  54.     /**
  55.      * @var EigyoRepository
  56.      */
  57.     protected $eigyoRepository;
  58.     /**
  59.      * @var QRCodeMikiRepository
  60.      */
  61.     protected $qrcodeMikiRepository;
  62.     /**
  63.     * MypageController constructor.
  64.     *
  65.     * @param OrderRepository $orderRepository
  66.     * @param CartService $cartService
  67.     * @param PurchaseFlow $purchaseFlow
  68.     * @param RequestStack $requestStack
  69.     * @param CustomerRepository $customerRepository
  70.     * @param EventLogRepository $eventLogRepository
  71.     * @param EigyoRepository $eigyoRepository
  72.     */
  73.     public function __construct(
  74.         OrderRepository $orderRepository,
  75.         CartService $cartService,
  76.         PurchaseFlow $purchaseFlow,
  77.         RequestStack $requestStack,
  78.         CustomerRepository $customerRepository,
  79.         EventLogRepository $eventLogRepository,
  80.         EigyoRepository $eigyoRepository,
  81.         QRCodeMikiRepository $qrcodeMikiRepository
  82.     )
  83.     {
  84.         $this->orderRepository $orderRepository;
  85.         $this->cartService $cartService;
  86.         $this->purchaseFlow $purchaseFlow;
  87.         $this->requestStack $requestStack;
  88.         $this->customerRepository $customerRepository;
  89.         $this->eventLogRepository $eventLogRepository;
  90.         $this->eigyoRepository $eigyoRepository;
  91.         $this->qrcodeMikiRepository $qrcodeMikiRepository;
  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.             //return $this->redirectToRoute('homepage');
  105.         }
  106.         //Cookie削除 Logout後に本ページが表示されるためこの処理で削除する
  107.         setcookie('ec'''time() - 3600'/');
  108.         setcookie('ec_wp'''time() - 3600'/');
  109.         /* @var $form \Symfony\Component\Form\FormInterface */
  110.         $builder $this->formFactory
  111.             ->createNamedBuilder(''CustomerLoginType::class);
  112.         $builder->get('login_memory')->setData((bool) $request->getSession()->get('_security.login_memory'));
  113.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  114.             $Customer $this->getUser();
  115.             if ($Customer instanceof Customer) {
  116.                 $builder->get('login_email')
  117. //                    ->setData($Customer->getEmail());
  118.                     ->setData($Customer->getHostKono());
  119.             }
  120.         }
  121.         $event = new EventArgs(
  122.             [
  123.                 'builder' => $builder,
  124.             ],
  125.             $request
  126.         );
  127.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  128.         $form $builder->getForm();
  129.         // ログイン前の画面へ戻す
  130.         $error $utils->getLastAuthenticationError();
  131.         if (!$error) {
  132.             $redirect NULL;
  133.             if ($request->getMethod() === 'GET') {
  134.                 switch (TRUE)
  135.                 {
  136.                     case $request->get('BS'):
  137.                         //$redirect = '/mypage/redirect/';
  138.                         $redirect $this->generateUrl('help_new_agreement', ['id' => '1'], UrlGeneratorInterface::ABSOLUTE_URL);
  139.                         break;
  140.                     case $request->get('event_id') == 1:
  141.                         //$redirect = '/mypage/event';
  142.                         $redirect $this->generateUrl('help_new_agreement', ['id' => '3'], UrlGeneratorInterface::ABSOLUTE_URL);
  143.                         break;
  144.                 }
  145.             }
  146.             if (empty($redirect))
  147.             {
  148.                 // ログインエラーがなければ戻り先をセット
  149.                 //$referer = $request->headers->get('referer');
  150.                 $referer $request->getSession()->get('_security.customer.target_path');
  151.                 if ($referer) {
  152.                     // refererチェック
  153.                     $referers parse_url($referer);
  154.                     if ($referers['host'] == $request->getHost()) {
  155.                         // ホストが同一であればrefererをセット
  156.                         $requestStackUri $this->requestStack->getMasterRequest()->getUri();
  157.                         if ($request->getUri() == $requestStackUri) {
  158.                             // ログイン画面遷移直前のuriをセット
  159.                             if (preg_match('/products/'$referer))
  160.                             {
  161.                                 //$redirect = $referer;
  162.                                 $arr explode("/"$referers['path']);
  163.                                 $productCd $arr[count($arr)-1];
  164.                                 $redirect $this->generateUrl('help_new_agreement', ['id' => '2''productCd' => $productCd], UrlGeneratorInterface::ABSOLUTE_URL);
  165.                             }
  166.                         } else {
  167.                             // ログイン必須画面のuriをセット
  168.                             $redirect $requestStackUri;
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.             if (empty($redirect))
  174.             {
  175.                 //$redirect = 'homepage';
  176.                 $this->generateUrl('help_new_agreement', ['id' => '0'], UrlGeneratorInterface::ABSOLUTE_URL);
  177.             }
  178.             $this->setLoginTargetPath($redirect);
  179.         }
  180.         return [
  181.             'error' => $error,
  182.             'form' => $form->createView(),
  183.         ];
  184.     }
  185.     /**
  186.      * loginリダイレクト 会員専用ページ
  187.      * @Route("/mypage/redirect/", name="mypage_redirect", methods={"GET"})
  188.      * @Template("Mypage/redirect.twig")
  189.      */
  190.     public function mypageRedirect(Request $request)
  191.     {
  192.         
  193.         $Customer $this->getUser();
  194.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED'))
  195.         {
  196.             $redirect null;
  197.             switch ($this->getUser()->getBc()->getId())
  198.             {
  199.                 case $this->eccubeConfig['eccube_bckbn1']:
  200.                     //URLが同じだとキャッシュが表示されるのでタイムスタンプを付与する
  201.                     $redirect $this->eccubeConfig['eccube_bckbn1_url'] . '?' time();
  202.                     break;
  203.                 case $this->eccubeConfig['eccube_bckbn2']:
  204.                     $redirect $this->eccubeConfig['eccube_bckbn1_url'] . '?' time();
  205.                     break;
  206.                 case $this->eccubeConfig['eccube_bckbn5']:
  207.                     $redirect $this->eccubeConfig['eccube_bckbn1_url'] . '?' time();
  208.                     break;
  209.                 case $this->eccubeConfig['eccube_bckbn6']:
  210.                     $redirect $this->eccubeConfig['eccube_bckbn1_url'] . '?' time();
  211.                     break;
  212.             }
  213.         }
  214.         return[
  215.             'url' => $redirect
  216.         ];
  217.     }
  218.     /**
  219.      * loginリダイレクト イベントページ
  220.      * カード会員募集イベントでアクセスログを取得後、オリコのカード募集ページにリダイレクト
  221.      * @Route("/mypage/event/", name="mypage_event", methods={"GET"})
  222.      * @Template("Mypage/redirect.twig")
  223.     */
  224.     public function mypageEvent(Request $request)
  225.     {
  226.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED'))
  227.         {
  228.             $Customer $this->getUser();
  229.             $EventLog = new \Customize\Entity\EventLog;
  230.             $EventLog->setEventId(1)
  231.                      ->setAccessDt(new \DateTime())
  232.                      ->setHostKono($Customer->getHostKono())
  233.                      ->setSibuCd($Customer->getSibu()->getSibuCd())
  234.                      ->setEigyoCd($Customer->getEigyoCd())
  235.                      ->setBiko('新メンバーズカード');
  236.             $this->eventLogRepository->save($EventLog);
  237.         }
  238.         return[
  239.             'url' => $this->eccubeConfig['eccube_event1_url']
  240.         ];
  241.     }
  242.     
  243.     /**
  244.      * 再購入を行う.
  245.      * 2024/05/21 Pico鈴木 再購入時にはノベルティを削除する
  246.      * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  247.      */
  248.     public function order(Request $request$order_no)
  249.     {
  250.         $this->isTokenValid();
  251.         log_info('再注文開始', [$order_no]);
  252.         $Customer $this->getUser();
  253.         /* @var $Order \Eccube\Entity\Order */
  254.         $Order $this->orderRepository->findOneBy(
  255.             [
  256.                 'order_no' => $order_no,
  257.                 'Customer' => $Customer,
  258.             ]
  259.         );
  260.         $event = new EventArgs(
  261.             [
  262.                 'Order' => $Order,
  263.                 'Customer' => $Customer,
  264.             ],
  265.             $request
  266.         );
  267.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  268.         if (!$Order) {
  269.             log_info('対象の注文が見つかりません', [$order_no]);
  270.             throw new NotFoundHttpException();
  271.         }
  272.         // エラーメッセージの配列
  273.         $errorMessages = [];
  274.         foreach ($Order->getOrderItems() as $OrderItem) {
  275.             try {
  276.                 if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  277.                     //ノベルティ削除(商品明細行で価格が0はノベルティと判断)
  278.                     if ($OrderItem->isProduct() && $OrderItem->getPrice() == 0) {
  279.                         log_info('ノベルティ(' $OrderItem->getProductName() . ')を再購入対象から除外', [$order_no]);
  280.                         continue;
  281.                     }
  282.                     $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  283.                     // 明細の正規化
  284.                     $Carts $this->cartService->getCarts();
  285.                     foreach ($Carts as $Cart) {
  286.                         $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  287.                         // 復旧不可のエラーが発生した場合は追加した明細を削除.
  288.                         if ($result->hasError()) {
  289.                             $this->cartService->removeProduct($OrderItem->getProductClass());
  290.                             foreach ($result->getErrors() as $error) {
  291.                                 $errorMessages[] = $error->getMessage();
  292.                             }
  293.                         }
  294.                         foreach ($result->getWarning() as $warning) {
  295.                             $errorMessages[] = $warning->getMessage();
  296.                         }
  297.                     }
  298.                     $this->cartService->save();
  299.                 }
  300.             } catch (CartException $e) {
  301.                 log_info($e->getMessage(), [$order_no]);
  302.                 $this->addRequestError($e->getMessage());
  303.             }
  304.         }
  305.         foreach ($errorMessages as $errorMessage) {
  306.             $this->addRequestError($errorMessage);
  307.         }
  308.         $event = new EventArgs(
  309.             [
  310.                 'Order' => $Order,
  311.                 'Customer' => $Customer,
  312.             ],
  313.             $request
  314.         );
  315.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  316.         if ($event->getResponse() !== null) {
  317.             return $event->getResponse();
  318.         }
  319.         log_info('再注文完了', [$order_no]);
  320.         return $this->redirect($this->generateUrl('cart'));
  321.     }
  322.     
  323. }