<?php
namespace App\Form;
use App\Entity\Equipo;
use App\Entity\Ubicacion;
use App\Entity\Unidad;
use App\Entity\Usuario;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\NotBlank;
class EquipoType extends AbstractType
{
private TokenStorageInterface $token;
public function __construct(TokenStorageInterface $token)
{
$this->token = $token;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
//->add('marbete')
->add('marbete', TextType::class, [
'label' => '* Marbete',
'constraints' => [new NotBlank(['message' => 'El campo "Marbete" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm',
'readonly' => $options['bloquear_edicion']
]
])
//->add('descripcion')
->add('descripcion', TextareaType::class, [
'label' => '* Descripción',
//'constraints' => [new NotBlank(['message' => 'El campo "Descripción" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm',
//'readonly' => $options['bloquear_edicion']
]
])
//->add('marca')
->add('marca', TextType::class, [
'label' => '* Marca',
'constraints' => [new NotBlank(['message' => 'El campo "Marca" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm',
'readonly' => $options['bloquear_edicion']
]
])
//->add('modelo')
->add('modelo', TextType::class, [
'label' => '* Modelo',
'constraints' => [new NotBlank(['message' => 'El campo "Modelo" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm',
'readonly' => $options['bloquear_edicion']
]
])
//->add('serie')
->add('serie', TextType::class, [
'label' => '* No. serie',
'constraints' => [new NotBlank(['message' => 'El campo "Numero de serie" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm',
'readonly' => $options['bloquear_edicion']
]
])
//->add('procesador')
->add('procesador', TextType::class, [
'label' => 'Procesador',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Procesador" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('nucleos')
->add('nucleos', IntegerType::class, [
'label' => 'Núcleos',
'constraints' => [new NotBlank(['message' => 'El campo "Núcleos" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('cpuVelocidad')
->add('cpuVelocidad', NumberType::class, [
'label' => 'Velocidad de CPU',
'constraints' => [new NotBlank(['message' => 'El campo "CPU" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('ram')
->add('ram', IntegerType::class, [
'label' => 'RAM',
'constraints' => [new NotBlank(['message' => 'El campo "RAM" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('hhd')
->add('hhd', NumberType::class, [
'label' => 'HDD',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "HDD" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//>add('ssd')
->add('ssd', NumberType::class, [
'label' => 'SSD',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "SDD" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorMarbete')
->add('monitorMarbete', TextType::class, [
'label' => 'Marbete',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Marbete de monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorMarca')
->add('monitorMarca', TextType::class, [
'label' => 'Marca',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Marca de monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorModelo')
->add('monitorModelo', TextType::class, [
'label' => 'Modelo',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Modelo de monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorSerie')
->add('monitorSerie', TextType::class, [
'label' => 'No. serie',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Número de serie del monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorTipo')
->add('monitorTipo', TextType::class, [
'label' => 'Tipo',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Tipo de monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('monitorPulgadas')
->add('monitorPulgadas', TextType::class, [
'label' => 'Pulgadas',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Pulgadas de monitor" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('tecladoMarca')
->add('tecladoMarca', TextType::class, [
'label' => 'Marca',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Marca de teclado" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('tecladoModelo')
->add('tecladoModelo', TextType::class, [
'label' => 'Modelo',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Modelo de teclado" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('tecladoSerie')
->add('tecladoSerie', TextType::class, [
'label' => 'No. serie',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Número de serie del teclaod" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('mouseMarca')
->add('mouseMarca', TextType::class, [
'label' => 'Marca',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Marca del mouse" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('mouseModelo')
->add('mouseModelo', TextType::class, [
'label' => 'Modelo',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Modelo del mouse" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('mouseSerie')
->add('mouseSerie', TextType::class, [
'label' => 'Serie',
'required' => false,
//'constraints' => [new NotBlank(['message' => 'El campo "Serie de mouse" no debe estar vacío.'])],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('uso')
->add('uso', ChoiceType::class, [
'label' => 'Uso',
'choices' => [
'Educativo' => 1,
'Docente' => 2,
'Administrativo' => 3
],
'placeholder' => 'Seleccione un uso',
'attr' => ['class' => 'form-control form-control-sm']
])
//->add('funcion')
->add('funcion', ChoiceType::class, [
'label' => 'Función',
'choices' => [
'Escritorio' => 1,
'Servidor' => 2
],
'placeholder' => 'Seleccione una función',
'attr' => ['class' => 'form-control form-control-sm']
])
//->add('funcionDescripcion')
->add('funcionDescripcion', TextType::class, [
'label' => 'Descripcion de la función',
'required' => false,
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('estatus')
->add('estatus', ChoiceType::class, [
'label' => 'Estatus',
'choices' => [
'En operacion' => 1,
'Descompuesto' => 2,
'Sin instalar' => 3,
'En proceso de baja' => 4,
'Baja' => 5
],
'placeholder' => 'Seleccione un estatus',
'attr' => ['class' => 'form-control form-control-sm']
])
//->add('usuario')
->add('usuario', EntityType::class, [
'label' => 'Resguardatario',
'class' => Usuario::class,
'choice_label' => function($usuario) {
return $usuario->getNombre() . ' ' . $usuario->getPapellido() . ' ' . $usuario->getSapellido();
},
'attr' => [
'class' => 'form-control form-control-sm'
],
'placeholder' => 'Seleccione un usuario',
'query_builder' => function(EntityRepository $entityRepository) use ($options) {
/** @var Usuario $user */
$user = $this->token->getToken()->getUser();
$query = $entityRepository->createQueryBuilder('u');
$query->where('u.rol >= 3');
$query->andWhere("u.estatus = 'Activo'");
if ($options['unidad']) {
$query->andWhere('u.unidad = :unidad');
$query->setParameter('unidad', $user->getUnidad());
}
$query->orderBy('u.nombre', 'ASC');
return $query;
}
])
//->add('ubicacion')
->add('ubicacion', EntityType::class, [
'label' => 'Ubicación',
'class' => Ubicacion::class,
'attr' => [
'class' => 'form-control form-control-sm'
],
'placeholder' => 'Seleccione una ubicación',
'query_builder' => function(EntityRepository $entityRepository) use ($options) {
/** @var Usuario $user */
$user = $this->token->getToken()->getUser();
$query = $entityRepository->createQueryBuilder('a');
$query->where('a.activo = 1');
if ($options['unidad']) {
$query->andWhere('a.unidad = :unidad');
$query->setParameter('unidad', $user->getUnidad());
}
$query->orderBy('a.area', 'ASC');
return $query;
}
])
//->add('ip')
->add('ip', TextType::class, [
'label' => 'Dirección IP',
'required' => false,
'constraints' => [
new Ip(['message' => 'El campo Dirección IP debe ser válido.'])
],
'attr' => [
'class' => 'form-control form-control-sm'
]
])
//->add('mac')
->add('mac', TextType::class, [
'label' => 'Dirección Mac',
'required' => false,
'attr' => [
'class' => 'form-control form-control-sm'
]
])
;
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$form = $event->getForm();
if ($options['unidad']) {
$form->add('unidad', EntityType::class, [
'class' => Unidad::class,
'required' => true,
'attr' => [
'class' => 'form-control form-control-sm'
],
'query_builder' => function(EntityRepository $entityRepository){
/** @var Usuario $user */
$user = $this->token->getToken()->getUser();
$query = $entityRepository->createQueryBuilder('u');
$query->andWhere('u.idUni = :unidad');
$query->orderBy('u.nombre', 'ASC');
$query->setParameter('unidad', $user->getUnidad()->getIdUni());
return $query;
}
]);
}
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Equipo::class,
'unidad' => false,
'bloquear_edicion' => false
]);
}
//Para subir formulario
}