src/Form/AddParticipantType.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Package;
  4. use App\Entity\Participant;
  5. use Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenEntityType;
  6. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Translation\Translator;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class AddParticipantType extends AbstractType
  19. {
  20.     /**
  21.      * @var Translator
  22.      */
  23.     private $translator;
  24.     /**
  25.      * @var UrlGeneratorInterface
  26.      */
  27.     private UrlGeneratorInterface $router;
  28.     /**
  29.      * @param TranslatorInterface $translator
  30.      * @param UrlGeneratorInterface $router
  31.      */
  32.     public function __construct(
  33.         TranslatorInterface $translator,
  34.         UrlGeneratorInterface $router
  35.     )
  36.     {
  37.         $this->translator $translator;
  38.         $this->router $router;
  39.     }
  40.     public function buildForm(FormBuilderInterface $builder, array $options)
  41.     {
  42.         $builder
  43.             ->add(
  44.                 'id',
  45.                 HiddenType::class
  46.             )
  47.             ->add(
  48.                 'gdpr',
  49.                 ChoiceType::class,
  50.                 [
  51.                     'label' => 'Politica de confidentialitate',
  52.                     'help' => $this->translator->trans('general.gdpr.text'),
  53.                     'help_html' => true,
  54.                     'help_translation_parameters' => [
  55.                         '%privacy_policy%' => '<a target="_blank" href="'
  56.                             $this->router->generate('app_gdpr') . '">'
  57.                             $this->translator->trans('general.gdpr.privacy_policy') . '</a>'
  58.                     ],
  59.                     'placeholder' => $this->translator->trans('general.placeholder'),
  60.                     'choices' => [
  61.                         'general.accept' => true
  62.                     ],
  63.                     'required' => true,
  64.                     'mapped' => false
  65.                 ]
  66.             )
  67.             ->add(
  68.                 'name',
  69.                 TextType::class,
  70.                 [
  71.                     'label' => $this->translator->trans('participant.label.name'),
  72.                     'required' => true,
  73.                     'help' => $this->translator->trans('participant.help.name').' '.$this->translator->trans('general.mandatory'),
  74.                 ]
  75.             )
  76.             ->add(
  77.                 'company',
  78.                 TextType::class,
  79.                 [
  80.                     'label' => $this->translator->trans('participant.label.company'),
  81.                     'required' => true,
  82.                     'help' => $this->translator->trans('participant.help.company').' '.$this->translator->trans('general.mandatory'),
  83.                 ]
  84.             )
  85.             ->add(
  86.                 'vatId',
  87.                 TextType::class,
  88.                 [
  89.                     'label' => $this->translator->trans('participant.label.vatId'),
  90.                     'required' => true,
  91.                     'help' => $this->translator->trans('participant.help.vatId').' '.$this->translator->trans('general.mandatory')
  92.                 ]
  93.             )
  94.             ->add(
  95.                 'email',
  96.                 EmailType::class,
  97.                 [
  98.                     'label' => $this->translator->trans('participant.label.email'),
  99.                     'required' => false,
  100.                     'help' => $this->translator->trans('participant.help.email'),
  101.                 ]
  102.             )
  103.             ->add(
  104.                 'phone',
  105.                 TextType::class,
  106.                 [
  107.                     'label' => $this->translator->trans('participant.label.phone'),
  108.                     'required' => false,
  109.                     'help' => $this->translator->trans('participant.help.phone'),
  110.                 ]
  111.             );
  112.         if (!$options['isExposed']) {
  113.             $builder->add(
  114.                     'status',
  115.                     ChoiceType::class,
  116.                     [
  117.                         'label' => 'Status badge',
  118.                         'choices' => [
  119.                             $this->translator->trans('participant.badgeStatus.unprinted') => Participant::STATUS_ENABLED_NOT_PRINTED,
  120.                             $this->translator->trans('participant.badgeStatus.printed') => Participant::STATUS_ENABLED_PRINTED,
  121.                         ],
  122.                         'help' => $this->translator->trans('participant.help.badge'),
  123.                     ]
  124.                 )
  125.                 ->add('package',
  126.                     EntityType::class,
  127.                     [
  128.                         'label' => $this->translator->trans('participant.table.package'),
  129.                         'class' => Package::class,
  130.                         'data' => $options['defaultPackage'],
  131.                         'required' => true,
  132.                         'help' => $this->translator->trans('participant.help.package').' '.$this->translator->trans('general.mandatory'),
  133.                     ],
  134.                 );
  135.         } else {
  136.             $builder->add('package',
  137.                 HiddenEntityType::class,
  138.                 [
  139.                     'label' => $this->translator->trans('participant.table.package'),
  140.                     'class' => Package::class,
  141.                     'data' => $options['defaultPackage'],
  142.                     'required' => true,
  143.                     'help' => $this->translator->trans('participant.help.package').' '.$this->translator->trans('general.mandatory'),
  144.                 ],
  145.             );
  146.         }
  147.         $builder
  148.             ->add(
  149.                 'save',
  150.                 SubmitType::class,
  151.                 [
  152.                     'label' => $this->translator->trans('general.save'),
  153.                 ]
  154.             );
  155.     }
  156.     public function configureOptions(OptionsResolver $resolver)
  157.     {
  158.         $resolver->setDefaults(
  159.             [
  160.                 'data_class' => Participant::class,
  161.                 'defaultPackage' => null,
  162.                 'isExposed' => false
  163.             ]
  164.         );
  165.         $resolver->setAllowedTypes('defaultPackage'Package::class);
  166.     }
  167. }