src/Eccube/Controller/NonMemberShoppingController.php line 78

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