src/FormSubscriber/GHVacanteSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. // src/Form/EventListener/AddNameFieldSubscriber.php
  3. namespace App\FormSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\Form\Form;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class GHVacanteSubscriber implements EventSubscriberInterface {
  12.     private $entityManager;
  13.     /**
  14.      * @var Security
  15.      */
  16.     private $token;
  17.     public function __construct(
  18.         EntityManagerInterface $entityManagerSecurity $token
  19.     ) {
  20.         $this->entityManager $entityManager;
  21.         $this->token $token;
  22.     }
  23.     public static function getSubscribedEvents(): array {
  24.         // Tells the dispatcher that you want to listen on the form.pre_set_data
  25.         // event and that the preSetData method should be called.
  26.         return [FormEvents::PRE_SUBMIT => 'preSubmit'];
  27.     }
  28.     public function preSubmit(FormEvent $event): void {
  29.         $data $event->getData();
  30.         $form $event->getForm();
  31.         //data es un arreglo con los valores establecidos por el usuario en el form.
  32.         //como $data contiene el pais seleccionado por el usuario al enviar el formulario,
  33.         // usamos el valor de la posicion $data['country'] para filtrar el sql de los estados
  34.         if (isset($data['empresaFilial'])) {
  35.             $idEmpresa $data['empresaFilial'];
  36.             $empresa $this->entityManager->getRepository(\App\Entity\GHPerfilCargo::class)->find($idEmpresa);
  37.             $user $this->token->getToken()->getUser();
  38.             $persona $user->getPersona();
  39.             $proceso $this->entityManager->getRepository(\App\Entity\ParProceso::class)->find($persona->getPerfilCargo()->getParProceso()->getId());
  40.             $idProcesoUser $this->token->getUser()->getPersona()->getPerfilCargo()->getParProceso()->getId();
  41.             // Validar si es gerente
  42.             $isGerente in_array($persona->getPerfilCargo()->getCargo()->getId(), [2,29,38]);
  43. //            dump($isGerente);
  44.             $form
  45.                     ->add('sede'\Symfony\Bridge\Doctrine\Form\Type\EntityType::class, ['class' => \App\Entity\TerSedeEmpresa::class, 'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idEmpresa) {
  46.                             return $er->createQueryBuilder('se')
  47.                             ->leftJoin('se.empresaFilial''ef')
  48.                             ->where('se.nombre is null')
  49.                             ->orWhere('ef.id  =:idEmpresa')
  50.                             ->setParameter('idEmpresa'$idEmpresa)
  51.                             ->orderBy('se.nombre''ASC');
  52.                         }, 'label' => 'Sucursal',
  53.                         'label_attr' => ['class' => 'field_required'],
  54.                         'attr' => ['class' => 'form-control'], 'placeholder' => 'Seleccione una Opción.'])
  55.                 ->add('perfilCargo'\Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
  56.                     'class' => \App\Entity\GHPerfilCargo::class,
  57.                     'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idEmpresa$idProcesoUser$isGerente) {
  58.                         $qb $er->createQueryBuilder('c')
  59.                             ->leftJoin('c.empresaFilial''ef')
  60.                             ->leftJoin('c.cargo''car')
  61.                             ->leftJoin('c.ParProceso''pp')
  62.                             ->where('c.estado = 1')
  63.                             ->andWhere('ef.id = :idEmpresaFilial')
  64.                             ->setParameter('idEmpresaFilial'$idEmpresa)
  65.                             ->orderBy('car.nombre''ASC');
  66.                         // Si es gerente, filtra por el proceso del usuario
  67.                         if (!$isGerente) {
  68.                             $qb->andWhere('pp.id = :idProcesoUser')
  69.                                 ->setParameter('idProcesoUser'$idProcesoUser);
  70.                         }
  71.                         return $qb;
  72.                     },
  73.                     'choice_label' => 'cargo.nombre',
  74.                     'placeholder' => 'Seleccione un cargo',
  75.                 ])
  76.             ;
  77.             $event->setData($data);
  78.         }
  79.         if (isset($data['perfilCargo'])) {
  80.             $idPerfilCargo $data['perfilCargo'];
  81.             $perfilCargo $this->entityManager->getRepository(\App\Entity\GHPerfilCargo::class)->find($idPerfilCargo);
  82.             $idCargo $perfilCargo $perfilCargo->getCargo()->getId() : null;
  83.             $form
  84.                 ->add('ParTipoContrato'\Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
  85.                     'class' => \App\Entity\ParTipoContrato::class,
  86.                     'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idCargo) {
  87.                         return $er->createQueryBuilder('tc')
  88.                             ->join('tc.perfilCargo''pc')
  89.                             ->join('pc.cargo''c')
  90.                             ->where('c.id = :idCargo')
  91.                             ->setParameter('idCargo'$idCargo)
  92.                             ->orderBy('tc.nombre''ASC');
  93.                     },
  94.                     'label' => 'Tipo de Contrato',
  95.                     'placeholder' => 'Seleccione una opción',
  96.                     'attr' => ['class' => 'form-control'],
  97.                     'required' => true,
  98.                 ])
  99.                 ->add('tipoVinculacion'\Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
  100.                     'class' => \App\Entity\ParTipoVinculacion::class,
  101.                     'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($idCargo) {
  102.                         return $er->createQueryBuilder('tv')
  103.                             ->join('tv.perfilCargo''pc')
  104.                             ->join('pc.cargo''c')
  105.                             ->where('c.id = :idCargo')
  106.                             ->setParameter('idCargo'$idCargo)
  107.                             ->orderBy('tv.nombre''ASC');
  108.                     },
  109.                     'label' => 'Tipo de Vinculación',
  110.                     'placeholder' => 'Seleccione una opción',
  111.                     'attr' => ['class' => 'form-control'],
  112.                     'required' => true,
  113.                 ])
  114.             ;
  115.             function agregarCampoHTMLSiExiste($titulo$html) {
  116.                 $plano trim(strip_tags($html));
  117.                 if ($plano !== '' && strtolower($plano) !== 'null') {
  118.                     return '<strong><i class="fas fa-check-circle fa-sm text-primary text-primary-shadow mr-2"></i> ' $titulo ':</strong> <br> ' $html '<br><br>';
  119.                 }
  120.                 return '';
  121.             }
  122.             $textPerfil '';
  123.             $textPerfil .= agregarCampoHTMLSiExiste('Cargo'$perfilCargo->getCargo()?->getNombre());
  124.             $textPerfil .= agregarCampoHTMLSiExiste('Rango Salarial'$perfilCargo->getRangoSalarial());
  125.             $textPerfil .= agregarCampoHTMLSiExiste('Responsabilidades y Funciones'$perfilCargo->getResponsabilidades());
  126.             $textPerfil .= agregarCampoHTMLSiExiste('Educación'$perfilCargo->getEducacion());
  127.             $textPerfil .= agregarCampoHTMLSiExiste('Formación'$perfilCargo->getFormacionExperiencia());
  128.             $textPerfil .= agregarCampoHTMLSiExiste('Habilidades'$perfilCargo->getHabilidades());
  129.             $textPerfil .= agregarCampoHTMLSiExiste('Experiencia'$perfilCargo->getExperiencia());
  130.             $textPerfil .= agregarCampoHTMLSiExiste('Conocimientos Específicos'$perfilCargo->getConocimientosEspecificos());
  131.             $textPerfil .= agregarCampoHTMLSiExiste('Criticidad'$perfilCargo->getParCriticidad()?->getNombreCriticidad());
  132.             $data['resumenPerfilCandidato'] = $textPerfil;
  133.             //$data['rangoSalarial'] = $perfilCargo->getRangoSalarial();
  134.             $event->setData($data);
  135.         }
  136.     }
  137. }