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->isVisibleSchoolKey() && $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.                     'Schools' => $this->schoolRepository->findAll()
  302.                 )
  303.             );
  304.         } else {
  305.             return [
  306.                 'form' => $form->createView(),
  307.                 'School' => $School,
  308.                 'Schools' => $this->schoolRepository->findAll(),
  309.             ];
  310.         }
  311.     }
  312.     /**
  313.      * 会員のアクティベート(本会員化)を行う.
  314.      *
  315.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  316.      * @Template("Entry/activate.twig")
  317.      */
  318.     public function activate(Request $request$secret_key$qtyInCart null)
  319.     {
  320.         $errors $this->recursiveValidator->validate(
  321.             $secret_key,
  322.             [
  323.                 new Assert\NotBlank(),
  324.                 new Assert\Regex(
  325.                     [
  326.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  327.                     ]
  328.                 ),
  329.             ]
  330.         );
  331.         if (!is_null($qtyInCart)) {
  332.             return [
  333.                 'qtyInCart' => $qtyInCart,
  334.             ];
  335.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  336.             // 会員登録処理を行う
  337.             $qtyInCart $this->entryActivate($request$secret_key);
  338.             return [
  339.                 'qtyInCart' => $qtyInCart,
  340.             ];
  341.         }
  342.         throw new HttpException\NotFoundHttpException();
  343.     }
  344.     /**
  345.      * 会員登録処理を行う
  346.      *
  347.      * @param Request $request
  348.      * @param $secret_key
  349.      *
  350.      * @return \Eccube\Entity\Cart|mixed
  351.      */
  352.     private function entryActivate(Request $request$secret_key)
  353.     {
  354.         log_info('本会員登録開始');
  355.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  356.         if (is_null($Customer)) {
  357.             return $this->redirectToRoute('homepage');
  358.         }
  359.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  360.         $Customer->setStatus($CustomerStatus);
  361.         $this->entityManager->persist($Customer);
  362.         $this->entityManager->flush();
  363.         log_info('本会員登録完了');
  364.         $event = new EventArgs(
  365.             [
  366.                 'Customer' => $Customer,
  367.             ],
  368.             $request
  369.         );
  370.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  371.         // メール送信
  372.         $this->mailService->sendCustomerCompleteMail($Customer);
  373.         // Assign session carts into customer carts
  374.         $Carts $this->cartService->getCarts();
  375.         $qtyInCart 0;
  376.         foreach ($Carts as $Cart) {
  377.             $qtyInCart += $Cart->getTotalQuantity();
  378.         }
  379.         // 本会員登録してログイン状態にする
  380.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  381.         $this->tokenStorage->setToken($token);
  382.         $request->getSession()->migrate(true);
  383.         if ($qtyInCart) {
  384.             $this->cartService->save();
  385.         }
  386.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  387.         return $qtyInCart;
  388.     }
  389.     private function createOrUpdateBrothers($brothers$deleted_brothers$Customer)
  390.     {
  391.         foreach ($deleted_brothers as $brother_id) {
  392.             $d_brother $this->brotherRepository->find($brother_id);
  393.             $d_brother->setDelFlg(true);
  394.             $d_brother->setUpdateDate(new \DateTime());
  395.             $this->entityManager->persist($d_brother);
  396.         }
  397.         if (!empty($brothers)) {
  398.             foreach ($brothers as $brother) {
  399.                 if (is_object($brother)) {
  400.                     $brother = (array)$brother;
  401.                 }
  402.                 if (!empty($brother['id'])) {
  403.                     $e_brother $this->brotherRepository->find($brother['id']);
  404.                 } else {
  405.                     $e_brother = new Brother();
  406.                 }
  407.                 $e_brother->setCustomer($Customer);
  408.                 $e_brother->setName($brother['name']);
  409.                 $e_brother->setSchoolYear($brother['school_year']);
  410.                 $e_brother->setSchoolClass($brother['school_class']);
  411.                 $e_brother->setSchool($this->schoolRepository->find($brother['school_id']));
  412.                 $e_brother->setDelFlg(false);
  413.                 $e_brother->setCreateDate(new \DateTime());
  414.                 $e_brother->setUpdateDate(new \DateTime());
  415.                 $this->entityManager->persist($e_brother);
  416.             }
  417.             $this->entityManager->flush();
  418.         }
  419.     }
  420.     /**
  421.      * @Route("/validate_brothers", name="validate_brothers", methods={"POST"})
  422.      */
  423.     public function validateBrothers(Request $request){
  424.         $brothers $request->get('brothers', []);
  425.         $invalid_brothers = [];
  426.         foreach ($brothers as $brother) {
  427.             if($this->schoolRepository->find($brother['school_id'])->getSchoolKey() !== $brother['school_key']){
  428.                 $invalid_brothers[] = $brother['school_key'];
  429.             }
  430.         }
  431.         return $this->json(['invalid_brothers' => $invalid_brothers]);
  432.     }
  433. }