src/FMT/Application/FormType/UserProfileType.php line 28

Open in your IDE?
  1. <?php
  2. namespace FMT\Application\FormType;
  3. use FMT\Data\Entity\User;
  4. use FMT\Data\Entity\UserProfile;
  5. use FMT\Data\Entity\UserSchool;
  6. use FMT\Domain\Repository\UserSchoolRepositoryInterface;
  7. use FMT\Application\Validator\Constraints\FmtEmail;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  11. use Symfony\Component\Form\Extension\Core\Type\TelType;
  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\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. use Symfony\Component\Validator\Constraints\Regex;
  20. /**
  21.  * Class UserProfileType
  22.  * @package FMT\Application\FormType\UserProfile
  23.  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24.  */
  25. class UserProfileType extends AbstractType
  26. {
  27.     /** @var  UserSchoolRepositoryInterface */
  28.     private $schoolRepository;
  29.     private TokenStorageInterface $tokenStorage;
  30.     /**
  31.      * UserProfileType constructor.
  32.      * @param UserSchoolRepositoryInterface $schoolRepository
  33.      */
  34.     public function __construct(UserSchoolRepositoryInterface $schoolRepositoryTokenStorageInterface $tokenStorage)
  35.     {
  36.         $this->schoolRepository $schoolRepository;
  37.         $this->tokenStorage $tokenStorage;
  38.     }
  39.     /**
  40.      * @param FormBuilderInterface $builder
  41.      * @param array $options
  42.      */
  43.     public function buildForm(FormBuilderInterface $builder, array $options)
  44.     {
  45.         $user $this->getUser();
  46.         if($user && ($user->isDonor() || $user->isIncompleteDonor())){
  47.             $otherEmailContraints = [
  48.                 new Length([
  49.                     'max' => 255,
  50.                 ])
  51.             ];
  52.         } else {
  53.             $otherEmailContraints = [
  54.                 new NotBlank(),
  55.                 new Length([
  56.                     'max' => 255,
  57.                 ]),
  58.                 //new FmtEmail(), // remove this for now - no email validation as this is just a fallback
  59.             ];
  60.         }
  61.         
  62.        
  63.         $builder
  64.             ->add('firstName'TextType::class, [
  65.                 'label' => $options['isLabelNeeded'] ? 'fmt.form.first_name' false,
  66.                 'required' => true,
  67.                 'attr' => [
  68.                     'placeholder' => 'First Name',
  69.                 ],
  70.                 'constraints' => [
  71.                     new NotBlank(),
  72.                     new Length([
  73.                         'min' => 2,
  74.                         'max' => 255,
  75.                     ]),
  76.                 ],
  77.             ])
  78.             ->add('lastName'TextType::class, [
  79.                 'label' => $options['isLabelNeeded'] ? 'fmt.form.last_name' false,
  80.                 'required' => true,
  81.                 'attr' => [
  82.                     'placeholder' => 'Last Name',
  83.                 ],
  84.                 'constraints' => [
  85.                     new NotBlank(),
  86.                     new Length([
  87.                         'min' => 2,
  88.                         'max' => 255,
  89.                     ]),
  90.                 ],
  91.             ])
  92.             ->add('phone'TelType::class, [
  93.                 'label' => $options['isLabelNeeded'] ? 'fmt.form.phone' false,
  94.                 'required' => true,
  95.                 'attr' => [
  96.                     'placeholder' => 'fmt.form.phone',
  97.                 ],
  98.                 'constraints' => [
  99.                     new NotBlank(),
  100.                     new Regex([
  101.                         'pattern' => '/[0-9]{3}-[0-9]{3}-[0-9]{4}/',
  102.                         'match' => true,
  103.                         'message' => 'fmt.sign_up.phone',
  104.                     ])
  105.                 ],
  106.             ])
  107.             ->add('school'EntityType::class, [
  108.                 'label' => $options['isLabelNeeded'] ? 'fmt.form.school' false,
  109.                 'required' => true,
  110.                 'disabled' => $options['isSchoolDisabled'],
  111.                 'class' => UserSchool::class,
  112.                 'choices' => $this->schoolRepository->getSchoolsCollection($this->getUser(), true),
  113.                 'choice_label' => 'name',
  114.             ])
  115.             ->add('snapchatId'TextType::class, [
  116.                 'required' => false,
  117.                 'label' => $options['isLabelNeeded'] ? 'fmt.user.profile.label.snapchat' false,
  118.                 'attr' => [
  119.                     'placeholder' => 'fmt.user.profile.snapchat_placeholder'
  120.                 ],
  121.                 'constraints' => [
  122.                     new Length([
  123.                         'max' => 255,
  124.                     ]),
  125.                 ],
  126.             ])
  127.             ->add('facebookMessengerId'TextType::class, [
  128.                 'required' => false,
  129.                 'label' => $options['isLabelNeeded'] ? 'fmt.user.profile.label.facebook_messenger' false,
  130.                 'attr' => [
  131.                     'placeholder' => 'fmt.user.profile.facebook_messenger_placeholder'
  132.                 ],
  133.                 'constraints' => [
  134.                     new Length([
  135.                         'max' => 255,
  136.                     ]),
  137.                 ],
  138.             ])
  139.             ->add('otherEmail'EmailType::class, [
  140.                 'required' => ($user && ($user->isDonor() || $user->isIncompleteDonor()))?false:true,
  141.                 'label' => $options['isLabelNeeded'] ? 'fmt.user.profile.label.other_email' false,
  142.                 'attr' => [
  143.                     'placeholder' => 'fmt.user.profile.other_email_placeholder'
  144.                 ],
  145.                 'constraints' => $otherEmailContraints,
  146.             ]);
  147.     }
  148.     /**
  149.      * @param OptionsResolver $resolver
  150.      */
  151.     public function configureOptions(OptionsResolver $resolver)
  152.     {
  153.         $resolver->setDefaults(
  154.             [
  155.                 'data_class' => UserProfile::class,
  156.                 'validation_groups' => ['Default''UserPortal'],
  157.                 'attr' => ['novalidate' => true],
  158.                 'isLabelNeeded' => false,
  159.                 'isSchoolDisabled' => false
  160.             ]
  161.         );
  162.         $resolver->setAllowedTypes('isLabelNeeded', ['boolean']);
  163.         $resolver->setAllowedTypes('isSchoolDisabled', ['boolean']);
  164.     }
  165.     /**
  166.      * @return UserInterface|null
  167.      */
  168.     private function getUser(): ?UserInterface
  169.     {
  170.         $user $this->tokenStorage->getToken()->getUser();
  171.         if ($user instanceof UserInterface) {
  172.             return $user;
  173.         }
  174.         return null;
  175.     }
  176. }