src/Form/ResourceRegisterType.php line 157

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Accent;
  4. use App\Entity\AccentMapping;
  5. use App\Entity\Country;
  6. use App\Entity\Currency;
  7. use App\Entity\Resource;
  8. use App\Entity\ResourceSource;
  9. use App\Entity\ScenarioAccentMapping;
  10. use App\Repository\AccentMappingRepository;
  11. use App\Repository\ScenarioAccentMappingRepository;
  12. use Doctrine\ORM\EntityRepository;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  18. use Symfony\Component\Form\Extension\Core\Type\FileType;
  19. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  20. use Symfony\Component\Form\Extension\Core\Type\TelType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextType;
  22. use Symfony\Component\Form\FormBuilderInterface;
  23. use Symfony\Component\OptionsResolver\OptionsResolver;
  24. use Symfony\Component\Validator\Constraints\File;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. class ResourceRegisterType extends AbstractType
  27. {
  28.     private $amRepo;
  29.     private $samRepo;
  30.     private $translator;
  31.     public function __construct(AccentMappingRepository $amRepoScenarioAccentMappingRepository $samRepoTranslatorInterface $translator)
  32.     {
  33.         $this->amRepo $amRepo;
  34.         $this->samRepo $samRepo;
  35.         $this->translator $translator;
  36.     }
  37.     public function configureOptions(OptionsResolver $resolver)
  38.     {
  39.         $resolver->setDefaults([
  40.             'data_class' => Resource::class,
  41.             'translation_domain' => 'form',
  42.         ]);
  43.     }
  44.     public function buildForm(FormBuilderInterface $builder, array $options)
  45.     {
  46.         $translator $this->translator;
  47.         $activeMappings $this->amRepo->findActive();
  48.         $scenarioActiveMappings $this->samRepo->findActive();
  49.         $accents = [];
  50.         $ids = [];
  51.         /**
  52.          * @var AccentMapping $mapping
  53.          */
  54.         foreach ($activeMappings as $mapping) {
  55.             if (!in_array($mapping->getAccent()->getId(), $ids)) {
  56.                 $ids[] = $mapping->getAccent()->getId();
  57.                 $accents[] = $mapping->getAccent();
  58.             }
  59.         }
  60.         /**
  61.          * @var ScenarioAccentMapping $mapping
  62.          */
  63.         foreach ($scenarioActiveMappings as $mapping) {
  64.             if (!in_array($mapping->getAccent()->getId(), $ids)) {
  65.                 $ids[] = $mapping->getAccent()->getId();
  66.                 $accents[] = $mapping->getAccent();
  67.             }
  68.         }
  69.         $builder
  70.             ->add('firstname'TextType::class, [
  71.                 'required' => true,
  72.                 'label' => 'Firstname',
  73.             ])
  74.             ->add('lastname'TextType::class, [
  75.                 'required' => true,
  76.                 'label' => 'Lastname',
  77.             ])
  78.             ->add('email'EmailType::class, [
  79.                 'required' => true,
  80.                 'label' => 'E-mail',
  81.             ])
  82.             ->add('phone'TelType::class, [
  83.                 'required' => true,
  84.                 'label' => 'Phone',
  85.             ])
  86.             ->add('gender'ChoiceType::class, [
  87.                 'required' => true,
  88.                 'choices' => array_flip(Resource::RESOURCE_GENDER),
  89.                 'label' => 'Gender',
  90.                 'translation_domain' => 'misc',
  91.                 'attr' =>  [
  92.                     'class' => 'custom',
  93.                 ]
  94.             ])
  95.             ->add('age'ChoiceType::class, [
  96.                 'required' => true,
  97.                 'choices' => array_flip(Resource::RESOURCE_AGE),
  98.                 'label' => 'Age',
  99.                 'translation_domain' => 'misc',
  100.                 'attr' =>  [
  101.                     'class' => 'custom',
  102.                 ]
  103.             ])
  104.             ->add('ethnicGroup'ChoiceType::class, [
  105.                 'required' => false,
  106.                 'choices' => array_flip(Resource::RESOURCE_ETHNICITY),
  107.                 'label' => 'Etchnicity',
  108.                 'translation_domain' => 'misc',
  109.             ])
  110.             ->add('education'ChoiceType::class, [
  111.                 'required' => false,
  112.                 'choices' => array_flip(Resource::RESOURCE_EDUCATION),
  113.                 'label' => 'Study level',
  114.                 'translation_domain' => 'misc',
  115.             ])
  116.             ->add('parentsEducation'ChoiceType::class, [
  117.                 'required' => false,
  118.                 'choices' => array_flip(Resource::RESOURCE_EDUCATION),
  119.                 'label' => 'Parents Study level',
  120.                 'translation_domain' => 'misc',
  121.             ])
  122.             ->add('country'EntityType::class, [
  123.                 'required' => true,
  124.                 'class' => Country::class,
  125.                 'choice_label' => 'name',
  126.                 'label' => 'Country',
  127.                 'translation_domain' => 'countries',
  128.                 'choice_translation_domain' => 'countries',
  129.                 'attr' =>  [
  130.                     'class' => 'custom',
  131.                 ]
  132.             ])
  133.             ->add('currency'EntityType::class, [
  134.                 'required' => true,
  135.                 'class' => Currency::class,
  136.                 'choice_label' => 'display',
  137.                 'label' => 'Currency',
  138.                 'translation_domain' => 'misc',
  139.                 'choice_translation_domain' => 'misc',
  140.                 'attr' => [
  141.                     'class' => 'custom',
  142.                 ],
  143.             ])
  144.             ->add('accent'EntityType::class, [
  145.                 'required' => true,
  146.                 'class' => Accent::class,
  147.                 'choices' => $accents,
  148.                 'choice_label' => function(Accent $accent) use ($translator) {
  149.                     return implode(' - ', [
  150.                         $translator->trans($accent->getLang(), [], 'languages'),
  151.                         $translator->trans($accent->getCountry(), [], 'countries'),
  152.                         $translator->trans($accent->getDisplay(), [], 'accents'),
  153.                     ]);
  154.                 },
  155.                 'label' => 'Geographical area / Accent',
  156.                 'translation_domain' => 'accents',
  157.                 'choice_translation_domain' => false,
  158.                 'placeholder' => '',
  159.                 'attr' =>  [
  160.                     'class' => 'custom',
  161.                 ]
  162.             ])
  163.             ->add('source'EntityType::class, [
  164.                 'required' => true,
  165.                 'empty_data' => 'other',
  166.                 'label' => 'How did you find us?',
  167.                 'class' => ResourceSource::class,
  168.                 'choice_label' => 'display',
  169.                 'translation_domain' => 'resource_sources',
  170.                 'choice_translation_domain' => 'resource_sources',
  171.                 'query_builder' => function (EntityRepository $repo) {
  172.                     return $repo->createQueryBuilder('rs')->addOrderBy('rs.sequence''ASC')->addOrderBy('rs.display''ASC');
  173.                 },
  174.             ])
  175.             ->add('sample_file'FileType::class, [
  176.                 'required' => true,
  177.                 'label' => false,
  178.                 'mapped' => false,
  179.                 'constraints' => [
  180.                     new File([
  181.                         'mimeTypes' => [
  182.                             'audio/*',
  183.                             'video/ogg',
  184.                         ],
  185.                         'mimeTypesMessage' => 'Please upload an audio file',
  186.                     ]),
  187.                 ],
  188.             ])
  189.             ->add('sponsor_code'TextType::class, [
  190.                 'required' => false,
  191.                 'mapped' => false,
  192.             ])
  193.             ->add('tra_acceptance'CheckboxType::class, [
  194.                 'required' => true,
  195.                 'mapped' => false,
  196.             ])
  197.             ->add('submit'SubmitType::class, [
  198.                 'label' => 'Submit',
  199.             ])
  200.         ;
  201.     }
  202. }