src/Eccube/Controller/EntryController.php line 280

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 Eccube\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Master\CustomerStatus;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Front\EntryType;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\CustomerRepository;
  20. use Eccube\Repository\Master\CustomerStatusRepository;
  21. use Eccube\Repository\PageRepository;
  22. use Eccube\Service\CartService;
  23. use Eccube\Service\MailService;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpKernel\Exception as HttpException;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  30. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  31. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  32. use Symfony\Component\Validator\Constraints as Assert;
  33. use Symfony\Component\Validator\Validator\ValidatorInterface;
  34. use Symfony\Component\Form\FormError;
  35. //----代理店カスタマイズ------
  36. //代理店名の取得はカスタムリポジトリを使用
  37. //----------
  38. use Customize\Repository\CustomizeCustomerRepository;
  39. class EntryController extends AbstractController
  40. {
  41.     /**
  42.      * @var CustomerStatusRepository
  43.      */
  44.     protected $customerStatusRepository;
  45.     /**
  46.      * @var ValidatorInterface
  47.      */
  48.     protected $recursiveValidator;
  49.     /**
  50.      * @var MailService
  51.      */
  52.     protected $mailService;
  53.     /**
  54.      * @var BaseInfo
  55.      */
  56.     protected $BaseInfo;
  57.     /**
  58.      * @var CustomerRepository
  59.      */
  60.     protected $customerRepository;
  61.     /**
  62.      * @var EncoderFactoryInterface
  63.      */
  64.     protected $encoderFactory;
  65.     /**
  66.      * @var TokenStorageInterface
  67.      */
  68.     protected $tokenStorage;
  69.     /**
  70.      * @var \Eccube\Service\CartService
  71.      */
  72.     protected $cartService;
  73.     /**
  74.      * @var PageRepository
  75.      */
  76.     protected $pageRepository;
  77.     //----代理店カスタマイズ------
  78.     //代理店名の取得はカスタムリポジトリを使用
  79.     //----------
  80.     private $customizeCustomerRepository;
  81.     /**
  82.      * EntryController constructor.
  83.      *
  84.      * @param CartService $cartService
  85.      * @param CustomerStatusRepository $customerStatusRepository
  86.      * @param MailService $mailService
  87.      * @param BaseInfoRepository $baseInfoRepository
  88.      * @param CustomerRepository $customerRepository
  89.      * @param EncoderFactoryInterface $encoderFactory
  90.      * @param ValidatorInterface $validatorInterface
  91.      * @param TokenStorageInterface $tokenStorage
  92.      */
  93.     public function __construct(
  94.         CartService $cartService,
  95.         CustomerStatusRepository $customerStatusRepository,
  96.         MailService $mailService,
  97.         BaseInfoRepository $baseInfoRepository,
  98.         CustomerRepository $customerRepository,
  99.         EncoderFactoryInterface $encoderFactory,
  100.         ValidatorInterface $validatorInterface,
  101.         TokenStorageInterface $tokenStorage,
  102.         PageRepository $pageRepository,
  103.         CustomizeCustomerRepository $customizeCustomerRepository
  104.     ) {
  105.         $this->customerStatusRepository $customerStatusRepository;
  106.         $this->mailService $mailService;
  107.         $this->BaseInfo $baseInfoRepository->get();
  108.         $this->customerRepository $customerRepository;
  109.         $this->encoderFactory $encoderFactory;
  110.         $this->recursiveValidator $validatorInterface;
  111.         $this->tokenStorage $tokenStorage;
  112.         $this->cartService $cartService;
  113.         $this->pageRepository $pageRepository;
  114.         $this->customizeCustomerRepository $customizeCustomerRepository;
  115.     }
  116.     /**
  117.      * 会員登録画面.
  118.      *
  119.      * @Route("/entry", name="entry", methods={"GET", "POST"})
  120.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  121.      * @Template("Entry/index.twig")
  122.      */
  123.     public function index(Request $request)
  124.     {
  125.         if ($this->isGranted('ROLE_USER')) {
  126.             log_info('認証済のためログイン処理をスキップ');
  127.             return $this->redirectToRoute('mypage');
  128.         }
  129.         /** @var $Customer \Eccube\Entity\Customer */
  130.         $Customer $this->customerRepository->newCustomer();
  131.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  132.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  133.         $event = new EventArgs(
  134.             [
  135.                 'builder' => $builder,
  136.                 'Customer' => $Customer,
  137.             ],
  138.             $request
  139.         );
  140.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  141.         /* @var $form \Symfony\Component\Form\FormInterface */
  142.         $form $builder->getForm();
  143.         $form->handleRequest($request);
  144.         // refパラメータを取得
  145.         $ref $request->query->get('ref');
  146.         if ($ref) {
  147.             // refがGETパラメータにある場合はセッションに保存
  148.             $this->session->set('entry_ref'$ref);
  149.         } else {
  150.             // GETパラメータにrefがない場合はセッションから取得
  151.             $ref $this->session->get('entry_ref');
  152.         }
  153.         $formAction $this->generateUrl('entry', ['ref' => $ref]);
  154.         // 英語環境でのカナバリデーションエラーを除去
  155.         $this->removeKanaValidationErrorsForEnglish($form);
  156.         $agencyCode $Customer->getAgencyCode();
  157.         // 代理店紹介コードのチェック
  158.         if (!empty($agencyCode)) {
  159.             $AgencyId $this->customizeCustomerRepository->findAgencyIdByAgencyCode($agencyCode);
  160.             if (!$AgencyId) {
  161.                 $form->get('agencyCode')->addError(new FormError('form_error.not_found_agency_code'));
  162.                 return [
  163.                     'form' => $form->createView(),
  164.                     'form_action' => $formAction,
  165.                     'ref' => $ref,
  166.                 ];
  167.             }
  168.         }
  169.         if ($form->isSubmitted() && $form->isValid()) {
  170.             // 英語環境の場合、カナフィールドを空に設定
  171.             $this->handleEnglishKanaData($Customer);
  172.             switch ($request->get('mode')) {
  173.                 case 'confirm':
  174.                     log_info('会員登録確認開始');
  175.                     log_info('会員登録確認完了');
  176.                     return $this->render(
  177.                         'Entry/confirm.twig',
  178.                         [
  179.                             'ref' => $ref,
  180.                             'form' => $form->createView(),
  181.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  182.                         ]
  183.                     );
  184.                 case 'complete':
  185.                     log_info('会員登録開始');
  186.                     $encoder $this->encoderFactory->getEncoder($Customer);
  187.                     $salt $encoder->createSalt();
  188.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  189.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  190.                     $Customer
  191.                         ->setSalt($salt)
  192.                         ->setPassword($password)
  193.                         ->setSecretKey($secretKey)
  194.                         ->setPoint(0);
  195.                     $this->entityManager->persist($Customer);
  196.                     $this->entityManager->flush();
  197.                     log_info('会員登録完了');
  198.                     // 会員登録完了時にrefをセッションから削除
  199.                     $this->session->remove('entry_ref');
  200.                     $event = new EventArgs(
  201.                         [
  202.                             'form' => $form,
  203.                             'Customer' => $Customer,
  204.                         ],
  205.                         $request
  206.                     );
  207.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  208.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  209.                     if ($activateFlg) {
  210.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  211.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  212.                         if ($event->hasResponse()) {
  213.                             return $event->getResponse();
  214.                         }
  215.                         log_info('仮会員登録完了画面へリダイレクト');
  216.                         return $this->redirectToRoute('entry_complete');
  217.                     } else {
  218.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  219.                         return $this->redirectToRoute('entry_activate', [
  220.                             'secret_key' => $Customer->getSecretKey(),
  221.                             'qtyInCart' => $qtyInCart,
  222.                         ]);
  223.                     }
  224.             }
  225.         }
  226.         return [
  227.             'form' => $form->createView(),
  228.             'form_action' => $formAction,
  229.             'ref' => $ref,
  230.         ];
  231.     }
  232.     /**
  233.      * 英語環境でのカナバリデーションエラーを除去
  234.      */
  235.     private function removeKanaValidationErrorsForEnglish($form)
  236.     {
  237.         $locale $this->session->get('_locale''ja');
  238.         if ($locale === 'en' && $form->isSubmitted()) {
  239.             // カナフィールドのエラーをクリア
  240.             if ($form->has('kana')) {
  241.                 $kanaForm $form->get('kana');
  242.                 // フォームエラーをクリア
  243.                 $this->clearFormErrors($kanaForm);
  244.                 // 個別フィールドのエラーもクリア
  245.                 if ($kanaForm->has('kana01')) {
  246.                     $this->clearFormErrors($kanaForm->get('kana01'));
  247.                 }
  248.                 if ($kanaForm->has('kana02')) {
  249.                     $this->clearFormErrors($kanaForm->get('kana02'));
  250.                 }
  251.             }
  252.         }
  253.     }
  254.     /**
  255.      * フォームエラーをクリアする
  256.      */
  257.     private function clearFormErrors($form)
  258.     {
  259.         // リフレクションを使ってプライベートなerrors配列をクリア
  260.         $reflection = new \ReflectionClass($form);
  261.         if ($reflection->hasProperty('errors')) {
  262.             $errorsProperty $reflection->getProperty('errors');
  263.             $errorsProperty->setAccessible(true);
  264.             $errorsProperty->setValue($form, []);
  265.         }
  266.     }
  267.     /**
  268.      * 英語環境でのカナデータ処理
  269.      */
  270.     private function handleEnglishKanaData($Customer)
  271.     {
  272.         $locale $this->session->get('_locale''ja');
  273.         if ($locale === 'en') {
  274.             // カナフィールドを空文字または適切な値に設定
  275.             $Customer->setKana01(''); // 空文字
  276.             $Customer->setKana02(''); // 空文字
  277.             // または、名前をそのまま使用する場合:
  278.             $Customer->setKana01($Customer->getName01() ?: '');
  279.             $Customer->setKana02($Customer->getName02() ?: '');
  280.         }
  281.     }
  282.     /**
  283.      * 会員登録完了画面.
  284.      *
  285.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  286.      * @Template("Entry/complete.twig")
  287.      */
  288.     public function complete()
  289.     {
  290.         return [];
  291.     }
  292.     /**
  293.      * 会員のアクティベート(本会員化)を行う.
  294.      *
  295.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  296.      * @Template("Entry/activate.twig")
  297.      */
  298.     public function activate(Request $request$secret_key$qtyInCart null)
  299.     {
  300.         $Customer $this->customerRepository->getEmailChangeCustomerBySecretKey($secret_key);
  301.         //メールアドレス変更時の処理
  302.         if ($Customer) {
  303.             if ($Customer->getEmailChangeFlag() == 1) {
  304.                 //変更前のメールアドレスを取得
  305.                 $currentEmail $Customer->getEmail();
  306.                 //変更後のメールアドレスを取得
  307.                 $changedEmail $Customer->getChangeEmail();
  308.                 //変更後のメールアドレスを保存
  309.                 $Customer->setEmail($changedEmail);
  310.                 //変更フラグをオフへ
  311.                 $Customer->setEmailChangeFlag(0);
  312.                 //変更後のメールアドレスをnullへ戻す
  313.                 $Customer->setChangeEmail(null);
  314.                 $this->entityManager->persist($Customer);
  315.                 $this->entityManager->flush();
  316.                 //変更完了のメール送信
  317.                 $this->mailService->sendChangeMailCompleteMail($Customer$currentEmail$changedEmail);
  318.                 return $this->redirectToRoute('complete_change_email');
  319.             }
  320.         }
  321.         $errors $this->recursiveValidator->validate(
  322.             $secret_key,
  323.             [
  324.                 new Assert\NotBlank(),
  325.                 new Assert\Regex(
  326.                     [
  327.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  328.                     ]
  329.                 ),
  330.             ]
  331.         );
  332.         if (!$this->session->has('eccube.login.target.path')) {
  333.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  334.         }
  335.         if (!is_null($qtyInCart)) {
  336.             return [
  337.                 'qtyInCart' => $qtyInCart,
  338.             ];
  339.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  340.             // 会員登録処理を行う
  341.             $qtyInCart $this->entryActivate($request$secret_key);
  342.             return [
  343.                 'qtyInCart' => $qtyInCart,
  344.             ];
  345.         }
  346.         throw new HttpException\NotFoundHttpException();
  347.     }
  348.     /**
  349.      * 会員登録処理を行う
  350.      *
  351.      * @param Request $request
  352.      * @param $secret_key
  353.      *
  354.      * @return \Eccube\Entity\Cart|mixed
  355.      */
  356.     private function entryActivate(Request $request$secret_key)
  357.     {
  358.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  359.         if (!$Customer) {
  360.             $Customer $this->customerRepository->getEmailChangeCustomerBySecretKey($secret_key);
  361.         }
  362.         //メールアドレス変更時の処理
  363.         if ($Customer->getEmailChangeFlag() == 1) {
  364.             $event = new EventArgs(
  365.                 [
  366.                     'Customer' => $Customer,
  367.                 ],
  368.                 $request
  369.             );
  370.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE);
  371.             return $this->redirectToRoute('complete_change_email');
  372.         }
  373.         log_info('本会員登録開始');
  374.         if (is_null($Customer)) {
  375.             throw new HttpException\NotFoundHttpException();
  376.         }
  377.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  378.         $Customer->setStatus($CustomerStatus);
  379.         $this->entityManager->persist($Customer);
  380.         $this->entityManager->flush();
  381.         log_info('本会員登録完了');
  382.         $event = new EventArgs(
  383.             [
  384.                 'Customer' => $Customer,
  385.             ],
  386.             $request
  387.         );
  388.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  389.         // メール送信
  390.         $this->mailService->sendCustomerCompleteMail($Customer);
  391.         // Assign session carts into customer carts
  392.         $Carts $this->cartService->getCarts();
  393.         $qtyInCart 0;
  394.         foreach ($Carts as $Cart) {
  395.             $qtyInCart += $Cart->getTotalQuantity();
  396.         }
  397.         if ($qtyInCart) {
  398.             $this->cartService->save();
  399.         }
  400.         return $qtyInCart;
  401.     }
  402. }