app/Customize/Controller/EntryController.php line 45

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 Customize\Form\Type\Front\EntryType;
  19. use Eccube\Repository\BaseInfoRepository;
  20. use Eccube\Repository\CustomerRepository;
  21. use Eccube\Repository\Master\CustomerStatusRepository;
  22. use Eccube\Repository\PageRepository;
  23. use Eccube\Service\CartService;
  24. use Customize\Service\MailService;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Exception as HttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  31. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  32. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  33. use Symfony\Component\Validator\Constraints as Assert;
  34. use Symfony\Component\Validator\Validator\ValidatorInterface;
  35. use Customize\Repository\SchoolRepository;
  36. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  37. use Customize\Form\Type\Front\SchoolCertificationType;
  38. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  39. use Customize\Repository\BrotherRepository;
  40. use Customize\Entity\Brother;
  41. class EntryController extends AbstractController
  42. {
  43.     /**
  44.      * @var CustomerStatusRepository
  45.      */
  46.     protected $customerStatusRepository;
  47.     /**
  48.      * @var ValidatorInterface
  49.      */
  50.     protected $recursiveValidator;
  51.     /**
  52.      * @var MailService
  53.      */
  54.     protected $mailService;
  55.     /**
  56.      * @var BaseInfo
  57.      */
  58.     protected $BaseInfo;
  59.     /**
  60.      * @var CustomerRepository
  61.      */
  62.     protected $customerRepository;
  63.     /**
  64.      * @var EncoderFactoryInterface
  65.      */
  66.     protected $encoderFactory;
  67.     /**
  68.      * @var TokenStorageInterface
  69.      */
  70.     protected $tokenStorage;
  71.     /**
  72.      * @var \Eccube\Service\CartService
  73.      */
  74.     protected $cartService;
  75.     /**
  76.      * @var PageRepository
  77.      */
  78.     protected $pageRepository;
  79.     /**
  80.      * @var schoolRepository
  81.      */
  82.     protected $schoolRepository;
  83.     protected $session;
  84.     /**
  85.      * @var BrotherRepository
  86.      */
  87.     protected $brotherRepository;
  88.     /**
  89.      * EntryController constructor.
  90.      *
  91.      * @param CartService $cartService
  92.      * @param CustomerStatusRepository $customerStatusRepository
  93.      * @param MailService $mailService
  94.      * @param BaseInfoRepository $baseInfoRepository
  95.      * @param CustomerRepository $customerRepository
  96.      * @param EncoderFactoryInterface $encoderFactory
  97.      * @param ValidatorInterface $validatorInterface
  98.      * @param TokenStorageInterface $tokenStorage
  99.      * @param SchoolRepository $schoolRepository
  100.      * @param SessionInterface $session
  101.      */
  102.     public function __construct(
  103.         CartService $cartService,
  104.         CustomerStatusRepository $customerStatusRepository,
  105.         MailService $mailService,
  106.         BaseInfoRepository $baseInfoRepository,
  107.         CustomerRepository $customerRepository,
  108.         EncoderFactoryInterface $encoderFactory,
  109.         ValidatorInterface $validatorInterface,
  110.         TokenStorageInterface $tokenStorage,
  111.         PageRepository $pageRepository,
  112.         SchoolRepository $schoolRepository,
  113.         SessionInterface $session,
  114.         BrotherRepository $brotherRepository
  115.     ) {
  116.         $this->customerStatusRepository $customerStatusRepository;
  117.         $this->mailService $mailService;
  118.         $this->BaseInfo $baseInfoRepository->get();
  119.         $this->customerRepository $customerRepository;
  120.         $this->encoderFactory $encoderFactory;
  121.         $this->recursiveValidator $validatorInterface;
  122.         $this->tokenStorage $tokenStorage;
  123.         $this->cartService $cartService;
  124.         $this->pageRepository $pageRepository;
  125.         $this->schoolRepository $schoolRepository;
  126.         $this->session $session;
  127.         $this->brotherRepository $brotherRepository;
  128.     }
  129.     /**
  130.      * 会員登録完了画面.
  131.      *
  132.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  133.      * @Template("Entry/complete.twig")
  134.      */
  135.     public function complete()
  136.     {
  137.         return [];
  138.     }
  139.     /**
  140.      * 会員登録画面.
  141.      *
  142.      * @Route("/entry/{gcd}", name="entry", methods={"GET", "POST"}, defaults={"gcd"=null})
  143.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  144.      * @Template("Entry/index.twig")
  145.      */
  146.     public function index(Request $request$gcd)
  147.     {
  148.         if ($this->isGranted('ROLE_USER')) {
  149.             log_info('認証済のためログイン処理をスキップ');
  150.             return $this->redirectToRoute('mypage');
  151.         }
  152.         /** @var $Customer \Eccube\Entity\Customer */
  153.         $Customer $this->customerRepository->newCustomer();
  154.         $School null;
  155.         $SchoolId $this->session->get('eccube.school');
  156.         if ($SchoolId 0) {
  157.             $School $this->schoolRepository->find($SchoolId);
  158.             if ($School) {
  159.                 $Customer->setSchool($School);
  160.                 $Customer->setSchoolCode($School->getSchoolCode());
  161.             }
  162.         }
  163.         if (!$School) {
  164.             $builder $this->formFactory->createNamedBuilder(''SchoolCertificationType::class, null, ['allow_extra_fields' => true]);
  165.             $form $builder->getForm();
  166.         } else {
  167.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  168.             $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  169.             $event = new EventArgs(
  170.                 [
  171.                     'builder' => $builder,
  172.                     'Customer' => $Customer,
  173.                 ],
  174.                 $request
  175.             );
  176.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  177.             /* @var $form \Symfony\Component\Form\FormInterface */
  178.             $form $builder->getForm();
  179.         }
  180.         $form->handleRequest($request);
  181.         $certification_error_flg 0;
  182.         $school_not_found false;
  183.         if ($form->isSubmitted() && $form->isValid()) {
  184.             switch ($request->get('mode')) {
  185.                 case 'certify':
  186.                     if ($gcd) {
  187.                         $School2 $this->schoolRepository->findOneBy([
  188.                             'entry_url' => $gcd,
  189.                             'school_key' => $form->get('school_key')->getData(),
  190.                         ]);
  191.                     } else {
  192.                         $School2 $form->get('School')->getData();
  193.                         if ($School2->getSchoolKey() !== $form->get('school_key')->getData()) {
  194.                             $School2 null;
  195.                         }
  196.                     }
  197.                     if (!$School2) {
  198.                         $certification_error_flg 1;
  199.                     } else {
  200.                         $this->session->set('eccube.school'$School2->getSchoolId());
  201.                         return $this->redirectToRoute('entry');
  202.                     }
  203.                     break;
  204.                 case 'confirm':
  205.                     log_info('会員登録確認開始');
  206.                     log_info('会員登録確認完了');
  207.                     $brothers $request->get('brothers''');
  208.                     $this->session->set('brothers'$brothers);
  209.                     $brothers =  json_decode($brothers);
  210.                     if (!empty($brothers)) {
  211.                         foreach ($brothers as $brother) {
  212.                             $brother_school $this->schoolRepository->find($brother->school_id);
  213.                             if ($brother_school) {
  214.                                 $brother->school_name $brother_school->getSchoolName();
  215.                             } else {
  216.                                 $brother->school_name null;
  217.                             }
  218.                         }
  219.                     }
  220.                     return $this->render(
  221.                         'Entry/confirm.twig',
  222.                         [
  223.                             'form' => $form->createView(),
  224.                             'School' => $School,
  225.                             'Brothers' => $brothers,
  226.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  227.                         ]
  228.                     );
  229.                 case 'complete':
  230.                     log_info('会員登録開始');
  231.                     $encoder $this->encoderFactory->getEncoder($Customer);
  232.                     $salt $encoder->createSalt();
  233.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  234.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  235.                     $phoneNumber $Customer->getTel01() . $Customer->getTel02() . $Customer->getTel03();
  236.                     $Customer
  237.                         ->setSalt($salt)
  238.                         ->setPassword($password)
  239.                         ->setSecretKey($secretKey)
  240.                         ->setPhoneNumber($phoneNumber)
  241.                         ->setPoint(0)
  242.                         ->setMailmagaFlg(1)
  243.                         ->setSchool($School);
  244.                     $this->entityManager->persist($Customer);
  245.                     $this->entityManager->flush();
  246.                     $brothers $this->session->get('brothers''');
  247.                     $brothers json_decode($brothers);
  248.                     $this->createOrUpdateBrothers($brothers, [], $Customer);
  249.                     log_info('会員登録完了');
  250.                     $event = new EventArgs(
  251.                         [
  252.                             'form' => $form,
  253.                             'Customer' => $Customer,
  254.                         ],
  255.                         $request
  256.                     );
  257.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  258.                     $this->session->set('eccube.school'0);
  259.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  260.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  261.                     if ($activateFlg && !empty($Customer->getEmail())) {
  262.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  263.                         // メール送信
  264.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  265.                         if ($event->hasResponse()) {
  266.                             return $event->getResponse();
  267.                         }
  268.                         log_info('仮会員登録完了画面へリダイレクト');
  269.                         return $this->redirectToRoute('entry_complete');
  270.                     } else {
  271.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  272.                         if(!empty($Customer->getEmail())){
  273.                             $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  274.                             // URLを変更するため完了画面にリダイレクト
  275.                             return $this->redirectToRoute('entry_activate', [
  276.                                 'secret_key' => $Customer->getSecretKey(),
  277.                                 'qtyInCart' => $qtyInCart,
  278.                             ]);
  279.                         }
  280.                         else
  281.                             return $this->redirectToRoute('homepage');
  282.                     }
  283.             }
  284.         }
  285.         if (!$School) {
  286.             $School2 null;
  287.             if ($gcd) {
  288.                 $School2 $this->schoolRepository->findOneBy(['entry_url' => $gcd]);
  289.                 if (!$School2) {
  290.                     $school_not_found true;
  291.                 }
  292.             }
  293.             return $this->render(
  294.                 "Entry/school_certification.twig",
  295.                 array(
  296.                     'School' => $School2,
  297.                     'gcd' => $gcd,
  298.                     'certification_error_flg' => $certification_error_flg,
  299.                     'school_not_found' => $school_not_found,
  300.                     'form' => $form->createView()
  301.                 )
  302.             );
  303.         } else {
  304.             return [
  305.                 'form' => $form->createView(),
  306.                 'School' => $School,
  307.                 'Schools' => $this->schoolRepository->findAll(),
  308.             ];
  309.         }
  310.     }
  311.     /**
  312.      * 会員のアクティベート(本会員化)を行う.
  313.      *
  314.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  315.      * @Template("Entry/activate.twig")
  316.      */
  317.     public function activate(Request $request$secret_key$qtyInCart null)
  318.     {
  319.         $errors $this->recursiveValidator->validate(
  320.             $secret_key,
  321.             [
  322.                 new Assert\NotBlank(),
  323.                 new Assert\Regex(
  324.                     [
  325.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  326.                     ]
  327.                 ),
  328.             ]
  329.         );
  330.         if (!is_null($qtyInCart)) {
  331.             return [
  332.                 'qtyInCart' => $qtyInCart,
  333.             ];
  334.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  335.             // 会員登録処理を行う
  336.             $qtyInCart $this->entryActivate($request$secret_key);
  337.             return [
  338.                 'qtyInCart' => $qtyInCart,
  339.             ];
  340.         }
  341.         throw new HttpException\NotFoundHttpException();
  342.     }
  343.     /**
  344.      * 会員登録処理を行う
  345.      *
  346.      * @param Request $request
  347.      * @param $secret_key
  348.      *
  349.      * @return \Eccube\Entity\Cart|mixed
  350.      */
  351.     private function entryActivate(Request $request$secret_key)
  352.     {
  353.         log_info('本会員登録開始');
  354.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  355.         if (is_null($Customer)) {
  356.             return $this->redirectToRoute('homepage');
  357.         }
  358.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  359.         $Customer->setStatus($CustomerStatus);
  360.         $this->entityManager->persist($Customer);
  361.         $this->entityManager->flush();
  362.         log_info('本会員登録完了');
  363.         $event = new EventArgs(
  364.             [
  365.                 'Customer' => $Customer,
  366.             ],
  367.             $request
  368.         );
  369.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  370.         // メール送信
  371.         $this->mailService->sendCustomerCompleteMail($Customer);
  372.         // Assign session carts into customer carts
  373.         $Carts $this->cartService->getCarts();
  374.         $qtyInCart 0;
  375.         foreach ($Carts as $Cart) {
  376.             $qtyInCart += $Cart->getTotalQuantity();
  377.         }
  378.         // 本会員登録してログイン状態にする
  379.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  380.         $this->tokenStorage->setToken($token);
  381.         $request->getSession()->migrate(true);
  382.         if ($qtyInCart) {
  383.             $this->cartService->save();
  384.         }
  385.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  386.         return $qtyInCart;
  387.     }
  388.     private function createOrUpdateBrothers($brothers$deleted_brothers$Customer)
  389.     {
  390.         foreach ($deleted_brothers as $brother_id) {
  391.             $d_brother $this->brotherRepository->find($brother_id);
  392.             $d_brother->setDelFlg(true);
  393.             $d_brother->setUpdateDate(new \DateTime());
  394.             $this->entityManager->persist($d_brother);
  395.         }
  396.         if (!empty($brothers)) {
  397.             foreach ($brothers as $brother) {
  398.                 if (is_object($brother)) {
  399.                     $brother = (array)$brother;
  400.                 }
  401.                 if (!empty($brother['id'])) {
  402.                     $e_brother $this->brotherRepository->find($brother['id']);
  403.                 } else {
  404.                     $e_brother = new Brother();
  405.                 }
  406.                 $e_brother->setCustomer($Customer);
  407.                 $e_brother->setName($brother['name']);
  408.                 $e_brother->setSchoolYear($brother['school_year']);
  409.                 $e_brother->setSchoolClass($brother['school_class']);
  410.                 $e_brother->setSchool($this->schoolRepository->find($brother['school_id']));
  411.                 $e_brother->setDelFlg(false);
  412.                 $e_brother->setCreateDate(new \DateTime());
  413.                 $e_brother->setUpdateDate(new \DateTime());
  414.                 $this->entityManager->persist($e_brother);
  415.             }
  416.             $this->entityManager->flush();
  417.         }
  418.     }
  419. }