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