<?php
namespace App\Controller\FrontendBundle\Registration;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\BackendBundle\Mailer\Target\Register\RegisterConfirmation;
use App\BackendBundle\Helper\MailerHelper;
use App\BackendBundle\Helper\TeacherHelper;
use App\BackendBundle\Helper\SchoolHelper;
use App\BackendBundle\Helper\RegisterHelper;
use App\BackendBundle\Helper\SecurityHelper;
use App\BackendBundle\Helper\SiteTitleHelper;
use App\BackendBundle\Helper\UserHelper;
use App\BackendBundle\Helper\ValidationHelper;
use App\Entity\User;
class RegisterTeacherController extends AbstractController {
private $teacherHelper;
private $mailerHelper;
private $registerHelper;
private $userHelper;
private $siteTitleHelper;
private $validationHelper;
private $securityHelper;
private $schoolHelper;
public function __construct(SiteTitleHelper $sitetitlehelper, TeacherHelper $teacherhelper, MailerHelper $mailerhelper, RegisterHelper $registerhelper, UserHelper $userhelper, ValidationHelper $validationhelper, SecurityHelper $securityhelper, SchoolHelper $schoolhelper) {
$this->siteTitleHelper = $sitetitlehelper;
$this->mailerHelper = $mailerhelper;
$this->registerHelper = $registerhelper;
$this->userHelper = $userhelper;
$this->validationHelper = $validationhelper;
$this->securityHelper = $securityhelper;
$this->teacherHelper = $teacherhelper;
$this->schoolHelper = $schoolhelper;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @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."})
*/
public function directorRegisterAction(Request $request) {
$this->siteTitleHelper->setTitleDescription($request);
if ($request->isXmlHttpRequest()) {
$data = array();
$data['status'] = 'ok';
$this->validateDirectorData($data, $request);
return new JsonResponse($data);
} else {
$validationData = $this->validationHelper->getFormValidationData('register');
return $this->render('@frontend/registration/register_director.html.twig', array(
'validationData' => $validationData
));
}
}
private function validateDirectorData(&$data, Request $request) {
$loginData = $request->request->get('login');
$personData = $request->request->get('person');
$schoolData = $request->request->get('school');
$isFormValid = $this->validationHelper->isFormValid('register', $request);
if (!$isFormValid) {
$data['status'] = 'error';
$data['message'] = 'Fehler beim Absenden der Formulardaten';
return;
}
$schoolID = $schoolData['schoolid'];
$school = $this->schoolHelper->getSchoolById($schoolID);
if (empty($school)) {
$data['status'] = 'error';
$data['message'] = 'Die Schule wurde nicht gefunden';
return;
}
$eMail = $loginData['email'];
$directorMail = $this->schoolHelper->getSchoolDirectorMail($school);
// check if the typed director mail matches the selected
// school director mail
if (isset($school) && $directorMail != $eMail) {
$data['status'] = 'error';
$data['message'] = 'Bitte tragen Sie im Feld „E-Mail-Adresse“, die im TIBS-System hinterlegte E-Mail-Adresse der Schule ein.';
return;
}
// at last check if the user is already confirmed
$user = $this->registerHelper->getUserByMail($eMail);
if (empty($user)) {
$newUser = $this->saveDirectorDataInDb($loginData, $personData, $school);
$this->sendConfirmationMail($newUser);
$data['url'] = $this->generateUrl('teacher_register_step_sendmail_confirmation');
return;
}
if (!$this->registerHelper->isUserConfirmed($user)) {
$resetActivationLinkURL = $this->generateUrl('teacher_register_step_sendmail_confirmation_reset');
$data['status'] = 'error';
$data['message'] = 'Sie sind bereits auf berufsreise.at registriert, haben jedoch Ihre Registrierungsbestätigung per E-Mail noch nicht durchgeführt. '
. 'Klicken Sie <a style="color:#000; text-decoration:underline;" href="' . $resetActivationLinkURL . '">hier</a>, '
. 'wenn wir Ihnen die E-Mail mit der Bestätigung nochmals senden sollen.';
$session = $this->get('session');
$session->set('resetActivationLinkUserID', $user->getId());
} else {
$data['status'] = 'error';
$data['message'] = 'Zu den eingegebenen Daten existiert bereits ein Benutzer';
}
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @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."})
*/
public function teacherRegisterAction(Request $request) {
$this->siteTitleHelper->setTitleDescription($request);
if ($request->isXmlHttpRequest()) {
$data = array();
$data['status'] = 'ok';
$this->validateTeacherData($data, $request);
return new JsonResponse($data);
} else {
$validationData = $this->validationHelper->getFormValidationData('register');
return $this->render('@frontend/registration/register_teacher.html.twig', array(
'validationData' => $validationData
));
}
}
private function validateTeacherData(&$data, Request $request) {
$loginData = $request->request->get('login');
$personData = $request->request->get('person');
$schoolData = $request->request->get('school');
$isFormValid = $this->validationHelper->isFormValid('register', $request);
if (!$isFormValid) {
$data['status'] = 'error';
$data['message'] = 'Fehler beim Absenden der Formulardaten';
return;
}
$user = $this->registerHelper->getUserByMail($loginData['email']);
if (empty($user)) {
$newUser = $this->saveTeacherDataInDb($loginData, $personData, $schoolData);
$this->sendConfirmationMail($newUser);
$data['url'] = $this->generateUrl('teacher_register_step_sendmail_confirmation');
} else {
$data['status'] = 'error';
$data['message'] = 'Zu den eingegebenen Daten existiert bereits ein Benutzer';
}
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/register/lehrer/success", name="teacher_register_step_sendmail_confirmation")
*/
public function registerSuccessAction() {
return $this->render('@frontend/registration/mailConfirmation/link_sent.html.twig');
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/register/lehrer/mail_confirmation_reset", name="teacher_register_step_sendmail_confirmation_reset")
*/
public function resetActivationLinkAction() {
$session = $this->get('session');
if (!is_int($session->get('resetActivationLinkUserID'))) {
return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
}
$user = $this->registerHelper->getUserById($session->get('resetActivationLinkUserID'));
if (!$this->registerHelper->resetUserActivationLinkCode($user)) {
return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
}
// when the code has been resetted, send another mail
$this->sendConfirmationMail($user);
$session->remove('resetActivationLinkUserID');
return $this->render('@frontend/registration/mailConfirmation/link_confirm_reset.html.twig');
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/register/lehrer/activate", name="teacher_register_step_mail_confirmation", defaults={"title": "Registrierung abgeschlossen", "description": "Vielen Dank für Ihre Registrierung auf berufsreise.at."})
*/
public function confirmActivationLinkAction(Request $request) {
$this->siteTitleHelper->setTitleDescription($request);
$confirmationCode = $request->query->get('cr');
if (!empty($confirmationCode)) {
$mailIsConfirmed = $this->registerHelper->activateUser($confirmationCode);
if ($mailIsConfirmed) {
$loginURL = $this->generateUrl('security_login');
return $this->render('@frontend/registration/mailConfirmation/link_confirmed.html.twig', array(
'loginURL' => $loginURL,
));
}
}
return $this->render('@frontend/registration/mailConfirmation/link_confirmed_error.html.twig');
}
private function saveDirectorDataInDb($loginData, $personData, $school) {
$em = $this->getDoctrine()->getManager();
// create the User Object for the Teacher
$userObj = $this->registerHelper->createUser($loginData['email'], $loginData['password'], $loginData['email'], array('ROLE_USER', 'ROLE_DIRECTOR'), false);
// convert the Birthdate and create the Person Object
$personObj = $this->registerHelper->createPerson($personData['firstname'], $personData['lastname'], $personData['gender'], null, null);
$em->persist($userObj);
$em->persist($personObj);
$this->userHelper->createUserData($userObj, $personObj, null, null);
// add all school classes to the teacher, that have been entered in the Code Field
// here the SchoolClassMappings get persisted but not flushed
$this->teacherHelper->addSchoolToDirector($userObj, $school);
return $userObj;
}
private function saveTeacherDataInDb($loginData, $personData, $schoolData) {
$em = $this->getDoctrine()->getManager();
// create the User Object for the Teacher
$userObj = $this->registerHelper->createUser($loginData['email'], $loginData['password'], $loginData['email'], array('ROLE_USER', 'ROLE_TEACHER'), false);
// convert the Birthdate and create the Person Object
$personObj = $this->registerHelper->createPerson($personData['firstname'], $personData['lastname'], $personData['gender'], null, null);
$em->persist($userObj);
$em->persist($personObj);
$this->userHelper->createUserData($userObj, $personObj, null, null);
$em->flush();
$invitations = $this->teacherHelper->getSchoolTeacherInvitationsByMail($loginData['email']);
foreach ($invitations as $invitation) {
$this->schoolHelper->createNewSchoolTeacherMapping($userObj, $invitation->getSchool(), $invitation->getRoles());
$invitationsState = $this->teacherHelper->getSchoolTeacherInvitationStateByName("accepted");
$invitation->setInvitationState($invitationsState);
$em->persist($invitation);
}
$em->flush();
return $userObj;
}
private function sendConfirmationMail(User $user) {
$mailerSwift = $this->mailerHelper->getMailerRegisterEmail();
$options = array();
$options['user'] = $user;
$code = $this->registerHelper->getUserActivationLinkByUser($user)->getCode();
$mailConfirmationUrl = $this->generateUrl(
'teacher_register_step_mail_confirmation', array(
'cr' => $code)
);
$options['confirmationUrl'] = $mailConfirmationUrl;
$target = new RegisterConfirmation($options);
$this->mailerHelper->sendMailTemplate($mailerSwift, $target);
}
}