src/Form/ViajeType.php line 84

  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\OptionsResolver\OptionsResolver;
  6. use Symfony\Component\Validator\Constraints\NotBlank;
  7. use Symfony\Component\Validator\Constraints\Email;
  8. use Doctrine\ORM\EntityRepository;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. class ViajeType extends AbstractType
  13. {
  14. //     protected $em;
  15.     protected $destinos;
  16.     protected $divisas;
  17. //     public function __construct(EntityManager $em)
  18. //     {
  19. //         $this->em = $em;
  20. //     }
  21.     private function getChoices($em$idioma)
  22.     {
  23.         $viajes=$em->getRepository("App\Entity\Viaje")->findAll();
  24.         $return=[];
  25.         $data = [];
  26.         foreach ($viajes as $viaje) {
  27.             $data[] = [
  28.                 'peso' => $viaje->getPeso(),
  29.                 'cantidadDia' => $viaje->getCantidadDia(),
  30.                 'destino' => $viaje->getDestino(),
  31.                 'divisaId' => $viaje->getDivisa()->getId(),
  32.             ];
  33.         }
  34.         // dump($data);
  35.         usort($data, function ($a$b) {
  36.             return $b['peso'] <=> $a['peso'];
  37.         });
  38.         // dump($data);die();
  39.         foreach ($data as $item) {
  40.             $this->destinos[$item['destino']] = $item['cantidadDia'];
  41.             $this->divisas[$item['destino']] = $item['divisaId'];
  42.         }
  43.         // foreach ($viajes as $key => $viaje) {
  44.         //         $this->destinos[$viaje->getCantidadDia()] = $viaje->getDestino();
  45.         //         $this->divisas[$viaje->getDestino()] = $viaje->getDivisa()->getId();
  46.         // }
  47.         // dump($this->destinos);die();
  48.         return $return;
  49.     }
  50.     
  51.     public function configureOptions(OptionsResolver $resolver)
  52.     {
  53.         $resolver->setDefaults( array(
  54.                     'idioma' => 'es',
  55.                     'em' => null
  56.         ));
  57.     }
  58.     public function buildForm(FormBuilderInterface $builder, array $options)
  59.     {
  60.         $this->getChoices($options['em'], $options['idioma']);
  61.         $builder
  62.             ->add('divisa',ChoiceType::class,array(
  63.                         'label' => false,
  64.                         'choices' => $this->divisas,
  65.                 )
  66.             )
  67.             ->add('destino',ChoiceType::class, array(
  68.                         'label' => false,
  69.                         'choices' => $this->destinos,
  70.                         // 'choice_value' => function ($value) {
  71.                         //     return $value;
  72.                         // },
  73.                         'choice_attr' => function ($choicestring $keymixed $value){
  74.                             if ($key)
  75.                                 return ['data-qty' => $this->destinos[$key] ];
  76.                             return [];
  77.                         },
  78.                     )
  79.             )
  80.             ->add('dias'ChoiceType::class, array(
  81.                         'attr' =>[
  82.                             'placeholder' => 'Reclamación relacionada con'
  83.                         ],
  84.                         'label' => false,
  85.                         'choices' => [
  86.                             '1' => '1',
  87.                             '2' => '2',
  88.                             '3' => '3',
  89.                             '4' => '4',
  90.                             '5' => '5',
  91.                             '6' => '6',
  92.                             '7' => '7',
  93.                             '8' => '8',
  94.                             '9' => '9',
  95.                             '10' => '10',
  96.                             '11' => '11',
  97.                             '12' => '12',
  98.                             '13' => '13',
  99.                             '14' => '14',
  100.                             '15' => '15',
  101.                             '16' => '16',
  102.                             '17' => '17',
  103.                             '18' => '18',
  104.                             '19' => '19',
  105.                             '20' => '20',
  106.                             '21' => '21',
  107.                             '22' => '22',
  108.                             '23' => '23',
  109.                             '24' => '24',
  110.                             '25' => '25',
  111.                             '26' => '26',
  112.                             '27' => '27',
  113.                             '28' => '28',
  114.                             ],
  115.                         'required' => true,
  116.                         'constraints' => array(new NotBlank()),
  117.                         'data' => '7'
  118.                         )
  119.             )
  120.             /*->add('cantidad', 'integer', array(
  121.                         'label' => false,
  122.                         'required' => true,
  123.                         'disabled' => true,
  124.                         'constraints' => array(new NotBlank()),
  125.                         )
  126.             )*/
  127.         ;
  128.     }
  129.     public function getName()
  130.     {
  131.         return 'dinamic_viaje';
  132.     }
  133. }