<?php
namespace App\Form;
use App\Entity\Equipo;
use App\Entity\Impresora;
use App\Entity\Otro;
use App\Entity\Red;
use App\Entity\Unidad;
use App\Entity\Usuario;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UnidadFiltroType extends AbstractType
{
private TokenStorageInterface $token;
public function __construct(TokenStorageInterface $token)
{
$this->token = $token;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('filtro', HiddenType::class, ['data' => true])
;
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$form = $event->getForm();
/** @var Usuario $user */
$user = $this->token->getToken()->getUser();
if ($options['unidad']) {
$form->add('unidad', EntityType::class, [
'class' => Unidad::class,
'attr' => ['class' => 'form-control'],
'label' => 'Unidad',
'placeholder' => 'Seleccione una unidad',
'query_builder' => function (EntityRepository $er) use ($user) {
// Verifica si el rol es mayor a 2
if ($user->getRol()->getIdRol() > 2) {
// Si el rol es mayor a 2, solo mostrar la unidad del usuario
return $er->createQueryBuilder('u')
->where('u.idUni = :unidadId')
->setParameter('unidadId', $user->getUnidad()->getIdUni())
->orderBy('u.nombre', 'ASC');
} else {
// Si el rol es 2 o menor, mostrar todas las unidades
return $er->createQueryBuilder('u')
->orderBy('u.nombre', 'ASC');
}
}
]);
if($options['tipo'] == 'equipo') {
$form->add('marca', EntityType::class, [
'required' => false,
'label' => 'Marca',
'class' => Equipo::class,
'choice_label' => 'marca',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;',
'data-live-search' => 'true',
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e')
->groupBy('e.marca')
->orderBy('e.marca', 'ASC');
}
]);
}
if($options['tipo'] == 'otro') {
$form->add('marca', EntityType::class, [
'required' => false,
'label' => 'Marca',
'class' => Otro::class,
'choice_label' => 'marca',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;',
'data-live-search' => 'true',
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('o')
->groupBy('o.marca')
->orderBy('o.marca', 'ASC');
}
]);
}
if($options['tipo'] == 'impresora') {
$form->add('marca', EntityType::class, [
'required' => false,
'label' => 'Marca',
'class' => Impresora::class,
'choice_label' => 'marca',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;',
'data-live-search' => 'true',
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('i')
->groupBy('i.marca')
->orderBy('i.marca', 'ASC');
}
]);
}
if($options['tipo'] == 'comunicacion') {
$form->add('marca', EntityType::class, [
'required' => false,
'label' => 'Marca',
'class' => Red::class,
'choice_label' => 'marca',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;',
'data-live-search' => 'true',
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('r')
->groupBy('r.marca')
->orderBy('r.marca', 'ASC');
}
]);
}
if($options['tipo'] == 'equipo') {
$form->add('modelo', EntityType::class, [
'required' => false,
'class' => 'App\Entity\Equipo',
'label' => 'Modelo',
'choice_label' => 'modelo',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e')
->groupBy('e.marca')
->orderBy('e.marca', 'ASC');
}
]);
}
if($options['tipo'] == 'otro') {
$form->add('modelo', EntityType::class, [
'required' => false,
'class' => Otro::class,
'label' => 'Modelo',
'choice_label' => 'modelo',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('o')
->groupBy('o.marca')
->orderBy('o.marca', 'ASC');
},
]);
}
if($options['tipo'] == 'impresora') {
$form->add('modelo', EntityType::class, [
'required' => false,
'class' => Impresora::class,
'label' => 'Modelo',
'choice_label' => 'modelo',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('i')
->groupBy('i.marca')
->orderBy('i.marca', 'ASC');
},
]);
}
if($options['tipo'] == 'comunicacion') {
$form->add('modelo', EntityType::class, [
'required' => false,
'class' => Red::class,
'label' => 'Modelo',
'choice_label' => 'modelo',
'placeholder' => 'Todas',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('r')
->groupBy('r.marca')
->orderBy('r.marca', 'ASC');
},
]);
}
$form->add('uso', ChoiceType::class, [
'required' => false,
'label' => 'Uso',
'choices' => [
'Educativo' => '1',
'Docente' => '2',
'Administrativo' => '3',
],
'placeholder' => 'Todos los usos',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
]);
$form->add('estatus', ChoiceType::class, [
'required' => false,
'label' => 'Estatus',
'choices' => [
'En operaciĆ³n' => '1',
'Descompuesto' => '2',
'Sin instalar' => '3',
'En proceso de baja' => '4',
'Baja' => '5',
],
'placeholder' => 'Todos los estatus',
'attr' => [
'class' => 'form-control',
'style' => 'width: 130px;'
],
]);
}
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'unidad' => false,
'marca' => false,
'modelo' => false,
'tipo' => false
]);
}
//Para subir formulario
}