app/Customize/Form/Type/AddCartType.php line 37

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 Customize\Form\Type;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Eccube\Common\EccubeConfig;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Eccube\Entity\CartItem;
  18. use Eccube\Entity\ProductClass;
  19. use Eccube\Form\DataTransformer\EntityToIdTransformer;
  20. use Eccube\Repository\ProductClassRepository;
  21. use Symfony\Component\Form\AbstractType;
  22. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  23. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  24. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  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->add('mitsumori_json'HiddenType::class, [
  67.                 'required' => false,
  68.                 'constraints' => [
  69.                     new Assert\Length(['max' => $this->config['eccube_ltext_len']]),
  70.                 ],
  71.             ])
  72.             ->add('product_id'HiddenType::class, [
  73.                 'data' => $Product->getId(),
  74.                 'mapped' => false,
  75.                 'constraints' => [
  76.                     new Assert\NotBlank(),
  77.                     new Assert\Regex(['pattern' => '/^\d+$/']),
  78.                 ], ])
  79.             ->add(
  80.                 $builder
  81.                     ->create('ProductClass'HiddenType::class, [
  82.                         'data_class' => null,
  83.                         'data' => $Product->hasProductClass() ? null $ProductClasses->first(),
  84.                         'constraints' => [
  85.                             new Assert\NotBlank(),
  86.                         ],
  87.                     ])
  88.                     ->addModelTransformer(new EntityToIdTransformer($this->doctrine->getManager(), ProductClass::class))
  89.             );
  90.         if ($Product->getStockFind()) {
  91.             $builder
  92.                 ->add('quantity'IntegerType::class, [
  93.                     'data' => 1,
  94.                     'attr' => [
  95.                         'min' => 1,
  96.                         'maxlength' => $this->config['eccube_int_len'],
  97.                     ],
  98.                     'constraints' => [
  99.                         new Assert\NotBlank(),
  100.                         new Assert\GreaterThanOrEqual([
  101.                             'value' => 1,
  102.                         ]),
  103.                         new Assert\Regex(['pattern' => '/^\d+$/']),
  104.                     ],
  105.                 ]);
  106.             if ($Product && $Product->getProductClasses()) {
  107.                 if (!is_null($Product->getClassName1())) {
  108.                     $builder->add('classcategory_id1'ChoiceType::class, [
  109.                         'label' => $Product->getClassName1(),
  110.                         'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories1AsFlip(),
  111.                         'mapped' => false,
  112.                     ]);
  113.                 }
  114.                 if (!is_null($Product->getClassName2())) {
  115.                     $builder->add('classcategory_id2'ChoiceType::class, [
  116.                         'label' => $Product->getClassName2(),
  117.                         'choices' => ['common.select' => '__unselected'],
  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.                         $form->add('classcategory_id2'ChoiceType::class, [
  128.                             'label' => $Product->getClassName2(),
  129.                             'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories2AsFlip($data['classcategory_id1']),
  130.                             'mapped' => false,
  131.                         ]);
  132.                     }
  133.                 }
  134.             });
  135.             $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  136.                 /** @var CartItem $CartItem */
  137.                 $CartItem $event->getData();
  138.                 $ProductClass $CartItem->getProductClass();
  139.                 // FIXME 価格の設定箇所、ここでいいのか
  140.                 if ($ProductClass) {
  141.                     $CartItem
  142.                         ->setProductClass($ProductClass)
  143.                         ->setPrice($ProductClass->getPrice02IncTax());
  144.                 }
  145.             });
  146.         }
  147.     }
  148.     /**
  149.      * {@inheritdoc}
  150.      */
  151.     public function configureOptions(OptionsResolver $resolver)
  152.     {
  153.         $resolver->setRequired('product');
  154.         $resolver->setDefaults([
  155.             'data_class' => CartItem::class,
  156.             'id_add_product_id' => true,
  157.             'constraints' => [
  158.                 // FIXME new Assert\Callback(array($this, 'validate')),
  159.             ],
  160.         ]);
  161.     }
  162.     /*
  163.      * {@inheritdoc}
  164.      */
  165.     public function finishView(FormView $viewFormInterface $form, array $options)
  166.     {
  167.         if ($options['id_add_product_id']) {
  168.             foreach ($view->vars['form']->children as $child) {
  169.                 $child->vars['id'] .= $options['product']->getId();
  170.             }
  171.         }
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function getBlockPrefix()
  177.     {
  178.         return 'add_cart';
  179.     }
  180.     /**
  181.      * validate
  182.      *
  183.      * @param type $data
  184.      * @param ExecutionContext $context
  185.      */
  186.     public function validate($dataExecutionContext $context)
  187.     {
  188.         $context->getValidator()->validate($data['product_class_id'], [
  189.             new Assert\NotBlank(),
  190.         ], '[product_class_id]');
  191.         if ($this->Product->getClassName1()) {
  192.             $context->validateValue($data['classcategory_id1'], [
  193.                 new Assert\NotBlank(),
  194.                 new Assert\NotEqualTo([
  195.                     'value' => '__unselected',
  196.                     'message' => 'form_error.not_selected',
  197.                 ]),
  198.             ], '[classcategory_id1]');
  199.         }
  200.         // 商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
  201.         if ($this->Product->getClassName2()) {
  202.             $context->getValidator()->validate($data['classcategory_id2'], [
  203.                 new Assert\NotBlank(),
  204.                 new Assert\NotEqualTo([
  205.                     'value' => '__unselected',
  206.                     'message' => 'form_error.not_selected',
  207.                 ]),
  208.             ], '[classcategory_id2]');
  209.         }
  210.     }
  211. }