<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\SqlConnect;
use App\Service\ApiCalls;
use Symfony\Component\HttpFoundation\RequestStack;
use Psr\Log\LoggerInterface;
use App\Service\WebsiteSetup;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Service\Breadcrumbs;
/**
* Description of AppController
*
* @author FairyWilbury
*/
class AppController extends AbstractController{
protected $SqlConnect;
protected $ApiCalls;
protected $Breadcrumbs;
protected $RequestStack;
protected $Http;
protected $Session;
protected $Cookie;
protected $Logger;
protected $WebsiteSetup;
protected $SecurityToken;
protected $data;
protected $entityUrl;
protected $mode;
protected $site;
protected $page;
protected $customJSFlag;
protected $defaultHomePage;
protected $RedirectHandle = NULL;
protected $commonItems = [];
private $footerItems = [
'_settings' => [],
'prefooter'=> [],
'footer'=> [],
'subfooter'=> [],
];
private $functionChecks = [
'mainNavRes' => FALSE,
'getPageText' => FALSE,
'getAttentionMsg' => FALSE,
'cards' => FALSE,
'getFooter' => FALSE
];
public function __construct(
SqlConnect $SqlConnect,
ApiCalls $ApiCalls,
Breadcrumbs $Breadcrumbs,
RequestStack $RequestStack,
LoggerInterface $Logger,
WebsiteSetup $WebsiteSetup,
TokenStorageInterface $Security
) {
$this->Logger = $Logger;
$this->RequestStack = $RequestStack;
$this->Http = $this->RequestStack->getCurrentRequest();
$this->Session = $this->RequestStack->getSession();
$this->Cookie = $this->Http->cookies;
$this->SecurityToken = $Security;
$this->entityUrl = $WebsiteSetup->entityUrl;
$this->site = $WebsiteSetup->site;
$this->mode = $WebsiteSetup->mode;
$this->SqlConnect = $SqlConnect;
$this->SqlConnect->init($this->site, $this->mode, $this->entityUrl);
$this->page = $this->Http->attributes->get('_route');
if($this->page == 'page'){
$params = $this->Http->attributes->get('_route_params');
$this->page = $params ["pageName"];
}
$this->ApiCalls = $ApiCalls;
$this->ApiCalls->setSqlConnect($this->SqlConnect);
$this->setData('site', $this->site);
$this->setData('entityUrl', $this->entityUrl);
$this->setData('mode', $this->mode);
$this->setData('page', $this->page);
$this->setData('imageSingleFlag', TRUE);
$this->Breadcrumbs = $Breadcrumbs;
$this-> setSubPath();
}
public function switchConnection($connectionName){
$this->SqlConnect = $SqlConnect;
$this->SqlConnect->init($connectionName);
}
protected function setData($key, $value){
$this->data[$key] = $value;
}
protected function getData($key = NULL){
if($key===NULL){
return $this->data;
}
else{
if(isset($this->data[$key])){
return $this->data[$key];
}
else{
return FALSE;
}
}
}
protected function setDataItem($array){
$this->data[key($array)] = $array[key($array)];
}
private function setSubPath(){
// $uri = $this->RequestStack->getCurrentRequest()->getRequestURI();
// $uriArr = explode('/', trim($uri, '/'));
// if(count($uriArr)>0){
// $this->setData('subPath', '/'.$uriArr[0]);
// }
// $this->setData('subPath', '');
$subpath = strtolower($this->site);
$this->setData('subPath', $subpath);
}
public function handleDbErrors($res){
if(isset($res[0]['apiReturnStatus'])){
switch ($res[0]['apiReturnStatus']){
case -1:
$this->SecurityToken->setToken();
return $this->redirectToRoute('signin');
case 1:
return FALSE;
default:
return FALSE;
}
}
else{
return FALSE;
}
}
public function customRenderPath($path, $isCustom){
//'course/index.html.twig'
if ($isCustom){
return $this->site.'/'.$path;
}
else{
return $path;
}
}
protected function search(){
$searchTerms = NULL;
if($this->Http->request->get("searchTerms")){
$searchTerms = $this->Http->request->get("searchTerms");
$this->Session->set('searchTerms', $searchTerms);
}
if($this->Session->get('searchTerms', $searchTerms)){
$searchTerms = $this->Session->get('searchTerms', $searchTerms);
}
$searchID = NULL;
if($this->Session->get('searchID')){
if($this->page==='courseList'){
$searchID = $this->Session->get('searchID');
$this->setData('searchID', $searchID);
}
else{
$this->Session->remove('searchID');
}
}
if($searchTerms!==NULL){
$this->setData('searchTerms', $searchTerms);
}
if($this->page==='categories' || $this->page==='courseList'){ #no redirect
if($this->Session->get('search')
|| ($this->Http->request->all() && $this->Http->request->get("searchTerms"))
|| $searchID!==NULL
){
if(!$this->Session->get('search')){ #not redirected - make a new call
$resSearch = $this->makeSearchCall($searchTerms, $searchID);
$check = $this->handleDbErrors($resSearch);
}
elseif($resSearch = $this->Session->get('search')){
$resSearch = $this->Session->get('search');
$check = TRUE;
$this->Session->remove('search');
}
else{
$this->Session->remove('search');
}
if($this->page==='courseList'){
$this->Session->remove('searchID');
}
$this->Session->remove('searchTerms');
if($check===FALSE){
if(isset($resSearch[0]["card_btn_route_params"]) && !empty($resSearch[0]["card_btn_route_params"])){
foreach($resSearch as $key=>$card){
$resSearch[$key]["card_btn_route_params"] = json_decode($card["card_btn_route_params"], TRUE);
}
$this->setData('search', $resSearch);
}
else{
$this->setData('search', $resSearch);
}
}
else{
return $check;
}
}
else{
return FALSE;
}
}
elseif($this->Http->request->all() && $this->Http->request->get("searchTerms")){ #redirect
$resSearch = $this->makeSearchCall($searchTerms, $searchID);
$this->Session->set('search', $resSearch);
$redirectTo = $this->searchRedirectPage($resSearch);
if($redirectTo){
$this->setData('searchRedirect', $redirectTo);
}
else{
$this->setData('searchEmpty', TRUE);
}
}
else{
if($this->Session->get('searchID')||$this->Session->get('search')||$this->Session->get('searchTerms')){
$this->Session->remove('searchID');
$this->Session->remove('search');
$this->Session->remove('searchTerms');
}
}
}
private function makeSearchCall($searchTerms, $searchID = NULL){
if($this->page!=='courseList'){
$searchID = NULL;
}
if($this->getUser()){
$resSearch = $this->ApiCalls->search($this->page, $searchTerms, $this->getUser()->getEmail(), $searchID);
}
else{
$resSearch = $this->ApiCalls->search($this->page, $searchTerms, NULL, $searchID);
}
if(isset($resSearch[0]['searchID'])){
$this->Session->set('searchID', $resSearch[0]['searchID']);
}
return $resSearch;
}
private function searchRedirectPage($resSearch){
if(isset($resSearch[0]) && (!isset($resSearch[0]['apiReturnStatus']) || $resSearch[0]['apiReturnStatus'] !== 0)){
if(isset($resSearch[0]['courseCode'])){
return 'courseList';
}
else{
return 'categories';
}
}
else{
$this->setData('searchMsgIcon', $resSearch[0]['icon']);
$this->setData('searchMsgTitle', $resSearch[0]['title']);
$this->setData('searchMsgHtml', $resSearch[0]['html']);
$this->setData('searchMsgFooter', $resSearch[0]['footer']);
return FALSE;
}
}
protected function commonCalls($functionNames = NULL){
$toolsFlag = $this->Session->get('toolsFlag');
if($toolsFlag===NULL){
$this->setData('toolsFlag', FALSE);
}
else{
$this->setData('toolsFlag', $toolsFlag);
}
if($this->getUser()!==NULL){
$username = $this->getUser()->getEmail();
$anonymousID = NULL;
}
else{
$username = NULL;
$anonymousID = $this->getAnonymousID();
}
if($functionNames === NULL || in_array('getHomeNav',$functionNames)){
$res = $this->getHomeNav($username, $this->getAnonymousID());
if($res == NULL){
if($functionNames === NULL || in_array('getPageTexts',$functionNames)){
$this->getPageTexts($username, $this->getAnonymousID());
}
if($functionNames === NULL || in_array('getAttentionMessage',$functionNames)){
$this->getAttentionMessage($username);
}
if($functionNames === NULL || in_array('getCards',$functionNames)){
$this->getCards($username);
}
if($functionNames === NULL || in_array('getFooter',$functionNames)){
$this->getFooter($username);
}
if($functionNames === NULL || in_array('getSearch',$functionNames)){
$this->getSearch();
}
return $this->multipleChecks($this->functionChecks);
}
else{
$this->RedirectHandle = $this->redirectToRoute($res);
}
}
}
private function multipleChecks($checks){
$flag = TRUE;
foreach($checks as $key=>$check){
if(!$check){
return $flag;
}
}
}
private function getHomeNav($userEmail = NULL, $sessionID = NULL){
$mainNavRes = $this->ApiCalls->getHomeNav($this->page, $userEmail, $sessionID);
$check = $this->handleDbErrors($mainNavRes);
if($check===FALSE){
$this->setData('mainMenu', $mainNavRes);
if(isset($mainNavRes[0])){
$this->setData('webenrolPass', $mainNavRes[0]["webenrolPasscode"]);
$this->setData('defaultHomePage', $mainNavRes[0]["defaultHomePage"]);
$this->defaultHomePage = $mainNavRes[0]["defaultHomePage"];
$this->Breadcrumbs->setDefaultHomePage($this->defaultHomePage);
if($this->getData('defaultHomePage')){
// $this->Session->set('defaultHomePage', $this->getData('defaultHomePage'));
if($this->defaultHomePage!=='index' && $this->page === 'index'){
return $this->defaultHomePage;
}
}
// if($this->Session->get('defaultHomePage')){
// $this->defaultHomePage = $this->Session->get('defaultHomePage');
// $this->Breadcrumbs->setDefaultHomePage($this->defaultHomePage);
// if($this->page==='index' && $this->defaultHomePage !== 'index'){
// return $this->defaultHomePage;
// }
// }
}
else{
// error handling here
}
}
else{
$this->functionChecks['mainNavRes'] = $check;
}
return NULL;
}
protected function getAttentionMessage($userEmail = NULL, $courseCode = NULL){
$msgData = $this->ApiCalls->getAttentionMessage($this->page,$userEmail, $courseCode);
$check = $this->handleDbErrors($msgData);
if($check===FALSE){
$this->setData('alerts', $msgData);
}
else{
$this->functionChecks['getAttentionMsg'] = $check;
}
}
private function getSearch(){
$this->search();
if($this->getData('searchRedirect')){
$this->RedirectHandle = $this->redirectToRoute($this->getData('searchRedirect'));
}
}
protected function getPageTexts($userEmail = NULL, $sessionID = NULL, $courseCode = NULL, $sectionID = NULL){
if($this->getAnonymousID() && !$this->getUser()){
$sessionID = $this->getAnonymousID();
}
$pageText = $this->ApiCalls->getPageText($this->page, $userEmail, $sessionID, $courseCode, $sectionID);
$check = $this->handleDbErrors($pageText);
if($check===FALSE){
if($pageText!==FALSE){
foreach($pageText as $key=>$textItem){
if(isset($textItem['css']) &&!empty($textItem['css'])){
$pageText[$key]['css'] = json_decode($textItem['css'], TRUE);
}
}
}
$this->setData('pageText', $pageText);
}
else{
$this->functionChecks['getPageText'] = $check;
}
}
private function getFooter($userEmail){
$resFooter = $this->ApiCalls->getFooter($userEmail);
$check = $this->handleDbErrors($resFooter);
if($check===FALSE){
$this->parseFooterData($resFooter);
$this->setData('footer', $this->commonItems["footerItems"]);
}
else{
$this->functionChecks['getFooter'] = $check;
}
}
private function getCards($userEmail = NULL){
$resCards = $this->ApiCalls->getHomePageData($this->page, $userEmail);
$check = $this->handleDbErrors($resCards);
if($check===FALSE){
foreach($resCards as $key=>$cardsItem){
if(isset($cardsItem['css']) &&!empty($cardsItem['css'])){
$resCards[$key]['css'] = json_decode($cardsItem['css'], TRUE);
}
}
if(isset($resCards[0]["card_btn_route_params"]) && !empty($resCards[0]["card_btn_route_params"])){
foreach($resCards as $key=>$card){
$resCards[$key]["card_btn_route_params"] = json_decode($card["card_btn_route_params"], TRUE);
}
$this->setData('cards', $resCards);
}
else{
$this->setData('cards', $resCards);
}
}
else{
$this->functionChecks['cards'] = $check;
}
}
protected function parseFooterData($footerData){
if($footerData){
$footerTypesSwitches = [];
foreach($footerData as $footerItem){
$footerTypesSwitches = $this->setFooterTypesSwitches($footerItem, $footerTypesSwitches);
switch ($footerItem["footerType"]){
case 'prefooter':
if(!isset($this->footerItems['prefooter'][$footerItem['column']])){
$this->footerItems['prefooter'][$footerItem['column']] = [];
}
$footerItem['inner-classes'] = json_decode($footerItem['inner-classes'], TRUE);
$footerItem['outer-classes'] = json_decode($footerItem['outer-classes'], TRUE);
$footerItem['column-classes'] = json_decode($footerItem['column-classes'], TRUE);
$footerItem['empty'] = FALSE;
if($footerItem['column']>0){
for($i=0; $i < $footerItem['column']; $i++){
if(!isset($this->footerItems['footer'][$i])){
$this->footerItems['prefooter'][$i] = [
0 => [
"css-footerType-switch" => $footerItem["css-footerType-switch"],
"empty" => TRUE,
'inner-classes' => $footerItem['inner-classes'],
'outer-classes' => $footerItem['outer-classes'],
'column-classes' => $footerItem['column-classes'],
"css-footerType-background-color" => $footerItem["css-footerType-background-color"],
"css-footerType-color" => $footerItem["css-footerType-color"]
]
];
}
}
}
array_push($this->footerItems['prefooter'][$footerItem['column']],$footerItem);
break;
case 'footer':
if(!isset($this->footerItems['footer'][$footerItem['column']])){
$this->footerItems['footer'][$footerItem['column']] = [];
}
$footerItem['inner-classes'] = json_decode($footerItem['inner-classes'], TRUE);
$footerItem['outer-classes'] = json_decode($footerItem['outer-classes'], TRUE);
$footerItem['column-classes'] = json_decode($footerItem['column-classes'], TRUE);
$footerItem['empty'] = FALSE;
if($footerItem['column']>0){
for($i=0; $i < $footerItem['column']; $i++){
if(!isset($this->footerItems['footer'][$i])){
$this->footerItems['footer'][$i] = [
0 => [
"css-footerType-switch" => $footerItem["css-footerType-switch"],
"empty" => TRUE,
'inner-classes' => $footerItem['inner-classes'],
'outer-classes' => $footerItem['outer-classes'],
'column-classes' => $footerItem['column-classes'],
"css-footerType-background-color" => $footerItem["css-footerType-background-color"],
"css-footerType-color" => $footerItem["css-footerType-color"]
]
];
}
}
}
array_push($this->footerItems['footer'][$footerItem['column']],$footerItem);
break;
case 'subfooter':
if(!isset($this->footerItems['subfooter'][$footerItem['column']])){
$this->footerItems['subfooter'][$footerItem['column']] = [];
}
$footerItem['inner-classes'] = json_decode($footerItem['inner-classes'], TRUE);
$footerItem['outer-classes'] = json_decode($footerItem['outer-classes'], TRUE);
$footerItem['column-classes'] = json_decode($footerItem['column-classes'], TRUE);
$footerItem['empty'] = FALSE;
if($footerItem['column']>0){
for($i=0; $i < $footerItem['column']; $i++){
if(!isset($this->footerItems['footer'][$i])){
$this->footerItems['subfooter'][$i] = [
0 => [
"css-footerType-switch" => $footerItem["css-footerType-switch"],
"empty" => TRUE,
'inner-classes' => $footerItem['inner-classes'],
'outer-classes' => $footerItem['outer-classes'],
'column-classes' => $footerItem['column-classes'],
"css-footerType-background-color" => $footerItem["css-footerType-background-color"],
"css-footerType-color" => $footerItem["css-footerType-color"]
]
];
}
}
}
array_push($this->footerItems['subfooter'][$footerItem['column']],$footerItem);
break;
case 'aside':
if(!isset($this->footerItems['aside'][$footerItem['column']])){
$this->footerItems['aside'][$footerItem['column']] = [];
}
$footerItem['inner-classes'] = json_decode($footerItem['inner-classes'], TRUE);
$footerItem['outer-classes'] = json_decode($footerItem['outer-classes'], TRUE);
$footerItem['column-classes'] = json_decode($footerItem['column-classes'], TRUE);
$footerItem['empty'] = FALSE;
if($footerItem['column']>0){
for($i=0; $i < $footerItem['column']; $i++){
if(!isset($this->footerItems['footer'][$i])){
$this->footerItems['aside'][$i] = [
0 => [
"css-footerType-switch" => $footerItem["css-footerType-switch"],
"empty" => TRUE,
'inner-classes' => $footerItem['inner-classes'],
'outer-classes' => $footerItem['outer-classes'],
'column-classes' => $footerItem['column-classes'],
"css-footerType-background-color" => $footerItem["css-footerType-background-color"],
"css-footerType-color" => $footerItem["css-footerType-color"]
]
];
}
}
}
array_push($this->footerItems['aside'][$footerItem['column']],$footerItem);
break;
}
}
$this->footerItems['_settings']['switches'] = $footerTypesSwitches;
}
else{
$this->commonItems['footerItems'] = NULL;
}
// echo '<pre>';
// var_dump($this->footerItems);
// echo '</pre>';
$this->commonItems['footerItems'] = $this->footerItems;
}
private function setFooterTypesSwitches($footerItem, $footerTypesSwitches){
if(!array_key_exists($footerItem["footerType"], $footerTypesSwitches)){
$footerTypesSwitches[$footerItem["footerType"]] = $footerItem["css-footerType-switch"];
}
return $footerTypesSwitches;
}
protected function getAnonymousID(){
return $this->Cookie->get("PHPSESSID");
}
}