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

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