src/Form/RegistrationFormType.php line 151

Open in your IDE?
  1. <?php
  2. /*
  3. * Ce fichier est la propriété de l'association (c) Projets Métiers
  4. *
  5. * (c) crée par Jean-Marc CATALA <jeanmmarccatala@gmail.com>
  6. *
  7. */
  8. namespace App\Form;
  9. use App\Entity\Institution;
  10. use App\Entity\User;
  11. use App\Repository\UserRepository;
  12. use Doctrine\ORM\EntityRepository;
  13. use phpDocumentor\Reflection\Types\Null_;
  14. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  19. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  20. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextType;
  22. use Symfony\Component\Form\FormBuilderInterface;
  23. use Symfony\Component\Form\FormError;
  24. use Symfony\Component\Form\FormEvent;
  25. use Symfony\Component\Form\FormEvents;
  26. use Symfony\Component\OptionsResolver\OptionsResolver;
  27. use Symfony\Component\Validator\Constraints\IsTrue;
  28. use Symfony\Component\Validator\Constraints\Length;
  29. use Symfony\Component\Validator\Constraints\NotBlank;
  30. use function date_date_set;
  31. use function dd;
  32. use function in_array;
  33. class RegistrationFormType extends AbstractType
  34. {
  35. /**
  36. * @var UserRepository
  37. */
  38. private $userRepository;
  39. public function __construct(UserRepository $userRepository)
  40. {
  41. $this->userRepository = $userRepository;
  42. }
  43. public function buildForm(FormBuilderInterface $builder, array $options)
  44. {
  45. $allowedFields = [];
  46. $role = $options['which_role'];
  47. if ($role === User::ROLE_COLLEGE) {
  48. $allowedFields = ['cp', 'institution', 'classe', 'expert', 'emailParent', 'profPrincipal'];
  49. }
  50. if ($role === User::ROLE_LYCEE) {
  51. $allowedFields = ['cp', 'institution', 'classe', 'expert', 'emailParent', 'profPrincipal'];
  52. }
  53. if ($role === User::ROLE_ADULTE) {
  54. $allowedFields = ['cp', 'institution', 'expert', 'conseille'];
  55. }
  56. if ($role === User::ROLE_PROF) {
  57. $allowedFields = ['cp', 'institution', 'expert'];
  58. }
  59. if ($role === User::ROLE_PARENT) {
  60. $allowedFields = ['emailEnfant'];
  61. }
  62. if ($role === User::ROLE_CONSEILLER) {
  63. $allowedFields = ['cp', 'institution', 'expert'];
  64. }
  65. if ($role === User::ROLE_MENTOR) {
  66. $allowedFields = ['cp', 'institution', 'expert'];
  67. }
  68. $builder
  69. ->add('email')
  70. ->add('name', TextType::class, array('label' => 'Prénom'))
  71. ->add('firstName', TextType::class, array('label' => 'Nom'))
  72. ->add('agreeTerms', CheckboxType::class, [
  73. 'mapped' => false,
  74. 'label' => "j'accepte les condition générales d'utilisation",
  75. 'label_attr' => ["class" => 'checkbox-custom'],
  76. 'constraints' => [
  77. new IsTrue([
  78. 'message' => 'vous devez accepter notre charte.',
  79. ]),
  80. ],
  81. ])
  82. ->add('plainPassword', RepeatedType::class, [
  83. // instead of being set onto the object directly,
  84. // this is read and encoded in the controller
  85. 'mapped' => false,
  86. 'type' => PasswordType::class,
  87. 'invalid_message' => 'Les deux mots de passe doivent être identiques.',
  88. 'first_options' => ['label' => 'Mot de passe'],
  89. 'second_options' => ['label' => 'Répéter le mot de passe'],
  90. 'constraints' => [
  91. new NotBlank([
  92. 'message' => 'Entrez votre mot de passe',
  93. ]),
  94. new Length([
  95. 'min' => 3,
  96. 'minMessage' => 'Le mot de passe doit dépasser {{ limit }} caractères',
  97. // max length allowed by Symfony for security reasons
  98. 'max' => 4096,
  99. ]),
  100. ],
  101. ]);
  102. if (in_array('cp', $allowedFields)) {
  103. $builder->add('cp', TextType::class, [
  104. 'label' => 'Code postal de l’établissement',
  105. 'mapped' => false,
  106. 'required' => false
  107. ]);
  108. }
  109. if (in_array('profPrincipal', $allowedFields)) {
  110. $builder
  111. ->add('profPrincipal', EntityType::class, [
  112. 'class' => User::class,
  113. 'label' => 'Professeur référent',
  114. 'required' => false,
  115. 'query_builder' => function (EntityRepository $er) {
  116. $build = $er->createQueryBuilder('u')
  117. ->andWhere('u.roles LIKE :role')
  118. ->setParameter('role', "%" . User::ROLE_PROF . "%");
  119. //dd($build);
  120. return $build;
  121. },
  122. 'choice_label' => 'fullName',
  123. 'placeholder' => 'Choisir',
  124. 'choice_attr' => function ($choice, $key, $value) {
  125. /*
  126. $listeProf = $choice->getInstitution()->getId();
  127. $test = ['data-institution-id' => $listeProf ,
  128. 'class'=>'d-none'];
  129. //dd($test);
  130. */
  131. $listeProf = $choice->getInstitution()->getId();
  132. //dd($listeProf);
  133. return ['data-institution-id' => $listeProf,
  134. 'class' => 'd-none'];
  135. },
  136. ]);
  137. }
  138. if (in_array('conseille', $allowedFields)) {
  139. $builder
  140. ->add('conseille', EntityType::class, [
  141. 'class' => User::class,
  142. 'label' => 'Conseiller',
  143. 'required' => false,
  144. 'query_builder' => function (EntityRepository $er) {
  145. return $er->createQueryBuilder('u')
  146. ->andWhere('u.roles LIKE :role')
  147. ->setParameter('role', "%" . User::ROLE_CONSEILLER . "%");
  148. },
  149. 'choice_label' => 'fullName',
  150. 'placeholder' => 'Choisir',
  151. 'choice_attr' => function ($choice, $key, $value) {
  152. //dd($choice, $key, $value);
  153. $listeConseille = $choice->getInstitution()->getId();
  154. //dd($listeConseille);
  155. return ['data-institution-id' => $choice->getInstitution()->getId(),
  156. 'class' => 'd-none'];
  157. },
  158. ]);
  159. }
  160. if (in_array('institution', $allowedFields)) {
  161. $builder->add('institution', EntityType::class, [
  162. 'label' => 'Institution',
  163. 'help' => ' ',
  164. 'help_html' => true,
  165. 'mapped' => false,
  166. 'required' => false,
  167. 'class' => Institution::class,
  168. 'query_builder' => function (EntityRepository $er) use ($role) {
  169. return $er->createQueryBuilder('i')
  170. ->andWhere('i.type IN (:type)')
  171. ->setParameter('type', Institution::convertTypeStringToConst($role));
  172. },
  173. 'choice_label' => 'title',
  174. 'choice_attr' => function ($choice, $key, $value) {
  175. return ['data-cp' => $choice->getCp(),
  176. 'data-expert' => $choice->getExpert(),
  177. 'class' => 'd-none'];
  178. },
  179. 'placeholder' => 'Choisir',
  180. 'row_attr' => ['class' => 'd-none'],
  181. 'attr' => ['max' => 5]
  182. ]);
  183. }
  184. if (in_array('expert', $allowedFields)) {
  185. $builder->add('expert', TextType::class, [
  186. 'label' => 'Votre code référent (expert métiers, référent PEP, référent FCPE…)',
  187. 'mapped' => false,
  188. 'required' => false,
  189. 'row_attr' => ['class' => 'd-none']
  190. ]);
  191. }
  192. if (in_array('classe', $allowedFields)) {
  193. if ($role === User::ROLE_COLLEGE) {
  194. $builder->add('classe', ChoiceType::class, [
  195. 'placeholder' => 'Choisir',
  196. "attr" => [
  197. "class" => "registerForm",
  198. ],
  199. 'choices' => [
  200. '6ème' => '6ème',
  201. '5ème' => '5ème',
  202. '4ème' => '4ème',
  203. '3ème' => '3ème',
  204. ],
  205. 'choice_attr' => [
  206. '6ème' => ['class' => 'choixMultiple'],
  207. '5ème' => ['class' => 'choixMultiple'],
  208. '4ème' => ['class' => 'choixMultiple'],
  209. '3ème' => ['class' => 'choixMultiple'],
  210. ],
  211. ]);
  212. }
  213. if ($role === User::ROLE_LYCEE) {
  214. $builder->add('classe', ChoiceType::class, [
  215. 'placeholder' => 'Choisir',
  216. "attr" => [
  217. "class" => "registerForm",
  218. ],
  219. 'choices' => [
  220. '2nde' => '2nde',
  221. '1ère' => '1ère',
  222. 'Terminale' => 'Terminale',
  223. ],
  224. 'choice_attr' => [
  225. '2nde' => ['class' => 'choixMultiple'],
  226. '1ère' => ['class' => 'choixMultiple'],
  227. 'Terminale' => ['class' => 'choixMultiple'],
  228. ],
  229. ]);
  230. }
  231. }
  232. if ($role === User::ROLE_LYCEE) {
  233. $label1 = "Email parent 1 (ce champ n'est pas obligatoire)";
  234. $required = false;
  235. }
  236. if ($role === User::ROLE_COLLEGE) {
  237. $label1 = "Email parent 1";
  238. $required = true;
  239. }
  240. if (in_array('emailParent', $allowedFields)) {
  241. $builder
  242. ->add('emailParent1', EmailType::class, [
  243. 'label' => $label1,
  244. 'required' => $required,
  245. ])
  246. ->add('emailParent2', EmailType::class, [
  247. 'label' => "Email parent 2, (ce champ n'est pas obligatoire)",
  248. 'required' => false,
  249. ]);
  250. }
  251. if (in_array('emailEnfant', $allowedFields)) {
  252. $builder
  253. ->add('emailEnfant', EmailType::class, [
  254. 'label' => 'Email étudiant',
  255. 'required' => true,
  256. 'mapped' => false,
  257. ]);
  258. }
  259. $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options) {
  260. $form = $event->getForm();
  261. if ($form->has('emailEnfant')) {
  262. $form->get('emailEnfant')->setData($options['emailEnfant']);
  263. }
  264. });
  265. $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  266. $form = $event->getForm();
  267. $user = $event->getData();
  268. /** @var User $user */
  269. if ($form->has('expert')) {
  270. $expert = $form->get('expert')->getData();
  271. $cp = $form->get('cp')->getData();
  272. $institution = $form->get('institution')->getData();
  273. if ($institution->getExpert() !== $expert or $institution->getCp() !== $cp) {
  274. $form->get('cp')->addError(new FormError('Le code postal et le référent ne correspondent pas'));
  275. }
  276. }
  277. if ($user->hasRole(User::ROLE_PARENT)) {
  278. $enfant = $this->userRepository->findChildFromParentEmail($form->get('emailEnfant')->getData(), $user->getEmail());
  279. if ($enfant) {
  280. $user->addEnfant($enfant);
  281. } else {
  282. $form->get('emailEnfant')->addError(new FormError("Nous n'avons pas trouvé d'email correspondant"));
  283. }
  284. }
  285. });
  286. }
  287. public function configureOptions(OptionsResolver $resolver)
  288. {
  289. $resolver->setDefaults([
  290. 'data_class' => User::class,
  291. 'which_role' => null,
  292. 'emailEnfant' => null
  293. ]);
  294. }
  295. }