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.             ->add(
  74.                 $builder
  75.                     ->create('ProductClass'HiddenType::class, [
  76.                         'data_class' => null,
  77.                         'data' => $Product->hasProductClass() ? null $ProductClasses->first(),
  78.                         'constraints' => [
  79.                             new Assert\NotBlank(),
  80.                         ],
  81.                     ])
  82.                     ->addModelTransformer(new EntityToIdTransformer($this->doctrine->getManager(), ProductClass::class))
  83.             );
  84.         if ($Product->getStockFind()) {
  85.             $builder
  86.                 ->add('maker_id'IntegerType::class)
  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.     }
  143.     /**
  144.      * {@inheritdoc}
  145.      */
  146.     public function configureOptions(OptionsResolver $resolver)
  147.     {
  148.         $resolver->setRequired('product');
  149.         $resolver->setDefaults([
  150.             'data_class' => CartItem::class,
  151.             'id_add_product_id' => true,
  152.             'constraints' => [
  153.                 // FIXME new Assert\Callback(array($this, 'validate')),
  154.             ],
  155.         ]);
  156.     }
  157.     /*
  158.      * {@inheritdoc}
  159.      */
  160.     public function finishView(FormView $viewFormInterface $form, array $options)
  161.     {
  162.         if ($options['id_add_product_id']) {
  163.             foreach ($view->vars['form']->children as $child) {
  164.                 $child->vars['id'] .= $options['product']->getId();
  165.             }
  166.         }
  167.     }
  168.     /**
  169.      * {@inheritdoc}
  170.      */
  171.     public function getBlockPrefix()
  172.     {
  173.         return 'add_cart';
  174.     }
  175.     /**
  176.      * validate
  177.      *
  178.      * @param type $data
  179.      * @param ExecutionContext $context
  180.      */
  181.     public function validate($dataExecutionContext $context)
  182.     {
  183.         $context->getValidator()->validate($data['product_class_id'], [
  184.             new Assert\NotBlank(),
  185.         ], '[product_class_id]');
  186.         if ($this->Product->getClassName1()) {
  187.             $context->validateValue($data['classcategory_id1'], [
  188.                 new Assert\NotBlank(),
  189.                 new Assert\NotEqualTo([
  190.                     'value' => '__unselected',
  191.                     'message' => 'form_error.not_selected',
  192.                 ]),
  193.             ], '[classcategory_id1]');
  194.         }
  195.         // 商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
  196.         if ($this->Product->getClassName2()) {
  197.             $context->getValidator()->validate($data['classcategory_id2'], [
  198.                 new Assert\NotBlank(),
  199.                 new Assert\NotEqualTo([
  200.                     'value' => '__unselected',
  201.                     'message' => 'form_error.not_selected',
  202.                 ]),
  203.             ], '[classcategory_id2]');
  204.         }
  205.     }
  206. }