<?php
namespace App\Controller;
use App\Controller\AppController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class SignupController extends AppController
{
public function index($sectionId = NULL, $sectionNameSlug = NULL): Response
{
$check = $this->commonCalls(['getFooter','getHomeNav','getSearch']);
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
if(!$this->getUser()){
$sections = $this->ApiCalls->businessRulesSectionsOnly();
if($sectionId === NULL){
$sectionId = $sections[0]["sectionID"];
}
$businessRulesData = $this->ApiCalls->businessRules(NULL, $sectionId);
$slugger = new AsciiSlugger('en_GB');
foreach($businessRulesData as $key=>$rule){
if($this->Session->get('reg-form')){
foreach($this->Session->get('reg-form') as $formKey=>$value){
if($businessRulesData[$key]['fieldname']===$formKey){
$businessRulesData[$key]['value'] = $value;
}
}
}
/*
* refresh and display status if logged in
*/
$businessRulesData[$key]['displayStatus'] = $this->getFieldDisplayStatus($rule);
}
$this->setData('sections', $sections);
$this->setData('businessRulesData', $businessRulesData);
if($sectionNameSlug===NULL){
$sectionName = $sections[0]['sectionName'];
$sectionNum = 1;
$sectionKey = 0;
}
else{
foreach($sections as $key=>$section){
$sectionNameToSlug = trim($section['sectionName']);
$slug = $slugger->slug($sectionNameToSlug);
if($slug == $sectionNameSlug){
$sectionName = $section['sectionName'];
$sectionKey = $key;
$sectionNum = $key+1;
}
}
}
$this->setData('sectionName', $sectionName);
$this->setData('sectionId', $sectionId);
$this->setData('sectionKey', $sectionKey);
$this->setData('sectionNum', $sectionNum); //counting from 1
if(isset($sections[$sectionNum]) && ($sectionNum) < count($sections)){ //this is _this_ section, counting from zero
$this->setData('nextSectionName', $sections[$sectionNum]["sectionName"]);
$this->setData('nextSectionId', $sections[$sectionNum]["sectionID"]);
$this->setData('nextSectionSlug', $slugger->slug($sections[$sectionNum]["sectionName"]));
$this->setData('nextSectionNum', $sectionNum + 1); //counting from 1
}
else{
$this->setData('nextSectionName', FALSE);
$this->setData('nextSectionId', FALSE);
$this->setData('nextSectionSlug', FALSE);
$this->setData('nextSectionNum', FALSE);
}
if(isset($sections[$sectionNum-1]) && ($sectionNum) > 1){ //this is _this_ section, counting from zero
$this->setData('prevSectionName', $sections[$sectionNum-2]["sectionName"]);
$this->setData('prevSectionId', $sections[$sectionNum-2]["sectionID"]);
$this->setData('prevSectionSlug', $slugger->slug($sections[$sectionNum-2]["sectionName"]));
$this->setData('prevSectionNum', $sectionNum-1); //counting from 1
}
else{
$this->setData('prevSectionName', FALSE);
$this->setData('prevSectionId', FALSE);
$this->setData('prevSectionSlug', FALSE);
$this->setData('prevSectionNum', FALSE);
}
// var_dump($this->getData('footer')); die();
return $this->render('signup/index.html.twig', $this->getData());
}
else{
return $this->redirectToRoute('profile');
}
}
public function profile($sectionId = NULL, $sectionNameSlug = NULL): Response
{
$check = $this->commonCalls();
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
if($this->getUser()!==NULL){
$userData = $this->ApiCalls->getProfile($this->getUser()->getEmail());
$check = $this->handleDbErrors($userData);
if($check===FALSE){
$this->setData('userData', $userData);
}
else{
return $check;
}
$sections = $this->ApiCalls->businessRulesSectionsOnly($this->getUser()->getEmail());
if($sectionId === NULL){
$sectionId = $sections[0]["sectionID"];
}
$businessRulesData = $this->ApiCalls->businessRules($this->getUser()->getEmail(), $sectionId);
$slugger = new AsciiSlugger('en_GB');
foreach($businessRulesData as $key=>$rule){
/*
* refresh and display status if logged in
*/
$businessRulesData[$key]['displayStatus'] = $this->getFieldDisplayStatus($rule);
}
$this->setData('sections', $sections);
$this->setData('businessRulesData', $businessRulesData);
if($sectionNameSlug===NULL){
$sectionName = $sections[0]['sectionName'];
$sectionNum = 1;
}
else{
foreach($sections as $key=>$section){
$sectionNameToSlug = trim($section['sectionName']);
$slug = $slugger->slug($sectionNameToSlug);
if($slug == $sectionNameSlug){
$sectionName = $section['sectionName'];
$sectionNum = $key+1;
}
}
}
$this->setData('sectionName', $sectionName);
$this->setData('sectionId', $sectionId);
$this->setData('sectionNum', $sectionNum); //counting from 1
if(isset($sections[$sectionNum]) && ($sectionNum + 1) < count($sections)){ //this is the _next_ section, counting from zero
$this->setData('nextSectionName', $sections[$sectionNum]["sectionName"]);
$this->setData('nextSectionId', $sections[$sectionNum]["sectionID"]);
$this->setData('nextSectionSlug', $slugger->slug($sections[$sectionNum]["sectionName"]));
$this->setData('nextSectionNum', $sectionNum + 1); //counting from 1
}
else{
$this->setData('nextSectionName', FALSE);
$this->setData('nextSectionId', FALSE);
$this->setData('nextSectionSlug', FALSE);
$this->setData('nextSectionNum', FALSE);
}
}
else{
return $this->redirectToRoute('signin');
}
return $this->render('profile/index.html.twig', $this->getData());
}
public function success(){
$check = $this->commonCalls();
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
$sections = $this->ApiCalls->businessRulesSectionsOnly();
$this->setData('sectionName', '');
$this->setData('sectionId', '');
$this->setData('sectionNum', count($sections)+1); //counting from 1
$this->setData('sections', $sections);
if($this->Session->get('reg-form')){
$this->Session->remove('reg-form');
}
return $this->render('signup/success.html.twig', $this->getData());
}
public function business_rules($sectionId, $courseCode = NULL){
if($this->getUser()){
$businessRulesData = $this->ApiCalls->businessRules($this->getUser()->getEmail(), $sectionId, -1, 'webenrol', NULL, $courseCode);
}
else{
$businessRulesData = $this->ApiCalls->businessRules(NULL, $sectionId, -1, 'webenrol', NULL, $courseCode);
}
return new JsonResponse($businessRulesData);
}
public function checks($type){
switch($type){
case 'email_exists':
$email = $this->Http->request->get('email');
if($this->getUser()===NULL){
$res = $this->ApiCalls->emailExists($email);
}
else{
$res = $this->ApiCalls->emailExists($email, $this->getUser()->getEmail());
}
}
return new JsonResponse($res);
}
public function submit($courseCode = NULL){
$formData = $this->Http->request->all();
// var_dump($formData);
// var_dump($this->Http->query->get('page'));
if($this->getUser()==NULL || $this->Http->query->get('page')=="application"){
if($this->Session->get('reg-form')){
$curSession = $this->Session->get('reg-form');
foreach($formData as $key => $value){
$curSession[$key] = $value;
}
$this->Session->set('reg-form', $curSession);
}
else{
$this->Session->set('reg-form', $formData);
}
}
if($this->Http->query->get('page')!=="application"){
$username = NULL;
}
else{
if($this->getUser()){
$username = $this->getUser()->getEmail();
}
else{
$username = NULL;
}
}
if($this->getUser()==NULL || $this->Http->query->get('page')=="application"){
if($this->Http->query->get('finish')){
$finish = TRUE;
if($this->Http->query->get('page') && $this->Http->query->get('page')=='application'){
$res = $this->ApiCalls-> submitApplication($this->Session->get('reg-form'), $courseCode, $username);
}
else{
$res = $this->ApiCalls-> submitSignup($this->Session->get('reg-form'));
}
$data = ['finish'=>$finish, 'res'=>$res];
}
else{
$finish = FALSE;
$data = ['finish'=>$finish];
}
}
else{
$res = $this->ApiCalls-> updateProfile($formData, $this->getUser()->getEmail());
$data = ['res'=>$res];
}
// var_dump($this->Session->get('reg-form'));
return new JsonResponse($data);
}
protected function getFieldDisplayStatus($field){
if($field['visible']===0){
return 'hide';
}
else if($field['requiresRefresh']===0 && $field['required']===1 && ($this->getUser()!==NULL)){
return 'disabled';
}
else if($field['requiresRefresh']===0 && $field['required']===0 && ($this->getUser()!==NULL)){
return 'hideIfEmpty';
}
else{
return 'enable';
}
}
public function courseApplication($courseCode, $sectionId = NULL, $sectionNameSlug = NULL){
$check = $this->commonCalls(['getHomeNav','getCards','getFooter','getSearch']);
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
if($this->getUser()){
$userData = $this->ApiCalls->getProfile($this->getUser()->getEmail());
$check = $this->handleDbErrors($userData);
if($check===FALSE){
$this->setData('userData', $userData);
}
else{
return $check;
}
$username = $this->getUser()->getEmail();
$sessionID = NULL;
}
else{
$username = NULL;
$sessionID = $this->getAnonymousID();
}
$this->getAttentionMessage($username, $courseCode);
$sections = $this->ApiCalls->businessRulesSectionsOnly($username, $sectionId, -1, 'webenrol', 'webEnrolFlag1', NULL, $courseCode);
if($sectionId === NULL){
$sectionId = $sections[0]["sectionID"];
}
$this->getPageTexts($username, $sessionID, $courseCode, $sectionId);
$businessRulesData = $this->ApiCalls->businessRules($username, $sectionId, -1, 'webenrol', NULL, $courseCode);
$slugger = new AsciiSlugger('en_GB');
foreach($businessRulesData as $key=>$rule){
if($this->Session->get('reg-form')){
foreach($this->Session->get('reg-form') as $formKey=>$value){
if($businessRulesData[$key]['fieldname']===$formKey){
$businessRulesData[$key]['value'] = $value;
}
}
}
/*
* refresh and display status if logged in
*/
$businessRulesData[$key]['displayStatus'] = $this->getFieldDisplayStatus($rule);
}
$this->setData('sections', $sections);
$this->setData('courseCode', $courseCode);
$this->setData('businessRulesData', $businessRulesData);
// var_dump($businessRulesData);
if($sectionNameSlug===NULL){
$sectionName = $sections[0]['sectionName'];
$sectionNum = 1;
$sectionKey = 0;
}
else{
foreach($sections as $key=>$section){
$sectionNameToSlug = trim($section['sectionName']);
$slug = $slugger->slug($sectionNameToSlug);
if($slug == $sectionNameSlug){
$sectionName = $section['sectionName'];
$sectionKey = $key;
$sectionNum = $key+1;
}
}
}
$this->setData('sectionName', $sectionName);
$this->setData('sectionId', $sectionId);
$this->setData('sectionKey', $sectionKey);
$this->setData('sectionNum', $sectionNum); //counting from 1
if(isset($sections[$sectionNum]) && ($sectionNum) < count($sections)){ //this is _this_ section, counting from zero
$this->setData('nextSectionName', $sections[$sectionNum]["sectionName"]);
$this->setData('nextSectionId', $sections[$sectionNum]["sectionID"]);
$this->setData('nextSectionSlug', $slugger->slug($sections[$sectionNum]["sectionName"]));
$this->setData('nextSectionNum', $sectionNum + 1); //counting from 1
}
else{
$this->setData('nextSectionName', FALSE);
$this->setData('nextSectionId', FALSE);
$this->setData('nextSectionSlug', FALSE);
$this->setData('nextSectionNum', FALSE);
}
if(isset($sections[$sectionNum-1]) && ($sectionNum) > 1){ //this is _this_ section, counting from zero
$this->setData('prevSectionName', $sections[$sectionNum-2]["sectionName"]);
$this->setData('prevSectionId', $sections[$sectionNum-2]["sectionID"]);
$this->setData('prevSectionSlug', $slugger->slug($sections[$sectionNum-2]["sectionName"]));
$this->setData('prevSectionNum', $sectionNum-1); //counting from 1
}
else{
$this->setData('prevSectionName', FALSE);
$this->setData('prevSectionId', FALSE);
$this->setData('prevSectionSlug', FALSE);
$this->setData('prevSectionNum', FALSE);
}
// var_dump($this->getData('footer')); die();
return $this->render('signup/application.html.twig', $this->getData());
}
}