src\Form\SubEmpresaType.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\SubEmpresa;
  4. use App\Entity\Empresa;
  5. use App\Entity\User;
  6. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. class SubEmpresaType extends AbstractType
  11. {
  12.     public function buildForm(FormBuilderInterface $builder, array $options): void
  13.     {
  14.         $builder
  15.             ->add('nome'null, [
  16.                 'label'    => 'Nome',
  17.                 'required' => false,
  18.                 'attr'     => ['class' => 'form-control''style' => 'margin:5px 0;'],
  19.             ])
  20.             ->add('descricao'null, [
  21.                 'label'    => 'Descrição',
  22.                 'required' => false,
  23.                 'attr'     => ['class' => 'form-control''style' => 'margin:5px 0;'],
  24.             ])
  25.             ->add('empresa'EntityType::class, [
  26.                 'class'        => Empresa::class,
  27.                 'choice_label' => 'nome',
  28.                 'placeholder'  => 'Selecione a empresa',
  29.                 'label'        => 'Empresa',
  30.                 'attr'         => ['class' => 'form-control''style' => 'margin:5px 0;'],
  31.             ])
  32.             ->add('users'EntityType::class, [
  33.                 'class'         => User::class,
  34.                 'label'         => 'Aprovadores',
  35.                 'multiple'      => true,
  36.                 'expanded'      => true,
  37.                 'by_reference'  => false// importante para ManyToMany
  38.                 'choices'       => $options['usuarios_permitidos'], // lista já filtrada no controller
  39.                 'choice_label'  => function (User $u) {
  40.                     $nome  method_exists($u'getNome') ? $u->getNome() : null;
  41.                     $roles $u->getRoles() ?? [];
  42.                     // escolhe um role "representativo" por prioridade
  43.                     $pick null;
  44.                     if (in_array('ROLE_SUPER'$rolestrue)) {
  45.                         $pick 'ROLE_SUPER';
  46.                     }
  47.                     if (!$pick) {
  48.                         foreach ($roles as $r) {
  49.                             if (str_starts_with($r'ROLE_ADMINISTRADOR')) {
  50.                                 $pick $r;
  51.                                 break;
  52.                             }
  53.                         }
  54.                     }
  55.                     if (!$pick) {
  56.                         foreach ($roles as $r) {
  57.                             if (str_starts_with($r'ROLE_APROVADOR')) {
  58.                                 $pick $r;
  59.                                 break;
  60.                             }
  61.                         }
  62.                     }
  63.                     if (!$pick && $roles) {
  64.                         $pick $roles[0]; // fallback
  65.                     }
  66.                     // transforma "ROLE_APROVADOR_ADMINISTRATIVO" -> "Aprovador Administrativo"
  67.                     $pretty 'Sem Papel';
  68.                     if ($pick) {
  69.                         $clean  preg_replace('/^ROLE_/'''$pick);
  70.                         $parts  explode('_'$clean);
  71.                         $pretty implode(' 'array_map(
  72.                             fn($p) => mb_convert_case($pMB_CASE_TITLE'UTF-8'),
  73.                             array_map('strtolower'$parts)
  74.                         ));
  75.                     }
  76.                     // Se quiser mostrar só o papel, retorne apenas $pretty
  77.                     return $nome sprintf('%s (%s)'$nome$pretty) : $pretty;
  78.                 },
  79.                 'attr'          => [],
  80.                 'choice_attr' => function () {
  81.                     static $index 0;
  82.                     $index++;
  83.                     return [
  84.                         'style' => $index === 1
  85.                             'margin-right:6px;'                       // 1º item: sem margin-left
  86.                             'margin-right:6px; margin-left:16px;'     // demais: com margin-left
  87.                     ];
  88.                 },
  89.                 'required'      => false,
  90.             ]);
  91.     }
  92.     public function configureOptions(OptionsResolver $resolver): void
  93.     {
  94.         $resolver->setDefaults([
  95.             'data_class'          => SubEmpresa::class,
  96.             'usuarios_permitidos' => [], // obrigatório: será preenchido no controller
  97.         ]);
  98.     }
  99. }