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