src/Controller/FrontendBundle/Registration/RegisterTeacherController.php line 128

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontendBundle\Registration;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use App\BackendBundle\Mailer\Target\Register\RegisterConfirmation;
  10. use App\BackendBundle\Helper\MailerHelper;
  11. use App\BackendBundle\Helper\TeacherHelper;
  12. use App\BackendBundle\Helper\SchoolHelper;
  13. use App\BackendBundle\Helper\RegisterHelper;
  14. use App\BackendBundle\Helper\SecurityHelper;
  15. use App\BackendBundle\Helper\SiteTitleHelper;
  16. use App\BackendBundle\Helper\UserHelper;
  17. use App\BackendBundle\Helper\ValidationHelper;
  18. use App\Entity\User;
  19. class RegisterTeacherController extends AbstractController {
  20.     private $teacherHelper;
  21.     private $mailerHelper;
  22.     private $registerHelper;
  23.     private $userHelper;
  24.     private $siteTitleHelper;
  25.     private $validationHelper;
  26.     private $securityHelper;
  27.     private $schoolHelper;
  28.     public function __construct(SiteTitleHelper $sitetitlehelperTeacherHelper $teacherhelperMailerHelper $mailerhelperRegisterHelper $registerhelperUserHelper $userhelperValidationHelper $validationhelperSecurityHelper $securityhelperSchoolHelper $schoolhelper) {
  29.         $this->siteTitleHelper $sitetitlehelper;
  30.         $this->mailerHelper $mailerhelper;
  31.         $this->registerHelper $registerhelper;
  32.         $this->userHelper $userhelper;
  33.         $this->validationHelper $validationhelper;
  34.         $this->securityHelper $securityhelper;
  35.         $this->teacherHelper $teacherhelper;
  36.         $this->schoolHelper $schoolhelper;
  37.     }
  38.     /**
  39.      * @return \Symfony\Component\HttpFoundation\Response
  40.      * @Route("/register/lehrer/new_director", name="teacher_register_step_director", defaults={"title": "Registrierung als Schulleitung", "description": "Registrieren Sie sich jetzt als Schulleitung und ermöglichen Sie Ihren Lehrpersonen und Schüler*innen, mit berufsreise.at zu arbeiten."}) 
  41.      */
  42.     public function directorRegisterAction(Request $request) {
  43.         $this->siteTitleHelper->setTitleDescription($request);
  44.         if ($request->isXmlHttpRequest()) {
  45.             $data = array();
  46.             $data['status'] = 'ok';
  47.             $this->validateDirectorData($data$request);
  48.             return new JsonResponse($data);
  49.         } else {
  50.             $validationData $this->validationHelper->getFormValidationData('register');
  51.             return $this->render('@frontend/registration/register_director.html.twig', array(
  52.                         'validationData' => $validationData
  53.             ));
  54.         }
  55.     }
  56.     private function validateDirectorData(&$dataRequest $request) {
  57.         $loginData $request->request->get('login');
  58.         $personData $request->request->get('person');
  59.         $schoolData $request->request->get('school');
  60.         $isFormValid $this->validationHelper->isFormValid('register'$request);
  61.         if (!$isFormValid) {
  62.             $data['status'] = 'error';
  63.             $data['message'] = 'Fehler beim Absenden der Formulardaten';
  64.             return;
  65.         }
  66.         $schoolID $schoolData['schoolid'];
  67.         $school $this->schoolHelper->getSchoolById($schoolID);
  68.         if (empty($school)) {
  69.             $data['status'] = 'error';
  70.             $data['message'] = 'Die Schule wurde nicht gefunden';
  71.             return;
  72.         }
  73.         $eMail $loginData['email'];
  74.         $directorMail $this->schoolHelper->getSchoolDirectorMail($school);
  75.         // check if the typed director mail matches the selected
  76.         // school director mail
  77.         if (isset($school) && $directorMail != $eMail) {
  78.             $data['status'] = 'error';
  79.             $data['message'] = 'Bitte tragen Sie im Feld „E-Mail-Adresse“, die im TIBS-System hinterlegte E-Mail-Adresse der Schule ein.';
  80.             return;
  81.         }
  82.         // at last check if the user is already confirmed
  83.         $user $this->registerHelper->getUserByMail($eMail);
  84.         if (empty($user)) {
  85.             $newUser $this->saveDirectorDataInDb($loginData$personData$school);
  86.             $this->sendConfirmationMail($newUser);
  87.             $data['url'] = $this->generateUrl('teacher_register_step_sendmail_confirmation');
  88.             return;
  89.         }
  90.         if (!$this->registerHelper->isUserConfirmed($user)) {
  91.             $resetActivationLinkURL $this->generateUrl('teacher_register_step_sendmail_confirmation_reset');
  92.             $data['status'] = 'error';
  93.             $data['message'] = 'Sie sind bereits auf berufsreise.at registriert, haben jedoch Ihre Registrierungsbestätigung per E-Mail noch nicht durchgeführt. '
  94.                     'Klicken Sie <a style="color:#000; text-decoration:underline;" href="' $resetActivationLinkURL '">hier</a>, '
  95.                     'wenn wir Ihnen die E-Mail mit der Bestätigung nochmals senden sollen.';
  96.             $session $this->get('session');
  97.             $session->set('resetActivationLinkUserID'$user->getId());
  98.         } else {
  99.             $data['status'] = 'error';
  100.             $data['message'] = 'Zu den eingegebenen Daten existiert bereits ein Benutzer';
  101.         }
  102.     }
  103.     /**
  104.      * @return \Symfony\Component\HttpFoundation\Response
  105.      * @Route("/register/lehrer/new", name="teacher_register_step_normal", defaults={"title": "Registrierung als Lehrperson", "description": "Registrieren Sie sich jetzt als Lehrperson und buchen Sie Angebote der Berufsorientierung oder sammeln zusammen mit Ihren Schüler*innen Punkte im Klassenprofil."}) 
  106.      */
  107.     public function teacherRegisterAction(Request $request) {
  108.         $this->siteTitleHelper->setTitleDescription($request);
  109.         if ($request->isXmlHttpRequest()) {
  110.             $data = array();
  111.             $data['status'] = 'ok';
  112.             $this->validateTeacherData($data$request);
  113.             return new JsonResponse($data);
  114.         } else {
  115.             $validationData $this->validationHelper->getFormValidationData('register');
  116.             return $this->render('@frontend/registration/register_teacher.html.twig', array(
  117.                         'validationData' => $validationData
  118.             ));
  119.         }
  120.     }
  121.     private function validateTeacherData(&$dataRequest $request) {
  122.         $loginData $request->request->get('login');
  123.         $personData $request->request->get('person');
  124.         $schoolData $request->request->get('school');
  125.         $isFormValid $this->validationHelper->isFormValid('register'$request);
  126.         if (!$isFormValid) {
  127.             $data['status'] = 'error';
  128.             $data['message'] = 'Fehler beim Absenden der Formulardaten';
  129.             return;
  130.         }
  131.         $user $this->registerHelper->getUserByMail($loginData['email']);
  132.         if (empty($user)) {
  133.             $newUser $this->saveTeacherDataInDb($loginData$personData$schoolData);
  134.             $this->sendConfirmationMail($newUser);
  135.             $data['url'] = $this->generateUrl('teacher_register_step_sendmail_confirmation');
  136.         } else {
  137.             $data['status'] = 'error';
  138.             $data['message'] = 'Zu den eingegebenen Daten existiert bereits ein Benutzer';
  139.         }
  140.     }
  141.     /**
  142.      * @return \Symfony\Component\HttpFoundation\Response
  143.      * @Route("/register/lehrer/success", name="teacher_register_step_sendmail_confirmation") 
  144.      */
  145.     public function registerSuccessAction() {
  146.         return $this->render('@frontend/registration/mailConfirmation/link_sent.html.twig');
  147.     }
  148.     /**
  149.      * @return \Symfony\Component\HttpFoundation\Response
  150.      * @Route("/register/lehrer/mail_confirmation_reset", name="teacher_register_step_sendmail_confirmation_reset") 
  151.      */
  152.     public function resetActivationLinkAction() {
  153.         $session $this->get('session');
  154.         if (!is_int($session->get('resetActivationLinkUserID'))) {
  155.             return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
  156.         }
  157.         $user $this->registerHelper->getUserById($session->get('resetActivationLinkUserID'));
  158.         if (!$this->registerHelper->resetUserActivationLinkCode($user)) {
  159.             return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
  160.         }
  161.         // when the code has been resetted, send another mail
  162.         $this->sendConfirmationMail($user);
  163.         $session->remove('resetActivationLinkUserID');
  164.         return $this->render('@frontend/registration/mailConfirmation/link_confirm_reset.html.twig');
  165.     }
  166.     /**
  167.      * @return \Symfony\Component\HttpFoundation\Response
  168.      * @Route("/register/lehrer/activate", name="teacher_register_step_mail_confirmation", defaults={"title": "Registrierung abgeschlossen", "description": "Vielen Dank für Ihre Registrierung auf berufsreise.at."}) 
  169.      */
  170.     public function confirmActivationLinkAction(Request $request) {
  171.         $this->siteTitleHelper->setTitleDescription($request);
  172.         $confirmationCode $request->query->get('cr');
  173.         if (!empty($confirmationCode)) {
  174.             $mailIsConfirmed $this->registerHelper->activateUser($confirmationCode);
  175.             if ($mailIsConfirmed) {
  176.                 $loginURL $this->generateUrl('security_login');
  177.                 return $this->render('@frontend/registration/mailConfirmation/link_confirmed.html.twig', array(
  178.                             'loginURL' => $loginURL,
  179.                 ));
  180.             }
  181.         }
  182.         return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
  183.     }
  184.     private function saveDirectorDataInDb($loginData$personData$school) {
  185.         $em $this->getDoctrine()->getManager();
  186.         // create the User Object for the Teacher
  187.         $userObj $this->registerHelper->createUser($loginData['email'], $loginData['password'], $loginData['email'], array('ROLE_USER''ROLE_DIRECTOR'), false);
  188.         // convert the Birthdate and create the Person Object
  189.         $personObj $this->registerHelper->createPerson($personData['firstname'], $personData['lastname'], $personData['gender'], nullnull);
  190.         $em->persist($userObj);
  191.         $em->persist($personObj);
  192.         $this->userHelper->createUserData($userObj$personObjnullnull);
  193.         // add all school classes to the teacher, that have been entered in the Code Field
  194.         // here the SchoolClassMappings get persisted but not flushed 
  195.         $this->teacherHelper->addSchoolToDirector($userObj$school);
  196.         return $userObj;
  197.     }
  198.     private function saveTeacherDataInDb($loginData$personData$schoolData) {
  199.         $em $this->getDoctrine()->getManager();
  200.         // create the User Object for the Teacher
  201.         $userObj $this->registerHelper->createUser($loginData['email'], $loginData['password'], $loginData['email'], array('ROLE_USER''ROLE_TEACHER'), false);
  202.         // convert the Birthdate and create the Person Object
  203.         $personObj $this->registerHelper->createPerson($personData['firstname'], $personData['lastname'], $personData['gender'], nullnull);
  204.         $em->persist($userObj);
  205.         $em->persist($personObj);
  206.         $this->userHelper->createUserData($userObj$personObjnullnull);
  207.         $em->flush();
  208.         $invitations $this->teacherHelper->getSchoolTeacherInvitationsByMail($loginData['email']);
  209.         foreach ($invitations as $invitation) {
  210.             $this->schoolHelper->createNewSchoolTeacherMapping($userObj$invitation->getSchool(), $invitation->getRoles());
  211.             $invitationsState $this->teacherHelper->getSchoolTeacherInvitationStateByName("accepted");
  212.             $invitation->setInvitationState($invitationsState);
  213.             $em->persist($invitation);
  214.         }
  215.         $em->flush();
  216.         return $userObj;
  217.     }
  218.     private function sendConfirmationMail(User $user) {
  219.         $mailerSwift $this->mailerHelper->getMailerRegisterEmail();
  220.         $options = array();
  221.         $options['user'] = $user;
  222.         $code $this->registerHelper->getUserActivationLinkByUser($user)->getCode();
  223.         $mailConfirmationUrl $this->generateUrl(
  224.                 'teacher_register_step_mail_confirmation', array(
  225.             'cr' => $code)
  226.         );
  227.         $options['confirmationUrl'] = $mailConfirmationUrl;
  228.         $target = new RegisterConfirmation($options);
  229.         $this->mailerHelper->sendMailTemplate($mailerSwift$target);
  230.     }
  231. }