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('quantity'IntegerType::class, [
  87.                     'data' => 1,
  88.                     'attr' => [
  89.                         'min' => 1,
  90.                         'maxlength' => $this->config['eccube_int_len'],
  91.                     ],
  92.                     'constraints' => [
  93.                         new Assert\NotBlank(),
  94.                         new Assert\GreaterThanOrEqual([
  95.                             'value' => 1,
  96.                         ]),
  97.                         new Assert\Regex(['pattern' => '/^\d+$/']),
  98.                     ],
  99.                 ]);
  100.             if ($Product && $Product->getProductClasses()) {
  101.                 if (!is_null($Product->getClassName1())) {
  102.                     $builder->add('classcategory_id1'ChoiceType::class, [
  103.                         'label' => $Product->getClassName1(),
  104.                         'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories1AsFlip(),
  105.                         'mapped' => false,
  106.                     ]);
  107.                 }
  108.                 if (!is_null($Product->getClassName2())) {
  109.                     $builder->add('classcategory_id2'ChoiceType::class, [
  110.                         'label' => $Product->getClassName2(),
  111.                         'choices' => ['common.select' => '__unselected'],
  112.                         'mapped' => false,
  113.                     ]);
  114.                 }
  115.             }
  116.             $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) {
  117.                 $data $event->getData();
  118.                 $form $event->getForm();
  119.                 if (isset($data['classcategory_id1']) && !is_null($Product->getClassName2())) {
  120.                     if ($data['classcategory_id1']) {
  121.                         $form->add('classcategory_id2'ChoiceType::class, [
  122.                             'label' => $Product->getClassName2(),
  123.                             'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories2AsFlip($data['classcategory_id1']),
  124.                             'mapped' => false,
  125.                         ]);
  126.                     }
  127.                 }
  128.             });
  129.             $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  130.                 /** @var CartItem $CartItem */
  131.                 $CartItem $event->getData();
  132.                 $ProductClass $CartItem->getProductClass();
  133.                 // FIXME 価格の設定箇所、ここでいいのか
  134.                 if ($ProductClass) {
  135.                     $CartItem
  136.                         ->setProductClass($ProductClass)
  137.                         ->setPrice($ProductClass->getPrice02IncTax());
  138.                 }
  139.             });
  140.         }
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function configureOptions(OptionsResolver $resolver)
  146.     {
  147.         $resolver->setRequired('product');
  148.         $resolver->setDefaults([
  149.             'data_class' => CartItem::class,
  150.             'id_add_product_id' => true,
  151.             'constraints' => [
  152.                 // FIXME new Assert\Callback(array($this, 'validate')),
  153.             ],
  154.         ]);
  155.     }
  156.     /*
  157.      * {@inheritdoc}
  158.      */
  159.     public function finishView(FormView $viewFormInterface $form, array $options)
  160.     {
  161.         if ($options['id_add_product_id']) {
  162.             foreach ($view->vars['form']->children as $child) {
  163.                 $child->vars['id'] .= $options['product']->getId();
  164.             }
  165.         }
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      */
  170.     public function getBlockPrefix()
  171.     {
  172.         return 'add_cart';
  173.     }
  174.     /**
  175.      * validate
  176.      *
  177.      * @param type $data
  178.      * @param ExecutionContext $context
  179.      */
  180.     public function validate($dataExecutionContext $context)
  181.     {
  182.         $context->getValidator()->validate($data['product_class_id'], [
  183.             new Assert\NotBlank(),
  184.         ], '[product_class_id]');
  185.         if ($this->Product->getClassName1()) {
  186.             $context->validateValue($data['classcategory_id1'], [
  187.                 new Assert\NotBlank(),
  188.                 new Assert\NotEqualTo([
  189.                     'value' => '__unselected',
  190.                     'message' => 'form_error.not_selected',
  191.                 ]),
  192.             ], '[classcategory_id1]');
  193.         }
  194.         // 商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
  195.         if ($this->Product->getClassName2()) {
  196.             $context->getValidator()->validate($data['classcategory_id2'], [
  197.                 new Assert\NotBlank(),
  198.                 new Assert\NotEqualTo([
  199.                     'value' => '__unselected',
  200.                     'message' => 'form_error.not_selected',
  201.                 ]),
  202.             ], '[classcategory_id2]');
  203.         }
  204.     }
  205. }