src/Eccube/Form/Type/AddCartType.php line 70

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;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Eccube\Common\EccubeConfig;
  16. use Eccube\Entity\CartItem;
  17. use Eccube\Entity\ProductClass;
  18. use Eccube\Form\DataTransformer\EntityToIdTransformer;
  19. use Eccube\Repository\ProductClassRepository;
  20. use Symfony\Component\Form\AbstractType;
  21. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  22. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  23. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  24. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  25. use Symfony\Component\Form\FormBuilderInterface;
  26. use Symfony\Component\Form\FormEvent;
  27. use Symfony\Component\Form\FormEvents;
  28. use Symfony\Component\Form\FormInterface;
  29. use Symfony\Component\Form\FormView;
  30. use Symfony\Component\OptionsResolver\OptionsResolver;
  31. use Symfony\Component\Validator\Constraints as Assert;
  32. use Symfony\Component\Validator\Context\ExecutionContext;
  33. class AddCartType extends AbstractType
  34. {
  35.     /**
  36.      * @var EccubeConfig
  37.      */
  38.     protected $config;
  39.     /**
  40.      * @var EntityManager
  41.      */
  42.     protected $em;
  43.     /**
  44.      * @var \Eccube\Entity\Product
  45.      */
  46.     protected $Product null;
  47.     /**
  48.      * @var ProductClassRepository
  49.      */
  50.     protected $productClassRepository;
  51.     protected $doctrine;
  52.     public function __construct(ManagerRegistry $doctrineEccubeConfig $config)
  53.     {
  54.         $this->doctrine $doctrine;
  55.         $this->config $config;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function buildForm(FormBuilderInterface $builder, array $options)
  61.     {
  62.         /* @var $Product \Eccube\Entity\Product */
  63.         $Product $options['product'];
  64.         $this->Product $Product;
  65.         $ProductClasses $Product->getProductClasses();
  66.         $builder
  67.             ->add('product_id'HiddenType::class, [
  68.                 'data' => $Product->getId(),
  69.                 'mapped' => false,
  70.                 'constraints' => [
  71.                     new Assert\NotBlank(),
  72.                     new Assert\Regex(['pattern' => '/^\d+$/']),
  73.                 ], ])
  74.             ->add(
  75.                 $builder
  76.                     ->create('ProductClass'HiddenType::class, [
  77.                         'data_class' => null,
  78.                         'data' => $Product->hasProductClass() ? null $ProductClasses->first(),
  79.                         'constraints' => [
  80.                             new Assert\NotBlank(),
  81.                         ],
  82.                     ])
  83.                     ->addModelTransformer(new EntityToIdTransformer($this->doctrine->getManager(), ProductClass::class))
  84.             );
  85.         if ($Product->getStockFind()) {
  86.             $builder
  87.                 ->add('quantity'IntegerType::class, [
  88.                     'data' => 1,
  89.                     'attr' => [
  90.                         'min' => 1,
  91.                         'maxlength' => $this->config['eccube_int_len'],
  92.                     ],
  93.                     'constraints' => [
  94.                         new Assert\NotBlank(),
  95.                         new Assert\GreaterThanOrEqual([
  96.                             'value' => 1,
  97.                         ]),
  98.                         new Assert\Regex(['pattern' => '/^\d+$/']),
  99.                     ],
  100.                 ]);
  101.             if ($Product && $Product->getProductClasses()) {
  102.                 if (!is_null($Product->getClassName1())) {
  103.                     $builder->add('classcategory_id1'ChoiceType::class, [
  104.                         'label' => $Product->getClassName1(),
  105.                         'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories1AsFlip(),
  106.                         'mapped' => false,
  107.                     ]);
  108.                 }
  109.                 if (!is_null($Product->getClassName2())) {
  110.                     $builder->add('classcategory_id2'ChoiceType::class, [
  111.                         'label' => $Product->getClassName2(),
  112.                         'choices' => ['common.select' => '__unselected'],
  113.                         'mapped' => false,
  114.                     ]);
  115.                 }
  116.             }
  117.             $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) {
  118.                 $data $event->getData();
  119.                 $form $event->getForm();
  120.                 if (isset($data['classcategory_id1']) && !is_null($Product->getClassName2())) {
  121.                     if ($data['classcategory_id1']) {
  122.                         $form->add('classcategory_id2'ChoiceType::class, [
  123.                             'label' => $Product->getClassName2(),
  124.                             'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories2AsFlip($data['classcategory_id1']),
  125.                             'mapped' => false,
  126.                         ]);
  127.                     }
  128.                 }
  129.             });
  130.             $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  131.                 /** @var CartItem $CartItem */
  132.                 $CartItem $event->getData();
  133.                 $ProductClass $CartItem->getProductClass();
  134.                 // FIXME 価格の設定箇所、ここでいいのか
  135.                 if ($ProductClass) {
  136.                     $CartItem
  137.                         ->setProductClass($ProductClass)
  138.                         ->setPrice($ProductClass->getPrice02IncTax());
  139.                 }
  140.             });
  141.         }
  142.         // Thêm field user_imei nếu sản phẩm có category IMEI
  143.         if ($Product && $Product->hasUserImeiCategory()) {
  144.             $builder->add('user_imei'TextareaType::class, [
  145.                 'required' => true,
  146.                 'mapped' => true,
  147.                 'constraints' => [
  148.                     new Assert\NotBlank([
  149.                         'message' => 'front.cart.user_imei_required',
  150.                     ]),
  151.                     // new Assert\Regex([
  152.                     //     'pattern' => '/^\d{15}$/',
  153.                     //     'message' => 'IMEI番号は15桁の数字で入力してください。',
  154.                     // ]),
  155.                 ],
  156.                 'attr' => [
  157.                     'placeholder' => 'front.cart.user_imei_placeholder',
  158.                     'maxlength' => 15,
  159.                 ],
  160.             ]);
  161.         }
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public function configureOptions(OptionsResolver $resolver)
  167.     {
  168.         $resolver->setRequired('product');
  169.         $resolver->setDefaults([
  170.             'data_class' => CartItem::class,
  171.             'id_add_product_id' => true,
  172.             'constraints' => [
  173.                 // FIXME new Assert\Callback(array($this, 'validate')),
  174.             ],
  175.         ]);
  176.     }
  177.     /*
  178.      * {@inheritdoc}
  179.      */
  180.     public function finishView(FormView $viewFormInterface $form, array $options)
  181.     {
  182.         if ($options['id_add_product_id']) {
  183.             foreach ($view->vars['form']->children as $child) {
  184.                 $child->vars['id'] .= $options['product']->getId();
  185.             }
  186.         }
  187.     }
  188.     /**
  189.      * {@inheritdoc}
  190.      */
  191.     public function getBlockPrefix()
  192.     {
  193.         return 'add_cart';
  194.     }
  195.     /**
  196.      * validate
  197.      *
  198.      * @param type $data
  199.      * @param ExecutionContext $context
  200.      */
  201.     public function validate($dataExecutionContext $context)
  202.     {
  203.         $context->getValidator()->validate($data['product_class_id'], [
  204.             new Assert\NotBlank(),
  205.         ], '[product_class_id]');
  206.         if ($this->Product->getClassName1()) {
  207.             $context->getValidator()->validate($data['classcategory_id1'], [
  208.                 new Assert\NotBlank(),
  209.                 new Assert\NotEqualTo([
  210.                     'value' => '__unselected',
  211.                     'message' => 'form_error.not_selected',
  212.                 ]),
  213.             ], '[classcategory_id1]');
  214.         }
  215.         // 商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
  216.         if ($this->Product->getClassName2()) {
  217.             $context->getValidator()->validate($data['classcategory_id2'], [
  218.                 new Assert\NotBlank(),
  219.                 new Assert\NotEqualTo([
  220.                     'value' => '__unselected',
  221.                     'message' => 'form_error.not_selected',
  222.                 ]),
  223.             ], '[classcategory_id2]');
  224.         }
  225.     }
  226. }