app/Customize/Controller/EntryController.php line 73

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Eccube\Entity\BaseInfo;
  4. use Eccube\Entity\Master\CustomerStatus;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Event\EventArgs;
  7. use Customize\Form\Type\Front\EntryType;
  8. use Eccube\Repository\BaseInfoRepository;
  9. use Eccube\Repository\CustomerRepository;
  10. use Eccube\Repository\Master\CustomerStatusRepository;
  11. use Eccube\Repository\PageRepository;
  12. use Eccube\Service\CartService;
  13. use Eccube\Service\MailService;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Exception as HttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  21. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. use Symfony\Component\Validator\Validator\ValidatorInterface;
  24. // 追加
  25. use Eccube\Controller\AbstractController;
  26. use Customize\Repository\CategoryRepository;
  27. class EntryController extends AbstractController
  28. {
  29.     protected $categoryRepository;
  30.     protected $customerStatusRepository;
  31.     protected $recursiveValidator;
  32.     protected $mailService;
  33.     protected $BaseInfo;
  34.     protected $customerRepository;
  35.     protected $encoderFactory;
  36.     protected $tokenStorage;
  37.     protected $cartService;
  38.     protected $pageRepository;
  39.     public function __construct(
  40.         CategoryRepository $categoryRepository,
  41.         CartService $cartService,
  42.         CustomerStatusRepository $customerStatusRepository,
  43.         MailService $mailService,
  44.         BaseInfoRepository $baseInfoRepository,
  45.         CustomerRepository $customerRepository,
  46.         EncoderFactoryInterface $encoderFactory,
  47.         ValidatorInterface $validatorInterface,
  48.         TokenStorageInterface $tokenStorage,
  49.         PageRepository $pageRepository
  50.     ) {
  51.         $this->categoryRepository $categoryRepository;
  52.         $this->customerStatusRepository $customerStatusRepository;
  53.         $this->mailService $mailService;
  54.         $this->BaseInfo $baseInfoRepository->get();
  55.         $this->customerRepository $customerRepository;
  56.         $this->encoderFactory $encoderFactory;
  57.         $this->recursiveValidator $validatorInterface;
  58.         $this->tokenStorage $tokenStorage;
  59.         $this->cartService $cartService;
  60.         $this->pageRepository $pageRepository;
  61.     }
  62.     /**
  63.      * ご利用規約.
  64.      *
  65.      * @Route("/entry/kiyaku", name="entry", methods={"GET", "POST"})
  66.      * @Template("Entry/kiyaku.twig")
  67.      */
  68.     public function kiyaku(Request $request)
  69.     {
  70.         // カートサービスを使用してカート情報を取得
  71.         $cart $this->cartService->getCart();
  72.         // 数量と合計金額を取得
  73.         $cartTotalQuantity 0;
  74.         $cartTotalPrice 0;
  75.         if(!empty($cart)){
  76.             $cartTotalQuantity $cart->getTotalQuantity();
  77.             $cartTotalPrice $cart->getTotalPrice();
  78.         }
  79.         // メニュー用カテゴリー一覧
  80.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  81.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  82.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  83.             return $a->getSortNo() <=> $b->getSortNo();
  84.         });
  85.         return [
  86.             'cartTotalQuantity' => $cartTotalQuantity,
  87.             'cartTotalPrice' => $cartTotalPrice,
  88.             'sortedChildren' => $sortedChildren,
  89.         ];
  90.     }
  91.     /**
  92.      * 会員登録画面.
  93.      *
  94.      * @Route("/entry", name="entry_input", methods={"GET", "POST"})
  95.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  96.      * @Template("Entry/index.twig")
  97.      */
  98.     public function input(Request $request)
  99.     {
  100.         if ($this->isGranted('ROLE_USER')) {
  101.             log_info('認証済のためログイン処理をスキップ');
  102.             return $this->redirectToRoute('mypage');
  103.         }
  104.         // カートサービスを使用してカート情報を取得
  105.         $cart $this->cartService->getCart();
  106.         // 数量と合計金額を取得
  107.         $cartTotalQuantity 0;
  108.         $cartTotalPrice 0;
  109.         if(!empty($cart)){
  110.             $cartTotalQuantity $cart->getTotalQuantity();
  111.             $cartTotalPrice $cart->getTotalPrice();
  112.         }
  113.         /** @var $Customer \Eccube\Entity\Customer */
  114.         $Customer $this->customerRepository->newCustomer();
  115.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  116.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  117.         $event = new EventArgs(
  118.             [
  119.                 'builder' => $builder,
  120.                 'Customer' => $Customer,
  121.             ],
  122.             $request
  123.         );
  124.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  125.         /* @var $form \Symfony\Component\Form\FormInterface */
  126.         $form $builder->getForm();
  127.         $form->handleRequest($request);
  128.         // メニュー用カテゴリー一覧
  129.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  130.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  131.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  132.             return $a->getSortNo() <=> $b->getSortNo();
  133.         });
  134.         if($form->isSubmitted() && $form->isValid()){
  135.             switch ($request->get('mode')) {
  136.                 case 'confirm':
  137.                     log_info('会員登録確認開始');
  138.                     log_info('会員登録確認完了');
  139.                     return $this->render(
  140.                         'Entry/confirm.twig',
  141.                         [
  142.                             'form' => $form->createView(),
  143.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  144.                             'cartTotalQuantity' => $cartTotalQuantity,
  145.                             'cartTotalPrice' => $cartTotalPrice,
  146.                             'sortedChildren' => $sortedChildren,
  147.                         ]
  148.                     );
  149.                 case 'complete':
  150.                     log_info('会員登録開始');
  151.                     $encoder $this->encoderFactory->getEncoder($Customer);
  152.                     $salt $encoder->createSalt();
  153.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  154.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  155.                     $Customer
  156.                         ->setSalt($salt)
  157.                         ->setPassword($password)
  158.                         ->setSecretKey($secretKey)
  159.                         ->setPoint(0);
  160.                     $this->entityManager->persist($Customer);
  161.                     $this->entityManager->flush();
  162.                     log_info('会員登録完了');
  163.                     $event = new EventArgs(
  164.                         [
  165.                             'form' => $form,
  166.                             'Customer' => $Customer,
  167.                         ],
  168.                         $request
  169.                     );
  170.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  171.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  172.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  173.                     if ($activateFlg) {
  174.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  175.                         // メール送信
  176.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  177.                         if ($event->hasResponse()) {
  178.                             return $event->getResponse();
  179.                         }
  180.                         log_info('仮会員登録完了画面へリダイレクト');
  181.                         return $this->redirectToRoute('entry_complete');
  182.                     } else {
  183.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  184.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  185.                         // URLを変更するため完了画面にリダイレクト
  186.                         return $this->redirectToRoute('entry_activate', [
  187.                             'secret_key' => $Customer->getSecretKey(),
  188.                             'qtyInCart' => $qtyInCart,
  189.                         ]);
  190.                     }
  191.             }
  192.         }
  193.         return [
  194.             'form' => $form->createView(),
  195.             'cartTotalQuantity' => $cartTotalQuantity,
  196.             'cartTotalPrice' => $cartTotalPrice,
  197.             'sortedChildren' => $sortedChildren,
  198.         ];
  199.     }
  200.     /**
  201.      * 会員登録完了画面.
  202.      *
  203.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  204.      * @Template("Entry/complete.twig")
  205.      */
  206.     public function complete()
  207.     {
  208.         // カートサービスを使用してカート情報を取得
  209.         $cart $this->cartService->getCart();
  210.         // 数量と合計金額を取得
  211.         $cartTotalQuantity 0;
  212.         $cartTotalPrice 0;
  213.         if(!empty($cart)){
  214.             $cartTotalQuantity $cart->getTotalQuantity();
  215.             $cartTotalPrice $cart->getTotalPrice();
  216.         }
  217.         // メニュー用カテゴリー一覧
  218.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  219.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  220.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  221.             return $a->getSortNo() <=> $b->getSortNo();
  222.         });
  223.         return [
  224.             'cartTotalQuantity' => $cartTotalQuantity,
  225.             'cartTotalPrice' => $cartTotalPrice,
  226.             'sortedChildren' => $sortedChildren,
  227.         ];
  228.     }
  229.     /**
  230.      * 会員のアクティベート(本会員化)を行う.
  231.      *
  232.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  233.      * @Template("Entry/activate.twig")
  234.      */
  235.     public function activate(Request $request$secret_key$qtyInCart null)
  236.     {
  237.         $errors $this->recursiveValidator->validate(
  238.             $secret_key,
  239.             [
  240.                 new Assert\NotBlank(),
  241.                 new Assert\Regex(
  242.                     [
  243.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  244.                     ]
  245.                 ),
  246.             ]
  247.         );
  248.         if (!$this->session->has('eccube.login.target.path')) {
  249.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  250.         }
  251.         // 数量と合計金額を取得
  252.         $cartTotalQuantity 0;
  253.         $cartTotalPrice 0;
  254.         if(!empty($cart)){
  255.             $cartTotalQuantity $cart->getTotalQuantity();
  256.             $cartTotalPrice $cart->getTotalPrice();
  257.         }
  258.         // メニュー用カテゴリー一覧
  259.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  260.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  261.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  262.             return $a->getSortNo() <=> $b->getSortNo();
  263.         });
  264.         if (!is_null($qtyInCart)) {
  265.             return [
  266.                 'qtyInCart' => $qtyInCart,
  267.                 'cartTotalQuantity' => $cartTotalQuantity,
  268.                 'cartTotalPrice' => $cartTotalPrice,
  269.                 'sortedChildren' => $sortedChildren,
  270.             ];
  271.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  272.             // 会員登録処理を行う
  273.             $qtyInCart $this->entryActivate($request$secret_key);
  274.             return [
  275.                 'qtyInCart' => $qtyInCart,
  276.                 'cartTotalQuantity' => $cartTotalQuantity,
  277.                 'cartTotalPrice' => $cartTotalPrice,
  278.                 'sortedChildren' => $sortedChildren,
  279.             ];
  280.         }
  281.         throw new HttpException\NotFoundHttpException();
  282.     }
  283.     /**
  284.      * 会員登録処理を行う
  285.      *
  286.      * @param Request $request
  287.      * @param $secret_key
  288.      *
  289.      * @return \Eccube\Entity\Cart|mixed
  290.      */
  291.     private function entryActivate(Request $request$secret_key)
  292.     {
  293.         log_info('本会員登録開始');
  294.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  295.         if (is_null($Customer)) {
  296.             throw new HttpException\NotFoundHttpException();
  297.         }
  298.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  299.         $Customer->setStatus($CustomerStatus);
  300.         $this->entityManager->persist($Customer);
  301.         $this->entityManager->flush();
  302.         log_info('本会員登録完了');
  303.         $event = new EventArgs(
  304.             [
  305.                 'Customer' => $Customer,
  306.             ],
  307.             $request
  308.         );
  309.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  310.         // メール送信
  311.         $this->mailService->sendCustomerCompleteMail($Customer);
  312.         // Assign session carts into customer carts
  313.         $Carts $this->cartService->getCarts();
  314.         $qtyInCart 0;
  315.         foreach ($Carts as $Cart) {
  316.             $qtyInCart += $Cart->getTotalQuantity();
  317.         }
  318.         // 本会員登録してログイン状態にする
  319.         $token = new UsernamePasswordToken($Customer'customer', ['ROLE_USER']);
  320.         $this->tokenStorage->setToken($token);
  321.         $request->getSession()->migrate(true);
  322.         if ($qtyInCart) {
  323.             $this->cartService->save();
  324.         }
  325.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  326.         return $qtyInCart;
  327.     }
  328. }