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' => true,
  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' => false,
  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\LessThanOrEqual([
  108.                         'value' => date('Y-m-d'strtotime('-1 day')),
  109.                         'message' => 'form_error.select_is_future_or_now_date',
  110.                     ]),
  111.                     new Assert\Range([
  112.                         'min'=> '0003-01-01',
  113.                         'minMessage' => 'form_error.out_of_range',
  114.                     ]),
  115.                 ],
  116.             ])
  117.             ->add('plain_password'RepeatedPasswordType::class)
  118.             ->add('status'CustomerStatusType::class, [
  119.                 'required' => true,
  120.                 'constraints' => [
  121.                     new Assert\NotBlank(),
  122.                 ],
  123.             ])
  124.             ->add(
  125.                 'point',
  126.                 NumberType::class,
  127.                 [
  128.                     'required' => false,
  129.                     'constraints' => [
  130.                         new Assert\NotBlank(),
  131.                         new Assert\Range([
  132.                             'min' => "-".$this->eccubeConfig['eccube_price_max'],
  133.                             'max' => $this->eccubeConfig['eccube_price_max']])
  134.                     ],
  135.                 ]
  136.             )
  137.             ->add('note'TextareaType::class, [
  138.                 'required' => false,
  139.                 'constraints' => [
  140.                     new Assert\Length([
  141.                         'max' => $this->eccubeConfig['eccube_ltext_len'],
  142.                     ]),
  143.                 ],
  144.             ]);
  145.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  146.             $form $event->getForm();
  147.             /** @var Customer $Customer */
  148.             $Customer $event->getData();
  149.             if ($Customer->getPlainPassword() != '' && $Customer->getPlainPassword() == $Customer->getEmail()) {
  150.                 $form['plain_password']['first']->addError(new FormError(trans('common.password_eq_email')));
  151.             }
  152.         });
  153.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  154.             $Customer $event->getData();
  155.             // ポイント数が入力されていない場合0を登録
  156.             if (is_null($Customer->getPoint())) {
  157.                 $Customer->setPoint(0);
  158.             }
  159.         });
  160.     }
  161.     /**
  162.      * {@inheritdoc}
  163.      */
  164.     public function configureOptions(OptionsResolver $resolver)
  165.     {
  166.         $resolver->setDefaults([
  167.             'data_class' => 'Eccube\Entity\Customer',
  168.         ]);
  169.     }
  170.     /**
  171.      * {@inheritdoc}
  172.      */
  173.     public function getBlockPrefix()
  174.     {
  175.         return 'admin_customer';
  176.     }
  177. }