src/Eccube/Form/Type/AddCartType.php line 64

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 Customize\Entity\Brother;
  14. use Customize\Repository\BrotherRepository;
  15. use Doctrine\ORM\EntityManager;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Eccube\Common\EccubeConfig;
  18. use Eccube\Entity\CartItem;
  19. use Eccube\Entity\ProductClass;
  20. use Eccube\Form\DataTransformer\EntityToIdTransformer;
  21. use Eccube\Repository\ProductClassRepository;
  22. use Symfony\Component\Form\AbstractType;
  23. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  24. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  25. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  26. use Symfony\Component\Form\FormBuilderInterface;
  27. use Symfony\Component\Form\FormEvent;
  28. use Symfony\Component\Form\FormEvents;
  29. use Symfony\Component\Form\FormInterface;
  30. use Symfony\Component\Form\FormView;
  31. use Symfony\Component\OptionsResolver\OptionsResolver;
  32. use Symfony\Component\Validator\Constraints as Assert;
  33. use Symfony\Component\Validator\Context\ExecutionContext;
  34. use Symfony\Component\Security\Core\Security;
  35. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  36. class AddCartType extends AbstractType
  37. {
  38.     /**
  39.      * @var EccubeConfig
  40.      */
  41.     protected $config;
  42.     /**
  43.      * @var EntityManager
  44.      */
  45.     protected $em;
  46.     /**
  47.      * @var \Eccube\Entity\Product
  48.      */
  49.     protected $Product null;
  50.     /**
  51.      * @var ProductClassRepository
  52.      */
  53.     protected $productClassRepository;
  54.     protected $doctrine;
  55.     public function __construct(ManagerRegistry $doctrineEccubeConfig $configSecurity $security)
  56.     {
  57.         $this->doctrine $doctrine;
  58.         $this->config $config;
  59.         $this->security $security;
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function buildForm(FormBuilderInterface $builder, array $options)
  65.     {
  66.         /* @var $Product \Eccube\Entity\Product */
  67.         $Product $options['product'];
  68.         $Order = isset($options['order']) ? $options['order'] : null;
  69.         $this->Product $Product;
  70.         $ProductClasses $Product->getProductClasses();
  71.         $builder
  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.         $Customer $Order $Order->getCustomer() : $this->security->getUser();
  148.         if (method_exists($Customer'getSchool') && $Customer->getSchool() && $Customer->getSchool()->isBrotherEnabled()) {
  149.             $builder->add('brother'EntityType::class, array(
  150.                 'required' => true,
  151.                 'class' => 'Customize\Entity\Brother',
  152.                 'expanded' => false,
  153.                 'multiple' => false,
  154.                 'placeholder' => '(購入する生徒)',
  155.                 'choice_label' => 'name',
  156.                 'constraints' => array(
  157.                     new Assert\NotBlank(),
  158.                 ),
  159.                 'query_builder' => function (BrotherRepository $br) use ($Customer$Product) {
  160.                     $Schools = [];
  161.                     foreach ($Product->getProductSchool() as $ps) {
  162.                         $Schools[] = $ps->getSchool();
  163.                     }
  164.                     return $br->createQueryBuilder('b')
  165.                         ->where('b.del_flg <> 1')
  166.                         ->andWhere('b.Customer = :Customer')
  167.                         ->andWhere('b.School In (:Schools)')
  168.                         ->setParameter('Customer'$Customer)
  169.                         ->setParameter('Schools'$Schools);
  170.                 },
  171.             ));
  172.         }
  173.     }
  174.     /**
  175.      * {@inheritdoc}
  176.      */
  177.     public function configureOptions(OptionsResolver $resolver)
  178.     {
  179.         $resolver->setRequired('product');
  180.         $resolver->setDefaults([
  181.             'data_class' => CartItem::class,
  182.             'id_add_product_id' => true,
  183.             'constraints' => [
  184.                 // FIXME new Assert\Callback(array($this, 'validate')),
  185.             ],
  186.         ]);
  187.         $resolver->setDefined('order');
  188.     }
  189.     /*
  190.      * {@inheritdoc}
  191.      */
  192.     public function finishView(FormView $viewFormInterface $form, array $options)
  193.     {
  194.         if ($options['id_add_product_id']) {
  195.             foreach ($view->vars['form']->children as $child) {
  196.                 $child->vars['id'] .= $options['product']->getId();
  197.             }
  198.         }
  199.     }
  200.     /**
  201.      * {@inheritdoc}
  202.      */
  203.     public function getBlockPrefix()
  204.     {
  205.         return 'add_cart';
  206.     }
  207.     /**
  208.      * validate
  209.      *
  210.      * @param type $data
  211.      * @param ExecutionContext $context
  212.      */
  213.     public function validate($dataExecutionContext $context)
  214.     {
  215.         $context->getValidator()->validate($data['product_class_id'], [
  216.             new Assert\NotBlank(),
  217.         ], '[product_class_id]');
  218.         if ($this->Product->getClassName1()) {
  219.             $context->validateValue($data['classcategory_id1'], [
  220.                 new Assert\NotBlank(),
  221.                 new Assert\NotEqualTo([
  222.                     'value' => '__unselected',
  223.                     'message' => 'form_error.not_selected',
  224.                 ]),
  225.             ], '[classcategory_id1]');
  226.         }
  227.         // 商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
  228.         if ($this->Product->getClassName2()) {
  229.             $context->getValidator()->validate($data['classcategory_id2'], [
  230.                 new Assert\NotBlank(),
  231.                 new Assert\NotEqualTo([
  232.                     'value' => '__unselected',
  233.                     'message' => 'form_error.not_selected',
  234.                 ]),
  235.             ], '[classcategory_id2]');
  236.         }
  237.     }
  238. }