app/Customize/Controller/EntryController.php line 118

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;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\Master\CustomerStatus;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Front\EntryType;
  19. use Eccube\Repository\BaseInfoRepository;
  20. use Eccube\Repository\CustomerRepository;
  21. use Eccube\Repository\Master\CustomerStatusRepository;
  22. use Eccube\Service\MailService;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpKernel\Exception as HttpException;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  28. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  29. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  30. use Symfony\Component\Validator\Constraints as Assert;
  31. use Symfony\Component\Validator\Validator\ValidatorInterface;
  32. use Eccube\Service\CartService;
  33. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  34. class EntryController extends AbstractController
  35. {
  36.     /**
  37.      * @var CustomerStatusRepository
  38.      */
  39.     protected $customerStatusRepository;
  40.     /**
  41.      * @var ValidatorInterface
  42.      */
  43.     protected $recursiveValidator;
  44.     /**
  45.      * @var MailService
  46.      */
  47.     protected $mailService;
  48.     /**
  49.      * @var BaseInfo
  50.      */
  51.     protected $BaseInfo;
  52.     /**
  53.      * @var CustomerRepository
  54.      */
  55.     protected $customerRepository;
  56.     /**
  57.      * @var EncoderFactoryInterface
  58.      */
  59.     protected $encoderFactory;
  60.     /**
  61.      * @var TokenStorageInterface
  62.      */
  63.     protected $tokenStorage;
  64.     /**
  65.      * @var \Eccube\Service\CartService
  66.      */
  67.     protected $cartService;
  68.     /**
  69.      * EntryController constructor.
  70.      *
  71.      * @param CartService $cartService
  72.      * @param CustomerStatusRepository $customerStatusRepository
  73.      * @param MailService $mailService
  74.      * @param BaseInfoRepository $baseInfoRepository
  75.      * @param CustomerRepository $customerRepository
  76.      * @param EncoderFactoryInterface $encoderFactory
  77.      * @param ValidatorInterface $validatorInterface
  78.      * @param TokenStorageInterface $tokenStorage
  79.      */
  80.     public function __construct(
  81.         CartService $cartService,
  82.         CustomerStatusRepository $customerStatusRepository,
  83.         MailService $mailService,
  84.         BaseInfoRepository $baseInfoRepository,
  85.         CustomerRepository $customerRepository,
  86.         EncoderFactoryInterface $encoderFactory,
  87.         ValidatorInterface $validatorInterface,
  88.         TokenStorageInterface $tokenStorage
  89.     ) {
  90.         $this->customerStatusRepository $customerStatusRepository;
  91.         $this->mailService $mailService;
  92.         $this->BaseInfo $baseInfoRepository->get();
  93.         $this->customerRepository $customerRepository;
  94.         $this->encoderFactory $encoderFactory;
  95.         $this->recursiveValidator $validatorInterface;
  96.         $this->tokenStorage $tokenStorage;
  97.         $this->cartService $cartService;
  98.     }
  99.     /**
  100.      * 会員登録画面.
  101.      *
  102.      * @Route("/entry", name="entry")
  103.      * @Template("Entry/index.twig")
  104.      */
  105.     public function index(Request $request)
  106.     {
  107.         $p_data $_POST ;
  108.         if(isset($p_data['additional']['member_type'])){
  109.             if($p_data['additional']['member_type'] == 1){
  110.                 $member_type ;
  111.             }
  112.             else{
  113.                 $member_type ;
  114.             }
  115.         }
  116.         else{
  117.             if (isset($p_data['mode'])){
  118.                 $member_type ;
  119.             }
  120.             else{
  121.                 $member_type ;
  122.             }
  123.         }
  124.         if ($this->isGranted('ROLE_USER')) {
  125.             log_info('認証済のためログイン処理をスキップ');
  126.             return $this->redirectToRoute('mypage');
  127.         }
  128.         /** @var $Customer \Eccube\Entity\Customer */
  129.         $Customer $this->customerRepository->newCustomer();
  130.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  131.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  132.         $event = new EventArgs(
  133.             [
  134.                 'builder' => $builder,
  135.                 'Customer' => $Customer,
  136.             ],
  137.             $request
  138.         );
  139.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE$event);
  140.         /* @var $form \Symfony\Component\Form\FormInterface */
  141.         $form $builder->getForm();
  142.         $form->handleRequest($request);
  143.         if ($form->isSubmitted() && $form->isValid() && ($member_type != 3)) {
  144.             switch ($request->get('mode')) {
  145.                 case 'confirm':
  146.                     log_info('会員登録確認開始');
  147.                     log_info('会員登録確認完了');
  148.                     return $this->render(
  149.                         'Entry/confirm.twig',
  150.                             [
  151.                                 'form' => $form->createView(),
  152.                                 'member_type' => $member_type,
  153.                             ]
  154.                         );
  155.                 case 'complete':
  156.                     log_info('会員登録開始');
  157.                     $encoder $this->encoderFactory->getEncoder($Customer);
  158.                     $salt $encoder->createSalt();
  159.                     $password $encoder->encodePassword($Customer->getPassword(), $salt);
  160.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  161.                     $Customer
  162.                         ->setSalt($salt)
  163.                         ->setPassword($password)
  164.                         ->setSecretKey($secretKey)
  165.                         ->setPoint(0);
  166.                     $this->entityManager->persist($Customer);
  167.                     $this->entityManager->flush();
  168. // 有料/無料会員情報DBへ登録
  169.                     $em $this -> getDoctrine() ->getManager() ;
  170.                     $check_id $p_data['entry']['email']['first'] ;
  171.                     $create date("Y-m-d H:i:s") ;
  172.                     $query "select id from dtb_customer where email='$check_id'" ;
  173.                     $statement $em->getConnection()->prepare($query) ;
  174.                     $result $statement->execute() ;
  175.                     $buff $statement->fetchall() ;
  176.                     foreach($buff as $a){
  177.                         $user_id $a['id'] ;
  178.                     }
  179.                     $query ="insert into dtb_customer_info (member_id, member_type) VALUES ($user_id$member_type)" ;
  180.                     $statement $em->getConnection()->prepare($query) ;
  181.                     $result $statement->execute() ;
  182.                     log_info('会員登録完了');
  183.                     $event = new EventArgs(
  184.                         [
  185.                             'form' => $form,
  186.                             'Customer' => $Customer,
  187.                         ],
  188.                         $request
  189.                     );
  190.                     $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE$event);
  191.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  192.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  193.                     if ($activateFlg) {
  194.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  195.                         // メール送信
  196.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  197.                         if ($event->hasResponse()) {
  198.                             return $event->getResponse();
  199.                         }
  200.                         log_info('仮会員登録完了画面へリダイレクト');
  201.                         return $this->redirectToRoute('entry_complete');
  202.                     } else {
  203.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  204.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  205.                         // URLを変更するため完了画面にリダイレクト
  206.                         return $this->redirectToRoute('entry_activate', [
  207.                             'secret_key' => $Customer->getSecretKey(),
  208.                             'qtyInCart' => $qtyInCart,
  209.                         ]);
  210.                     }
  211.             }
  212.         }
  213.         return [
  214.             'form' => $form->createView(),
  215.             'member_type' => $member_type,
  216.         ];
  217.     }
  218.     /**
  219.      * 会員登録完了画面.
  220.      *
  221.      * @Route("/entry/complete", name="entry_complete")
  222.      * @Template("Entry/complete.twig")
  223.      */
  224.     public function complete()
  225.     {
  226.         return [];
  227.     }
  228.     /**
  229.      * 会員のアクティベート(本会員化)を行う.
  230.      *
  231.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate")
  232.      * @Template("Entry/activate.twig")
  233.      */
  234.     public function activate(Request $request$secret_key$qtyInCart null)
  235.     {
  236.         $errors $this->recursiveValidator->validate(
  237.             $secret_key,
  238.             [
  239.                 new Assert\NotBlank(),
  240.                 new Assert\Regex(
  241.                     [
  242.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  243.                     ]
  244.                 ),
  245.             ]
  246.         );
  247.         if(!is_null($qtyInCart)) {
  248.             return [
  249.                 'qtyInCart' => $qtyInCart,
  250.             ];
  251.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  252.             // 会員登録処理を行う
  253.             $qtyInCart $this->entryActivate($request$secret_key);
  254.             return [
  255.                 'qtyInCart' => $qtyInCart,
  256.             ];
  257.         }
  258.         throw new HttpException\NotFoundHttpException();
  259.     }
  260.     /**
  261.      * 会員登録処理を行う
  262.      *
  263.      * @param Request $request
  264.      * @param $secret_key
  265.      * @return \Eccube\Entity\Cart|mixed
  266.      */
  267.     private function entryActivate(Request $request$secret_key)
  268.     {
  269.         log_info('本会員登録開始');
  270.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  271.         if (is_null($Customer)) {
  272.             throw new HttpException\NotFoundHttpException();
  273.         }
  274.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  275.         $Customer->setStatus($CustomerStatus);
  276.         $this->entityManager->persist($Customer);
  277.         $this->entityManager->flush();
  278.         log_info('本会員登録完了');
  279.         $event = new EventArgs(
  280.             [
  281.                 'Customer' => $Customer,
  282.             ],
  283.             $request
  284.         );
  285.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE$event);
  286.         // メール送信
  287.         $this->mailService->sendCustomerCompleteMail($Customer);
  288.         // Assign session carts into customer carts
  289.         $Carts $this->cartService->getCarts();
  290.         $qtyInCart 0;
  291.         foreach ($Carts as $Cart) {
  292.             $qtyInCart += $Cart->getTotalQuantity();
  293.         }
  294.         // 本会員登録してログイン状態にする
  295.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  296.         $this->tokenStorage->setToken($token);
  297.         $request->getSession()->migrate(true);
  298.         if ($qtyInCart) {
  299.             $this->cartService->save();
  300.         }
  301.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  302.         return $qtyInCart;
  303.     }
  304. }