app/Customize/Controller/NonMemberShoppingController.php line 32

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\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Form\Type\Front\NonMemberType;
  16. use Eccube\Form\Validator\Email;
  17. use Eccube\Repository\Master\PrefRepository;
  18. use Eccube\Service\CartService;
  19. use Eccube\Service\OrderHelper;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Validator\Constraints as Assert;
  24. use Symfony\Component\Validator\Validator\ValidatorInterface;
  25. use Eccube\Controller\AbstractShoppingController;
  26. use Eccube\Repository\PageRepository;
  27. class NonMemberShoppingController extends AbstractShoppingController
  28. {
  29.     /**
  30.      * @var ValidatorInterface
  31.      */
  32.     protected $validator;
  33.     /**
  34.      * @var PrefRepository
  35.      */
  36.     protected $prefRepository;
  37.     /**
  38.      * @var PageRepository
  39.      */
  40.     private $pageRepository;
  41.     /**
  42.      * @var OrderHelper
  43.      */
  44.     protected $orderHelper;
  45.     /**
  46.      * @var CartService
  47.      */
  48.     protected $cartService;
  49.     /**
  50.      * NonMemberShoppingController constructor.
  51.      *
  52.      * @param ValidatorInterface $validator
  53.      * @param PrefRepository $prefRepository
  54.      * @param PageRepository $pageRepository
  55.      * @param OrderHelper $orderHelper
  56.      * @param CartService $cartService
  57.      */
  58.     public function __construct(
  59.         ValidatorInterface $validator,
  60.         PrefRepository $prefRepository,
  61.         PageRepository $pageRepository,
  62.         OrderHelper $orderHelper,
  63.         CartService $cartService
  64.     ) {
  65.         $this->validator $validator;
  66.         $this->prefRepository $prefRepository;
  67.         $this->pageRepository $pageRepository;
  68.         $this->orderHelper $orderHelper;
  69.         $this->cartService $cartService;
  70.     }
  71.     /**
  72.      * 非会員処理
  73.      *
  74.      * @Route("/shopping/nonmember", name="shopping_nonmember", methods={"GET", "POST"})
  75.      * @Route("/shopping/nonmember/confirm", name="shopping_nonmember_confirm", methods={"GET", "POST"})
  76.      * @Template("Shopping/nonmember.twig")
  77.      */
  78.     public function index(Request $request)
  79.     {
  80.         // ログイン済みの場合は, 購入画面へリダイレクト.
  81.         // if ($this->isGranted('ROLE_USER')) {
  82.         //     return $this->redirectToRoute('shopping');
  83.         // }
  84.         // カートチェック.
  85.         $Cart $this->cartService->getCart();
  86.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  87.             return $this->redirectToRoute('cart');
  88.         }
  89.         $Customer $this->getUser() ? $this->getUser() : $this->orderHelper->getNonMember();
  90.         if ($Customer) {
  91.             $Customer->setPlainPassword('123qweasd');
  92.         }
  93.         $builder $this->formFactory->createBuilder(NonMemberType::class, $Customer);
  94.         $event = new EventArgs(
  95.             [
  96.                 'builder' => $builder,
  97.             ],
  98.             $request
  99.         );
  100.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_INITIALIZE);
  101.         $form $builder->getForm();
  102.         $form->handleRequest($request);
  103.         if ($form->isSubmitted() && $form->isValid()) {
  104.             switch ($request->get('mode')) {
  105.                 case 'confirm':
  106.                     return $this->render('Shopping/nonmember_confirm.twig', [
  107.                         'form' => $form->createView(),
  108.                         'Page' => $this->pageRepository->getPageByRoute('shopping_nonmember_confirm'),
  109.                     ]);
  110.                 case 'complete':
  111.                     log_info('非会員お客様情報登録開始');
  112.         
  113.                     $data $form->getData();
  114.         
  115.                     // 非会員用セッションを作成
  116.                     $this->session->set(OrderHelper::SESSION_NON_MEMBER$data);
  117.                     $this->session->set(OrderHelper::SESSION_NON_MEMBER_ADDRESSESserialize([]));
  118.         
  119.                     $event = new EventArgs(
  120.                         [
  121.                             'form' => $form,
  122.                         ],
  123.                         $request
  124.                     );
  125.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE);
  126.         
  127.                     if ($event->getResponse() !== null) {
  128.                         return $event->getResponse();
  129.                     }
  130.         
  131.                     log_info('非会員お客様情報登録完了');
  132.         
  133.                     return $this->redirectToRoute('shopping');
  134.             }
  135.         }
  136.         return [
  137.             'form' => $form->createView(),
  138.         ];
  139.     }
  140.     /**
  141.      * お客様情報の変更(非会員)
  142.      *
  143.      * @Route("/shopping/customer", name="shopping_customer", methods={"POST"})
  144.      */
  145.     public function customer(Request $request)
  146.     {
  147.         if (!$request->isXmlHttpRequest()) {
  148.             return $this->json(['status' => 'NG'], 400);
  149.         }
  150.         $this->isTokenValid();
  151.         try {
  152.             log_info('非会員お客様情報変更処理開始');
  153.             $data $request->request->all();
  154.             // 入力チェック
  155.             $errors $this->customerValidation($data);
  156.             foreach ($errors as $error) {
  157.                 if ($error->count() != 0) {
  158.                     log_info('非会員お客様情報変更入力チェックエラー');
  159.                     return $this->json(['status' => 'NG'], 400);
  160.                 }
  161.             }
  162.             $pref $this->prefRepository->findOneBy(['name' => $data['customer_pref']]);
  163.             if (!$pref) {
  164.                 log_info('非会員お客様情報変更入力チェックエラー');
  165.                 return $this->json(['status' => 'NG'], 400);
  166.             }
  167.             $preOrderId $this->cartService->getPreOrderId();
  168.             $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  169.             if (!$Order) {
  170.                 log_info('受注が存在しません');
  171.                 $this->addError('front.shopping.order_error');
  172.                 return $this->redirectToRoute('shopping_error');
  173.             }
  174.             $Order
  175.                 ->setName01($data['customer_name01'])
  176.                 ->setName02($data['customer_name02'])
  177.                 ->setKana01($data['customer_kana01'])
  178.                 ->setKana02($data['customer_kana02'])
  179.                 ->setCompanyName($data['customer_company_name'])
  180.                 ->setPhoneNumber($data['customer_phone_number'])
  181.                 ->setPostalCode($data['customer_postal_code'])
  182.                 ->setPref($pref)
  183.                 ->setAddr01($data['customer_addr01'])
  184.                 ->setAddr02($data['customer_addr02'])
  185.                 ->setEmail($data['customer_email']);
  186.             $this->entityManager->flush();
  187.             $this->session->set(OrderHelper::SESSION_NON_MEMBER, [
  188.                 'name01' => $data['customer_name01'],
  189.                 'name02' => $data['customer_name02'],
  190.                 'kana01' => $data['customer_kana01'],
  191.                 'kana02' => $data['customer_kana02'],
  192.                 'company_name' => $data['customer_company_name'],
  193.                 'phone_number' => $data['customer_phone_number'],
  194.                 'postal_code' => $data['customer_postal_code'],
  195.                 'pref' => $pref,
  196.                 'addr01' => $data['customer_addr01'],
  197.                 'addr02' => $data['customer_addr02'],
  198.                 'email' => $data['customer_email'],
  199.             ]);
  200.             $event = new EventArgs(
  201.                 [
  202.                     'Order' => $Order,
  203.                     'data' => $data,
  204.                 ],
  205.                 $request
  206.             );
  207.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_CUSTOMER_INITIALIZE);
  208.             log_info('非会員お客様情報変更処理完了', [$Order->getId()]);
  209.             $message = ['status' => 'OK''kana01' => $data['customer_kana01'], 'kana02' => $data['customer_kana02']];
  210.             $response $this->json($message);
  211.         } catch (\Exception $e) {
  212.             log_error('予期しないエラー', [$e->getMessage()]);
  213.             $response $this->json(['status' => 'NG'], 500);
  214.         }
  215.         return $response;
  216.     }
  217.     /**
  218.      * 非会員でのお客様情報変更時の入力チェック
  219.      *
  220.      * @param array $data リクエストパラメータ
  221.      *
  222.      * @return \Symfony\Component\Validator\ConstraintViolationListInterface[]
  223.      */
  224.     protected function customerValidation(array &$data)
  225.     {
  226.         // 入力チェック
  227.         $errors = [];
  228.         $errors[] = $this->validator->validate(
  229.             $data['customer_name01'],
  230.             [
  231.                 new Assert\NotBlank(),
  232.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  233.                 new Assert\Regex(
  234.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  235.                 ),
  236.             ]
  237.         );
  238.         $errors[] = $this->validator->validate(
  239.             $data['customer_name02'],
  240.             [
  241.                 new Assert\NotBlank(),
  242.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  243.                 new Assert\Regex(
  244.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  245.                 ),
  246.             ]
  247.         );
  248.         $data['customer_kana01'] = mb_convert_kana($data['customer_kana01'], 'CV''utf-8');
  249.         $errors[] = $this->validator->validate(
  250.             $data['customer_kana01'],
  251.             [
  252.                 new Assert\NotBlank(),
  253.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  254.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  255.             ]
  256.         );
  257.         $data['customer_kana02'] = mb_convert_kana($data['customer_kana02'], 'CV''utf-8');
  258.         $errors[] = $this->validator->validate(
  259.             $data['customer_kana02'],
  260.             [
  261.                 new Assert\NotBlank(),
  262.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  263.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  264.             ]);
  265.         $errors[] = $this->validator->validate(
  266.             $data['customer_company_name'],
  267.             [
  268.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
  269.             ]
  270.         );
  271.         $errors[] = $this->validator->validate(
  272.             $data['customer_phone_number'],
  273.             [
  274.                 new Assert\NotBlank(),
  275.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  276.                 new Assert\Length(
  277.                     ['max' => $this->eccubeConfig['eccube_tel_len_max']]
  278.                 ),
  279.             ]
  280.         );
  281.         $errors[] = $this->validator->validate(
  282.             $data['customer_postal_code'],
  283.             [
  284.                 new Assert\NotBlank(),
  285.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  286.                 new Assert\Length(
  287.                     ['max' => $this->eccubeConfig['eccube_postal_code']]
  288.                 ),
  289.             ]
  290.         );
  291.         $errors[] = $this->validator->validate(
  292.             $data['customer_addr01'],
  293.             [
  294.                 new Assert\NotBlank(),
  295.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address1_len']]),
  296.             ]
  297.         );
  298.         $errors[] = $this->validator->validate(
  299.             $data['customer_addr02'],
  300.             [
  301.                 new Assert\NotBlank(),
  302.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address2_len']]),
  303.             ]
  304.         );
  305.         $errors[] = $this->validator->validate(
  306.             $data['customer_email'],
  307.             [
  308.                 new Assert\NotBlank(),
  309.                 new Email(nullnull$this->eccubeConfig['eccube_rfc_email_check'] ? 'strict' null),
  310.             ]
  311.         );
  312.         return $errors;
  313.     }
  314. }