<?php
// src/Form/EventListener/AddNameFieldSubscriber.php
namespace App\FormSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Form;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class GHVacanteSubscriber implements EventSubscriberInterface {
private $entityManager;
/**
* @var Security
*/
private $token;
public function __construct(
EntityManagerInterface $entityManager, Security $token
) {
$this->entityManager = $entityManager;
$this->token = $token;
}
public static function getSubscribedEvents(): array {
// Tells the dispatcher that you want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return [FormEvents::PRE_SUBMIT => 'preSubmit'];
}
public function preSubmit(FormEvent $event): void {
$data = $event->getData();
$form = $event->getForm();
//data es un arreglo con los valores establecidos por el usuario en el form.
//como $data contiene el pais seleccionado por el usuario al enviar el formulario,
// usamos el valor de la posicion $data['country'] para filtrar el sql de los estados
if (isset($data['empresaFilial'])) {
$idEmpresa = $data['empresaFilial'];
$empresa = $this->entityManager->getRepository(\App\Entity\GHPerfilCargo::class)->find($idEmpresa);
$user = $this->token->getToken()->getUser();
$persona = $user->getPersona();
$proceso = $this->entityManager->getRepository(\App\Entity\ParProceso::class)->find($persona->getPerfilCargo()->getParProceso()->getId());
$idProcesoUser = $this->token->getUser()->getPersona()->getPerfilCargo()->getParProceso()->getId();
// Validar si es gerente
$isGerente = in_array($persona->getPerfilCargo()->getCargo()->getId(), [2,29,38]);
// dump($isGerente);
$form
->add('sede', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, ['class' => \App\Entity\TerSedeEmpresa::class, 'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idEmpresa) {
return $er->createQueryBuilder('se')
->leftJoin('se.empresaFilial', 'ef')
->where('se.nombre is null')
->orWhere('ef.id =:idEmpresa')
->setParameter('idEmpresa', $idEmpresa)
->orderBy('se.nombre', 'ASC');
}, 'label' => 'Sucursal',
'label_attr' => ['class' => 'field_required'],
'attr' => ['class' => 'form-control'], 'placeholder' => 'Seleccione una Opción.'])
->add('perfilCargo', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
'class' => \App\Entity\GHPerfilCargo::class,
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idEmpresa, $idProcesoUser, $isGerente) {
$qb = $er->createQueryBuilder('c')
->leftJoin('c.empresaFilial', 'ef')
->leftJoin('c.cargo', 'car')
->leftJoin('c.ParProceso', 'pp')
->where('c.estado = 1')
->andWhere('ef.id = :idEmpresaFilial')
->setParameter('idEmpresaFilial', $idEmpresa)
->orderBy('car.nombre', 'ASC');
// Si es gerente, filtra por el proceso del usuario
if (!$isGerente) {
$qb->andWhere('pp.id = :idProcesoUser')
->setParameter('idProcesoUser', $idProcesoUser);
}
return $qb;
},
'choice_label' => 'cargo.nombre',
'placeholder' => 'Seleccione un cargo',
])
;
$event->setData($data);
}
if (isset($data['perfilCargo'])) {
$idPerfilCargo = $data['perfilCargo'];
$perfilCargo = $this->entityManager->getRepository(\App\Entity\GHPerfilCargo::class)->find($idPerfilCargo);
$idCargo = $perfilCargo ? $perfilCargo->getCargo()->getId() : null;
$form
->add('ParTipoContrato', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
'class' => \App\Entity\ParTipoContrato::class,
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idCargo) {
return $er->createQueryBuilder('tc')
->join('tc.perfilCargo', 'pc')
->join('pc.cargo', 'c')
->where('c.id = :idCargo')
->setParameter('idCargo', $idCargo)
->orderBy('tc.nombre', 'ASC');
},
'label' => 'Tipo de Contrato',
'placeholder' => 'Seleccione una opción',
'attr' => ['class' => 'form-control'],
'required' => true,
])
->add('tipoVinculacion', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
'class' => \App\Entity\ParTipoVinculacion::class,
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idCargo) {
return $er->createQueryBuilder('tv')
->join('tv.perfilCargo', 'pc')
->join('pc.cargo', 'c')
->where('c.id = :idCargo')
->setParameter('idCargo', $idCargo)
->orderBy('tv.nombre', 'ASC');
},
'label' => 'Tipo de Vinculación',
'placeholder' => 'Seleccione una opción',
'attr' => ['class' => 'form-control'],
'required' => true,
])
;
function agregarCampoHTMLSiExiste($titulo, $html) {
$plano = trim(strip_tags($html));
if ($plano !== '' && strtolower($plano) !== 'null') {
return '<strong><i class="fas fa-check-circle fa-sm text-primary text-primary-shadow mr-2"></i> ' . $titulo . ':</strong> <br> ' . $html . '<br><br>';
}
return '';
}
$textPerfil = '';
$textPerfil .= agregarCampoHTMLSiExiste('Cargo', $perfilCargo->getCargo()?->getNombre());
$textPerfil .= agregarCampoHTMLSiExiste('Rango Salarial', $perfilCargo->getRangoSalarial());
$textPerfil .= agregarCampoHTMLSiExiste('Responsabilidades y Funciones', $perfilCargo->getResponsabilidades());
$textPerfil .= agregarCampoHTMLSiExiste('Educación', $perfilCargo->getEducacion());
$textPerfil .= agregarCampoHTMLSiExiste('Formación', $perfilCargo->getFormacionExperiencia());
$textPerfil .= agregarCampoHTMLSiExiste('Habilidades', $perfilCargo->getHabilidades());
$textPerfil .= agregarCampoHTMLSiExiste('Experiencia', $perfilCargo->getExperiencia());
$textPerfil .= agregarCampoHTMLSiExiste('Conocimientos Específicos', $perfilCargo->getConocimientosEspecificos());
$textPerfil .= agregarCampoHTMLSiExiste('Criticidad', $perfilCargo->getParCriticidad()?->getNombreCriticidad());
$data['resumenPerfilCandidato'] = $textPerfil;
//$data['rangoSalarial'] = $perfilCargo->getRangoSalarial();
$event->setData($data);
}
}
}