src/Form/UnidadFiltroType.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Equipo;
  4. use App\Entity\Impresora;
  5. use App\Entity\Otro;
  6. use App\Entity\Red;
  7. use App\Entity\Unidad;
  8. use App\Entity\Usuario;
  9. use Doctrine\ORM\EntityRepository;
  10. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. class UnidadFiltroType extends AbstractType
  20. {
  21.     private TokenStorageInterface $token;
  22.     public function __construct(TokenStorageInterface $token)
  23.     {
  24.         $this->token $token;
  25.     }
  26.     public function buildForm(FormBuilderInterface $builder, array $options): void
  27.     {
  28.         $builder
  29.             ->add('filtro'HiddenType::class, ['data' => true])
  30.         ;
  31.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
  32.             $form $event->getForm();
  33.             /** @var Usuario $user */
  34.             $user $this->token->getToken()->getUser();
  35.             if ($options['unidad']) {
  36.                 $form->add('unidad'EntityType::class, [
  37.                     'class' => Unidad::class,
  38.                     'attr' => ['class' => 'form-control'],
  39.                     'label' => 'Unidad',
  40.                     'placeholder' => 'Seleccione una unidad',
  41.                     'query_builder' => function (EntityRepository $er) use ($user) {
  42.                         // Verifica si el rol es mayor a 2
  43.                         if ($user->getRol()->getIdRol() > 2) {
  44.                             // Si el rol es mayor a 2, solo mostrar la unidad del usuario
  45.                             return $er->createQueryBuilder('u')
  46.                                 ->where('u.idUni = :unidadId')
  47.                                 ->setParameter('unidadId'$user->getUnidad()->getIdUni())
  48.                                 ->orderBy('u.nombre''ASC');
  49.                         } else {
  50.                             // Si el rol es 2 o menor, mostrar todas las unidades
  51.                             return $er->createQueryBuilder('u')
  52.                                 ->orderBy('u.nombre''ASC');
  53.                         }
  54.                     }
  55.                 ]);
  56.                 if($options['tipo'] == 'equipo') {
  57.                     $form->add('marca'EntityType::class, [
  58.                         'required' => false,
  59.                         'label' => 'Marca',
  60.                         'class' => Equipo::class,
  61.                         'choice_label' => 'marca',
  62.                         'placeholder' => 'Todas',
  63.                         'attr' => [
  64.                             'class' => 'form-control',
  65.                             'style' => 'width: 130px;',
  66.                             'data-live-search' => 'true',
  67.                         ],
  68.                         'query_builder' => function(EntityRepository $er) {
  69.                             return $er->createQueryBuilder('e')
  70.                                 ->groupBy('e.marca')
  71.                                 ->orderBy('e.marca''ASC');
  72.                         }
  73.                     ]);
  74.                 }
  75.                 if($options['tipo'] == 'otro') {
  76.                     $form->add('marca'EntityType::class, [
  77.                         'required' => false,
  78.                         'label' => 'Marca',
  79.                         'class' => Otro::class,
  80.                         'choice_label' => 'marca',
  81.                         'placeholder' => 'Todas',
  82.                         'attr' => [
  83.                             'class' => 'form-control',
  84.                             'style' => 'width: 130px;',
  85.                             'data-live-search' => 'true',
  86.                         ],
  87.                         'query_builder' => function(EntityRepository $er) {
  88.                             return $er->createQueryBuilder('o')
  89.                                 ->groupBy('o.marca')
  90.                                 ->orderBy('o.marca''ASC');
  91.                         }
  92.                     ]);
  93.                 }
  94.                 if($options['tipo'] == 'impresora') {
  95.                     $form->add('marca'EntityType::class, [
  96.                         'required' => false,
  97.                         'label' => 'Marca',
  98.                         'class' => Impresora::class,
  99.                         'choice_label' => 'marca',
  100.                         'placeholder' => 'Todas',
  101.                         'attr' => [
  102.                             'class' => 'form-control',
  103.                             'style' => 'width: 130px;',
  104.                             'data-live-search' => 'true',
  105.                         ],
  106.                         'query_builder' => function(EntityRepository $er) {
  107.                             return $er->createQueryBuilder('i')
  108.                                 ->groupBy('i.marca')
  109.                                 ->orderBy('i.marca''ASC');
  110.                         }
  111.                     ]);
  112.                 }
  113.                 if($options['tipo'] == 'comunicacion') {
  114.                     $form->add('marca'EntityType::class, [
  115.                         'required' => false,
  116.                         'label' => 'Marca',
  117.                         'class' => Red::class,
  118.                         'choice_label' => 'marca',
  119.                         'placeholder' => 'Todas',
  120.                         'attr' => [
  121.                             'class' => 'form-control',
  122.                             'style' => 'width: 130px;',
  123.                             'data-live-search' => 'true',
  124.                         ],
  125.                         'query_builder' => function(EntityRepository $er) {
  126.                             return $er->createQueryBuilder('r')
  127.                                 ->groupBy('r.marca')
  128.                                 ->orderBy('r.marca''ASC');
  129.                         }
  130.                     ]);
  131.                 }
  132.                 if($options['tipo'] == 'equipo') {
  133.                     $form->add('modelo'EntityType::class, [
  134.                         'required' => false,
  135.                         'class' => 'App\Entity\Equipo',
  136.                         'label' => 'Modelo',
  137.                         'choice_label' => 'modelo',
  138.                         'placeholder' => 'Todas',
  139.                         'attr' => [
  140.                             'class' => 'form-control',
  141.                             'style' => 'width: 130px;'
  142.                         ],
  143.                         'query_builder' => function(EntityRepository $er) {
  144.                             return $er->createQueryBuilder('e')
  145.                                 ->groupBy('e.marca')
  146.                                 ->orderBy('e.marca''ASC');
  147.                         }
  148.                     ]);
  149.                 }
  150.                 if($options['tipo'] == 'otro') {
  151.                     $form->add('modelo'EntityType::class, [
  152.                         'required' => false,
  153.                         'class' => Otro::class,
  154.                         'label' => 'Modelo',
  155.                         'choice_label' => 'modelo',
  156.                         'placeholder' => 'Todas',
  157.                         'attr' => [
  158.                             'class' => 'form-control',
  159.                             'style' => 'width: 130px;'
  160.                         ],
  161.                         'query_builder' => function(EntityRepository $er) {
  162.                             return $er->createQueryBuilder('o')
  163.                                 ->groupBy('o.marca')
  164.                                 ->orderBy('o.marca''ASC');
  165.                         },
  166.                     ]);
  167.                 }
  168.                 if($options['tipo'] == 'impresora') {
  169.                     $form->add('modelo'EntityType::class, [
  170.                         'required' => false,
  171.                         'class' => Impresora::class,
  172.                         'label' => 'Modelo',
  173.                         'choice_label' => 'modelo',
  174.                         'placeholder' => 'Todas',
  175.                         'attr' => [
  176.                             'class' => 'form-control',
  177.                             'style' => 'width: 130px;'
  178.                         ],
  179.                         'query_builder' => function(EntityRepository $er) {
  180.                             return $er->createQueryBuilder('i')
  181.                                 ->groupBy('i.marca')
  182.                                 ->orderBy('i.marca''ASC');
  183.                         },
  184.                     ]);
  185.                 }
  186.                 if($options['tipo'] == 'comunicacion') {
  187.                     $form->add('modelo'EntityType::class, [
  188.                         'required' => false,
  189.                         'class' => Red::class,
  190.                         'label' => 'Modelo',
  191.                         'choice_label' => 'modelo',
  192.                         'placeholder' => 'Todas',
  193.                         'attr' => [
  194.                             'class' => 'form-control',
  195.                             'style' => 'width: 130px;'
  196.                         ],
  197.                         'query_builder' => function(EntityRepository $er) {
  198.                             return $er->createQueryBuilder('r')
  199.                                 ->groupBy('r.marca')
  200.                                 ->orderBy('r.marca''ASC');
  201.                         },
  202.                     ]);
  203.                 }
  204.                 $form->add('uso'ChoiceType::class, [
  205.                     'required' => false,
  206.                     'label' => 'Uso',
  207.                     'choices' => [
  208.                         'Educativo' => '1',
  209.                         'Docente' => '2',
  210.                         'Administrativo' => '3',
  211.                     ],
  212.                     'placeholder' => 'Todos los usos',
  213.                     'attr' => [
  214.                         'class' => 'form-control',
  215.                         'style' => 'width: 130px;'
  216.                     ],
  217.                 ]);
  218.                 $form->add('estatus'ChoiceType::class, [
  219.                     'required' => false,
  220.                     'label' => 'Estatus',
  221.                     'choices' => [
  222.                         'En operaciĆ³n' => '1',
  223.                         'Descompuesto' => '2',
  224.                         'Sin instalar' => '3',
  225.                         'En proceso de baja' => '4',
  226.                         'Baja' => '5',
  227.                     ],
  228.                     'placeholder' => 'Todos los estatus',
  229.                     'attr' => [
  230.                         'class' => 'form-control',
  231.                         'style' => 'width: 130px;'
  232.                     ],
  233.                 ]);
  234.             }
  235.         });
  236.     }
  237.     public function configureOptions(OptionsResolver $resolver): void
  238.     {
  239.         $resolver->setDefaults([
  240.             'unidad' => false,
  241.             'marca' => false,
  242.             'modelo' => false,
  243.             'tipo' => false
  244.         ]);
  245.     }
  246.     //Para subir formulario
  247. }