src/Form/ViajeType.php line 84
- <?php
- namespace App\Form;
- use Symfony\Component\Form\AbstractType;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- use Symfony\Component\Validator\Constraints\NotBlank;
- use Symfony\Component\Validator\Constraints\Email;
- use Doctrine\ORM\EntityRepository;
- use Doctrine\ORM\EntityManager;
- use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- class ViajeType extends AbstractType
- {
- // protected $em;
- protected $destinos;
- protected $divisas;
- // public function __construct(EntityManager $em)
- // {
- // $this->em = $em;
- // }
- private function getChoices($em, $idioma)
- {
- $viajes=$em->getRepository("App\Entity\Viaje")->findAll();
- $return=[];
- $data = [];
- foreach ($viajes as $viaje) {
- $data[] = [
- 'peso' => $viaje->getPeso(),
- 'cantidadDia' => $viaje->getCantidadDia(),
- 'destino' => $viaje->getDestino(),
- 'divisaId' => $viaje->getDivisa()->getId(),
- ];
- }
- // dump($data);
- usort($data, function ($a, $b) {
- return $b['peso'] <=> $a['peso'];
- });
- // dump($data);die();
- foreach ($data as $item) {
- $this->destinos[$item['destino']] = $item['cantidadDia'];
- $this->divisas[$item['destino']] = $item['divisaId'];
- }
- // foreach ($viajes as $key => $viaje) {
- // $this->destinos[$viaje->getCantidadDia()] = $viaje->getDestino();
- // $this->divisas[$viaje->getDestino()] = $viaje->getDivisa()->getId();
- // }
- // dump($this->destinos);die();
- return $return;
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults( array(
- 'idioma' => 'es',
- 'em' => null
- ));
- }
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $this->getChoices($options['em'], $options['idioma']);
- $builder
- ->add('divisa',ChoiceType::class,array(
- 'label' => false,
- 'choices' => $this->divisas,
- )
- )
- ->add('destino',ChoiceType::class, array(
- 'label' => false,
- 'choices' => $this->destinos,
- // 'choice_value' => function ($value) {
- // return $value;
- // },
- 'choice_attr' => function ($choice, string $key, mixed $value){
- if ($key)
- return ['data-qty' => $this->destinos[$key] ];
- return [];
- },
- )
- )
- ->add('dias', ChoiceType::class, array(
- 'attr' =>[
- 'placeholder' => 'Reclamación relacionada con'
- ],
- 'label' => false,
- 'choices' => [
- '1' => '1',
- '2' => '2',
- '3' => '3',
- '4' => '4',
- '5' => '5',
- '6' => '6',
- '7' => '7',
- '8' => '8',
- '9' => '9',
- '10' => '10',
- '11' => '11',
- '12' => '12',
- '13' => '13',
- '14' => '14',
- '15' => '15',
- '16' => '16',
- '17' => '17',
- '18' => '18',
- '19' => '19',
- '20' => '20',
- '21' => '21',
- '22' => '22',
- '23' => '23',
- '24' => '24',
- '25' => '25',
- '26' => '26',
- '27' => '27',
- '28' => '28',
- ],
- 'required' => true,
- 'constraints' => array(new NotBlank()),
- 'data' => '7'
- )
- )
- /*->add('cantidad', 'integer', array(
- 'label' => false,
- 'required' => true,
- 'disabled' => true,
- 'constraints' => array(new NotBlank()),
- )
- )*/
- ;
- }
- public function getName()
- {
- return 'dinamic_viaje';
- }
- }