src/Eccube/Form/Type/Admin/CustomerType.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\Admin;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Form\Type\AddressType;
  16. use Eccube\Form\Type\KanaType;
  17. use Eccube\Form\Type\Master\CustomerStatusType;
  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\RepeatedPasswordType;
  24. use Eccube\Form\Validator\Email;
  25. use Symfony\Component\Form\AbstractType;
  26. use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
  27. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  28. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  29. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  30. use Symfony\Component\Form\Extension\Core\Type\TextType;
  31. use Symfony\Component\Form\FormBuilderInterface;
  32. use Symfony\Component\Form\FormError;
  33. use Symfony\Component\Form\FormEvent;
  34. use Symfony\Component\Form\FormEvents;
  35. use Symfony\Component\OptionsResolver\OptionsResolver;
  36. use Symfony\Component\Validator\Constraints as Assert;
  37. class CustomerType extends AbstractType
  38. {
  39.     /**
  40.      * @var EccubeConfig
  41.      */
  42.     protected $eccubeConfig;
  43.     /**
  44.      * CustomerType constructor.
  45.      *
  46.      * @param EccubeConfig $eccubeConfig
  47.      */
  48.     public function __construct(EccubeConfig $eccubeConfig)
  49.     {
  50.         $this->eccubeConfig $eccubeConfig;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function buildForm(FormBuilderInterface $builder, array $options)
  56.     {
  57.         $builder
  58.             ->add('name'NameType::class, [
  59.                 'required' => true,
  60.             ])
  61.             ->add('kana'KanaType::class, [
  62.                 'required' => false,
  63.             ])
  64.             ->add('company_name'TextType::class, [
  65.                 'required' => false,
  66.                 'constraints' => [
  67.                     new Assert\Length([
  68.                         'max' => $this->eccubeConfig['eccube_stext_len'],
  69.                     ]),
  70.                 ],
  71.             ])
  72.             ->add('postal_code'PostalType::class, [
  73.                 'required' => true,
  74.             ])
  75.             ->add('address'AddressType::class, [
  76.                 'required' => true,
  77.             ])
  78.             ->add('phone_number'PhoneNumberType::class, [
  79.                 'required' => true,
  80.             ])
  81.             ->add('email'EmailType::class, [
  82.                 'required' => true,
  83.                 'constraints' => [
  84.                     new Assert\NotBlank(),
  85.                     new Email(nullnull$this->eccubeConfig['eccube_rfc_email_check'] ? 'strict' null),
  86.                     new Assert\Length([
  87.                         'max' => $this->eccubeConfig['eccube_email_len'],
  88.                     ]),
  89.                 ],
  90.                 'attr' => [
  91.                     'placeholder' => 'common.mail_address_sample',
  92.                 ],
  93.             ])
  94.             ->add('sex'SexType::class, [
  95.                 'required' => false,
  96.             ])
  97.             ->add('job'JobType::class, [
  98.                 'required' => false,
  99.             ])
  100.             ->add('birth'BirthdayType::class, [
  101.                 'required' => true,
  102.                 'input' => 'datetime',
  103.                 'years' => range(date('Y'), date('Y') - $this->eccubeConfig['eccube_birth_max']),
  104.                 'widget' => 'single_text',
  105.                 'placeholder' => ['year' => '----''month' => '--''day' => '--'],
  106.                 'constraints' => [
  107.                     new Assert\NotBlank(),
  108.                     new Assert\LessThanOrEqual([
  109.                         'value' => date('Y-m-d'strtotime('-1 day')),
  110.                         'message' => 'form_error.select_is_future_or_now_date',
  111.                     ]),
  112.                     new Assert\Range([
  113.                         'min'=> '0003-01-01',
  114.                         'minMessage' => 'form_error.out_of_range',
  115.                     ]),
  116.                 ],
  117.             ])
  118.             ->add('plain_password'RepeatedPasswordType::class)
  119.             ->add('status'CustomerStatusType::class, [
  120.                 'required' => true,
  121.                 'constraints' => [
  122.                     new Assert\NotBlank(),
  123.                 ],
  124.             ])
  125.             ->add(
  126.                 'point',
  127.                 NumberType::class,
  128.                 [
  129.                     'required' => false,
  130.                     'constraints' => [
  131.                         new Assert\NotBlank(),
  132.                         new Assert\Range([
  133.                             'min' => "-".$this->eccubeConfig['eccube_price_max'],
  134.                             'max' => $this->eccubeConfig['eccube_price_max']])
  135.                     ],
  136.                 ]
  137.             )
  138.             ->add('note'TextareaType::class, [
  139.                 'required' => false,
  140.                 'constraints' => [
  141.                     new Assert\Length([
  142.                         'max' => $this->eccubeConfig['eccube_ltext_len'],
  143.                     ]),
  144.                 ],
  145.             ]);
  146.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  147.             $form $event->getForm();
  148.             /** @var Customer $Customer */
  149.             $Customer $event->getData();
  150.             if ($Customer->getPlainPassword() != '' && $Customer->getPlainPassword() == $Customer->getEmail()) {
  151.                 $form['plain_password']['first']->addError(new FormError(trans('common.password_eq_email')));
  152.             }
  153.         });
  154.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  155.             $Customer $event->getData();
  156.             // ポイント数が入力されていない場合0を登録
  157.             if (is_null($Customer->getPoint())) {
  158.                 $Customer->setPoint(0);
  159.             }
  160.         });
  161.     }
  162.     /**
  163.      * {@inheritdoc}
  164.      */
  165.     public function configureOptions(OptionsResolver $resolver)
  166.     {
  167.         $resolver->setDefaults([
  168.             'data_class' => 'Eccube\Entity\Customer',
  169.         ]);
  170.     }
  171.     /**
  172.      * {@inheritdoc}
  173.      */
  174.     public function getBlockPrefix()
  175.     {
  176.         return 'admin_customer';
  177.     }
  178. }