<?php
/**
 * @author David DRAPEAU <david@david-drapeau.com>
 * @version 0.1
 */
// Reporte toutes les erreurs PHP (Voir l'historique des modifications)
//if(isset($_SESSION['config']['framework_environment']) && $_SESSION['config']['framework_environment']=="dev"){
ini_set("display_errors", "stdout");
error_reporting(E_ALL & E_NOTICE);
//}

// Send page content into a buffer instead of draw it directly.
// Required for file downloads !!
ob_start();

session_start();
//unset($_SESSION);

if(isset($_SESSION['user']) and !isset($_SESSION['user']['id'])){
  unset($_SESSION['user']);
  unset($_SESSION['config']);
}

require_once './modules/InterfacePostgreSQL.php';
require_once './modules/InterfaceConfig.php';
require_once './modules/InterfaceTranslation.php';
require_once './modules/InterfaceEmail.php';

require_once './modules/ClassConfig.php';
$oConf = new ClassConfig();

require_once './modules/ClassTranslation.php';
require_once './modules/ClassController.php';
require_once './modules/ClassLang.php';
require_once './modules/ClassFeature.php';
require_once './modules/ClassFeatureTranslation.php';
require_once './modules/ClassCountry.php';
require_once './modules/ClassCurrency.php';
require_once './modules/ClassUser.php';
require_once './modules/ClassEmail.php';
require_once './modules/ClassForm.php';

//IMPORT MODULES FROM CUSTOM DIRECTORY
require_once './custom/modules.php';

try {
  $oPDOLink = $oConf->databaseConnect();
} catch(PDOException $e){
    print "Error:". $e->getMessage()."<br/>";
    die("If you are seeing this message, please send urgently a message to contact@busines-user-only.com");
}

//// Scan the BDD, try to get Aiibi's version. If we can't, we can deduce we need to install the database from scratch
//$statement = $oPDOLink->query("SELECT v FROM core_config WHERE k='aiibi_version' AND is_active=TRUE;");
//if($statement == FALSE){
//  // We can connect teh database, but can't run the SQL statement.
//  // We consider that the database is inexistant, so we redirect to installation script.
//  header('Location: '.$oConf->getURLRoot('install')); 
//  exit;
//}

if(!isset($_COOKIE['firstvisit'])){
  $display_cookie_message = true;
  setcookie("wefra-odoo11", "false", time()+60*60*24*60);
}

$oTrans = new ClassTranslation();
$oController = new ClassController($_REQUEST);
$oLang = new ClassLang();
$oFeat = new ClassFeature();
$oFeatTrans = new ClassFeatureTranslation();
$oUser = new ClassUserCustom();
$oCountry = new ClassCountry();
$oCurrency = new ClassCurrency();
$oERP = new ClassERP();
////TODO method to code
//if(isset($_SESSION['user']) and $_SESSION['user']['modules_path']!="") $oUser->getCustomerModules($_SESSION['user']['modules_path']);

// Load configuration in _SESSION
//unset($_SESSION['config']); //uncomment this line, even for test, creates a bug for translations for logged users
if (!isset($_SESSION['config'])) {
    $_SESSION['config'] = $oConf->getConfig();
    $_SESSION['erp'] = $oERP->getConfig();
    
    //////
    //// The code below creates a bug in Chromium "Too many redirections
    //$find_lang = "/[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z]/";
    //preg_match($find_lang, $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang);
    //$lang = str_replace('-', '_', strtolower($lang[0]));
    //$langs = $oLang->listLanguages();
    //
    //foreach ($langs as $k=>$v) {
    //  if ($langs[$k]['code'] == $lang) {
    //    $_SESSION['config']['default_lang_code'] = $lang;
    //  }
    //}
    
    $_SESSION['translations'] = $oTrans->listTranslations($_SESSION['config']['default_lang_code']);
    $_SESSION['countries'] = $oCountry->listCountries('priority ASC, name ASC');
    $_SESSION['activeCountries'] = $oCountry->listActiveCountries('priority ASC, name ASC');
    $_SESSION['currencies'] = $oCurrency->listCurrencies('priority ASC, name ASC');
    $_SESSION['languages'] = $oLang->listLanguages();
    $_SESSION['activeLanguages'] = $oLang->listActiveLanguages();
}

if(!isset($_SESSION['translations'])){
    $_SESSION['translations'] = $oTrans->listTranslations('fr_fr');
}
if(!isset($_SESSION['countries'])){
    $_SESSION['countries'] = $oCountry->listCountries('priority ASC, name ASC');
}
if(!isset($_SESSION['activeCountries'])){
    $_SESSION['activeCountries'] = $oCountry->listActiveCountries('priority ASC, name ASC');
}
if(!isset($_SESSION['currencies'])){
    $_SESSION['currencies'] = $oCurrency->listCurrencies('priority ASC, name ASC');
}
if(!isset($_SESSION['languages'])){
    $_SESSION['languages'] = $oLang->listLanguages();
}
if(!isset($_SESSION['activeLanguages'])){
    $_SESSION['activeLanguages'] = $oLang->listActiveLanguages();
}

//$data_updated = array();
//$user = array();
//if(isset($_SESSION['user']) && isset($_POST['submit_personal_data'])){
//    $_SESSION['data_updated'] = $oUser->updatePersonalData($_SESSION['user']['id'], $_POST);
//    //$data_updated = $oUser->updatePersonalData($_SESSION['user']['id'], $_POST);
//    $user = $oUser->getFullProfile($_SESSION['user']['id']);
//    $_SESSION['user']['core_lang_id'] = $user['core_lang_id'];
//    $_SESSION['user']['core_country_id'] = $user['core_country_id'];
//    $_SESSION['user']['lang_code'] = $user['lang_code'];
//    $_SESSION['translations'] = $oTrans->listTranslations($user['lang_code']);
//    $_SESSION['features'] = $oFeat->getFeaturesOfUser($_SESSION['user']['id'], $_SESSION['user']['lang_code']);
//    unset($_POST);
//    unset($user);
//    //$_SESSION['data_updated'] = $data_updated;
//    //unset($data_updated);
//}

// If no requested page defined, load default page in default lang code
if (!isset($_REQUEST['page'])) header("Location: ".$oConf->getURLRoot($oFeatTrans->getFeatureTranslation('menu_home_url', $_SESSION['config']['default_lang_code'])));

// Get features for menu
//unset($_SESSION['features']);
if (!isset($_SESSION['features'])) {
    if (!isset($_SESSION['user']['id'])) {
        $_SESSION['features'] = $oFeat->getFeatures($_SESSION['config']['default_lang_code']);
    } else if (isset($_SESSION['user']['id'])) {
        $_SESSION['features'] = $oFeat->getFeaturesOfUser($_SESSION['user']['id']);
    }
}


require_once 'themes/materialize/index.php' ;  // this line need to be uncommented for multi-design option
//require_once 'themes/'.$_SESSION['config']['theme'].'/index.php' ;  // this line need to be uncommented for multi-design option


ob_end_flush();
?>
