src/Eccube/Form/Type/Front/EntryType.php line 41

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\Form\Type\Front;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Form\Type\AddressType;
  16. use Eccube\Form\Type\KanaType;
  17. use Customize\Form\Type\Front\HiddenKanaType;
  18. use Eccube\Form\Type\Master\JobType;
  19. use Eccube\Form\Type\Master\SexType;
  20. use Eccube\Form\Type\NameType;
  21. use Eccube\Form\Type\PhoneNumberType;
  22. use Eccube\Form\Type\PostalType;
  23. use Eccube\Form\Type\RepeatedEmailType;
  24. use Eccube\Form\Type\RepeatedPasswordType;
  25. use Symfony\Component\Form\AbstractType;
  26. use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
  27. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  28. use Symfony\Component\Form\Extension\Core\Type\TextType;
  29. use Symfony\Component\Form\FormBuilderInterface;
  30. use Symfony\Component\Form\FormError;
  31. use Symfony\Component\Form\FormEvent;
  32. use Symfony\Component\Form\FormEvents;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  35. use Symfony\Component\OptionsResolver\OptionsResolver;
  36. use Symfony\Component\Validator\Constraints as Assert;
  37. class EntryType extends AbstractType
  38. {
  39.     /**
  40.      * @var EccubeConfig
  41.      */
  42.     protected $eccubeConfig;
  43.     /**
  44.      * @var RequestStack
  45.      */
  46.     protected $requestStack;
  47.     /**
  48.      * @var SessionInterface
  49.      */
  50.     protected $session;
  51.     /**
  52.      * 
  53.      * @param EccubeConfig $eccubeConfig
  54.      * @param RequestStack $requestStack
  55.      * @param SessionInterface $session
  56.      *
  57.      */
  58.     public function __construct(
  59.         EccubeConfig $eccubeConfig,
  60.         RequestStack $requestStack,
  61.         SessionInterface $session
  62.     ) {
  63.         $this->eccubeConfig $eccubeConfig;
  64.         $this->requestStack $requestStack;
  65.         $this->session $session;
  66.     }
  67.     /**
  68.      * 現在の言語を取得
  69.      */
  70.     private function getCurrentLocale(): string
  71.     {
  72.         $request $this->requestStack->getCurrentRequest();
  73.         // 1. セッションから言語設定を取得
  74.         $locale $this->session->get('_locale');
  75.         // 2. セッションにない場合はリクエストパラメータから取得
  76.         if (!$locale && $request) {
  77.             $locale $request->get('_locale');
  78.         }
  79.         // 3. それでもない場合はリクエストのロケールを使用
  80.         if (!$locale && $request) {
  81.             $locale $request->getLocale();
  82.         }
  83.         // 4. デフォルトは日本語
  84.         if (!$locale) {
  85.             $locale 'ja';
  86.         }
  87.         // サポートされている言語かチェック
  88.         $supportedLocales = ['ja''en'];
  89.         if (!in_array($locale$supportedLocales)) {
  90.             $locale 'ja';
  91.         }
  92.         return $locale;
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function buildForm(FormBuilderInterface $builder, array $options)
  98.     {
  99.         // 現在のロケールを取得
  100.         $locale $this->getCurrentLocale();
  101.         $isEnglish $locale === 'en';
  102.         $builder
  103.             ->add('name'NameType::class, [
  104.                 'required' => true,
  105.             ]);
  106.         // 英語の場合は隠しフィールド、日本語の場合は通常のKanaType
  107.         if ($isEnglish) {
  108.             // 英語の場合:隠しフィールドとして追加(バリデーションなし)
  109.             $builder->add('kana'HiddenKanaType::class, [
  110.                 'mapped' => true,
  111.                 'required' => false,
  112.             ]);
  113.         } else {
  114.             // 日本語の場合:通常のKanaTypeを使用
  115.             $builder->add('kana'KanaType::class, []);
  116.         }
  117.         
  118.         $builder
  119.             ->add('company_name'TextType::class, [
  120.                 'required' => false,
  121.                 'constraints' => [
  122.                     new Assert\Length([
  123.                         'max' => $this->eccubeConfig['eccube_stext_len'],
  124.                     ]),
  125.                 ],
  126.             ])
  127.             ->add('postal_code'PostalType::class)
  128.             ->add('address'AddressType::class)
  129.             ->add('phone_number'PhoneNumberType::class, [
  130.                 'required' => true,
  131.             ])
  132.             ->add('email'RepeatedEmailType::class)
  133.             ->add('plain_password'RepeatedPasswordType::class)
  134.             ->add('birth'BirthdayType::class, [
  135.                 'required' => true,
  136.                 'input' => 'datetime',
  137.                 'years' => range(date('Y'), date('Y') - $this->eccubeConfig['eccube_birth_max']),
  138.                 'widget' => 'choice',
  139.                 'placeholder' => ['year' => '----''month' => '--''day' => '--'],
  140.                 'constraints' => [
  141.                     new Assert\LessThanOrEqual([
  142.                         'value' => date('Y-m-d'strtotime('-1 day')),
  143.                         'message' => 'form_error.select_is_future_or_now_date',
  144.                     ]),
  145.                     new Assert\NotBlank(['message' => 'form_error.enter_your_date_of_birth']),
  146.                 ],
  147.             ])
  148.             ->add('sex'SexType::class, [
  149.                 'required' => false,
  150.             ])
  151.             ->add('job'JobType::class, [
  152.                 'required' => false,
  153.             ])
  154.             ->add('agencyCode'TextType::class, [
  155.                 'required' => false,
  156.                 'label' => '代理店紹介コード',
  157.                 'attr' => ['placeholder' => 'form_error.place_agency_code'],
  158.             ])
  159.         ;
  160.         $builder->addEventListener(
  161.             FormEvents::PRE_SET_DATA,
  162.             function (FormEvent $event) {
  163.                 $Customer $event->getData();
  164.                 if ($Customer instanceof Customer && !$Customer->getId()) {
  165.                     $form $event->getForm();
  166.                     $form->add('user_policy_check'CheckboxType::class, [
  167.                         'required' => true,
  168.                         'label' => null,
  169.                         'mapped' => false,
  170.                         'constraints' => [
  171.                             new Assert\NotBlank(['message' => 'form_error.check_term']),
  172.                         ],
  173.                     ]);
  174.                 }
  175.             }
  176.         );
  177.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  178.             $form $event->getForm();
  179.             /** @var Customer $Customer */
  180.             $Customer $event->getData();
  181.             if ($Customer->getPlainPassword() != '' && $Customer->getPlainPassword() == $Customer->getEmail()) {
  182.                 $form['plain_password']['first']->addError(new FormError(trans('common.password_eq_email')));
  183.             }
  184.         });
  185.     }
  186.     /**
  187.      * {@inheritdoc}
  188.      */
  189.     public function configureOptions(OptionsResolver $resolver)
  190.     {
  191.         $resolver->setDefaults([
  192.             'data_class' => 'Eccube\Entity\Customer',
  193.         ]);
  194.     }
  195.     /**
  196.      * {@inheritdoc}
  197.      */
  198.     public function getBlockPrefix()
  199.     {
  200.         // todo entry,mypageで共有されているので名前を変更する
  201.         return 'entry';
  202.     }
  203. }