src/Form/Carrito/CarroType.php line 21

  1. <?php
  2. namespace App\Form\Carrito;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\Form\FormView;
  6. use Symfony\Component\Form\FormInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use App\Form\Carrito\CartStep1Type;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\ORM\EntityRepository;
  11. use App\Entity\Pedido;
  12. use App\Entity\Carro;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  16. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  19. class CarroType extends AbstractType
  20. {
  21.     protected $em;
  22.     protected $locale;
  23.     protected $pedidos;
  24.     public function __construct(EntityManagerInterface $em$locale=null)
  25.     {
  26.         $this->em       $em;
  27.         $this->locale   $locale;
  28.     }
  29.     public function buildForm(FormBuilderInterface $builder, array $options)
  30.     {
  31.         //Las entidades mostradas al usuario son del tipo DivisaIdioma (ver CartStep1Type)
  32.         //mientras que las que recibe el pedido son del tipo Divisa
  33.         //en PRE_SUMBIT guardamos las ID'S y en SUBMIT buscamos las divisas correspondientes
  34.         //a la entidad para hacer la transformación de DivisaIdioma(Formulario) a Divisa(Entidad Carro)
  35.         $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
  36.             $data $event->getData();
  37.             $pedidos=$data->getPedidos();
  38.             
  39.             foreach ($pedidos as $key=>$pedido) {
  40.                 $this->pedidos[$key]['divisaOrigen'] = 10;
  41.             }
  42. //             dump($this->pedidos);
  43. //             dump($pedidos);
  44. //             dump($data);die();
  45.             
  46.             foreach ($pedidos as $key=>$pedido) {
  47.                  $divisaFinal=$this->em->getRepository('App\Entity\Divisa')->findOneById($this->pedidos[$key]['divisaFinal']);
  48.                  $divisaOrigen=$this->em->getRepository('App\Entity\Divisa')->findOneById($this->pedidos[$key]['divisaOrigen']);
  49. /*                $divisaFinal= $pedido->getDivisaFinal();
  50.                 $divisaOrigen= $pedido->getDivisaOrigen();*/
  51.                 $pedido->setDivisaFinal($divisaFinal);
  52.                 $pedido->setDivisaOrigen($divisaOrigen);
  53.             }
  54.             
  55. //             dump($pedidos); die();
  56.         });
  57.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
  58.             $data $event->getData();
  59.             $this->pedidos=$data['pedidos'];
  60.         });
  61.         $carro $builder->getData();
  62.         foreach ($carro->getPedidos() as $pedido){
  63.         }
  64.         $builder
  65.             ->add('pedidos'CollectionType::class, array(
  66.                     'entry_type' => CartStep1Type::class,
  67.                     'by_reference' => false,
  68.                     'label' => false,
  69.                     'entry_options' => array(
  70.                         'label' => false,
  71.                         'data_class' => 'App\Entity\Pedido'
  72.                     ),
  73.                     'allow_add' => true,
  74.                     'prototype' => true,
  75.                     'allow_delete' => true,
  76. //                     'cascade_validation' => true,
  77.                 )
  78.             )
  79.             ->add('reserva'HiddenType::class, array(
  80.                     'label' => false,
  81.                     'data' => 0,
  82.                     'attr' => array(
  83.                         'class'=>'cantidadFinal'
  84.                     )
  85.                 )
  86.             )
  87.             ->add('tipo'HiddenType::class, array(
  88.                     'data' => 0,
  89.                     'required' => false,
  90.                     'attr' => array('name' => 'tags'),
  91.                     'mapped' => false,
  92.                     'data' => $options['tipo_value'],
  93.                 )
  94.             )
  95.             ->add('codigoPromo'HiddenType::class, array(
  96.                     'label' => 'Codigo Promocional',
  97.                     'attr'  => array(
  98.                         'class' => 'codigoPromInp',
  99.                     )
  100.                 )
  101.             )
  102.             ->add('tipoPromo'HiddenType::class, array(
  103.                     'required' => false,
  104.                     'mapped' => false,
  105.                     'data' => $options['tipo_promo'],
  106.                 )
  107.             )
  108.             ->add('metodoEntrega'HiddenType::class, array(
  109.                     'required' => false,
  110.                     'mapped' => false,
  111.                 )
  112.             )
  113.             ->add('provincia'HiddenType::class, array(
  114.                     'label' => false,
  115.                     'required' => false
  116.                 )
  117.             )
  118.         ;
  119.     }
  120.     public function configureOptions(OptionsResolver $resolver)
  121.     {
  122. //         parent::setDefaultOptions($resolver);
  123.         $resolver->setDefaults( array(
  124.                     'data_class' => 'App\Entity\Carro',
  125.                     'tipo_value' => 0,
  126.                     'tipo_promo' => Carro::TIPO_PROMO_DIVISA,
  127.                     'em' => null,
  128.                     'locale' => null
  129.         ));
  130.     }
  131.     public function getName()
  132.     {
  133.         return 'dinamic_shop_carro';
  134.     }
  135. }