<?php
session_start();

require_once('../modules/InterfaceTranslation.php');
require_once('../modules/InterfacePostgreSQL.php');
require_once('../modules/InterfaceConfig.php');
require_once('../modules/ClassConfig.php');
require_once('../modules/ClassFeature.php');
require_once('../modules/ClassFeatureTranslation.php');
require_once('../modules/ClassUser.php');
require_once('../modules/ClassTranslation.php');

$oFeat = new ClassFeature();
$oFeatTrans = new ClassFeatureTranslation();
$oUser = new ClassUser();
$oTrans = new ClassTranslation();

$oldLang = $_SESSION['config']['default_lang_code'];
$newLang = $_REQUEST['lang'];
$oldPage = $_SERVER['HTTP_REFERER'];

if(isset($_SESSION['user'])){
    $lang = $oUser->setLangForUser($_SESSION['user']['id'], $newLang);
    $_SESSION['user']['lang_code'] = $newLang;
    $_SESSION['user']['core_lang_id'] = $lang['id'];
    $_SESSION['features'] = $oFeat->getFeaturesOfUser($_SESSION['user']['id'], $newLang);
} else {
    $_SESSION['features'] = $oFeat->getFeatures($newLang);
    $_SESSION['config']['default_lang_code'] = $newLang;
}
$_SESSION['config']['default_lang_code'] = $newLang;
$_SESSION['translations'] = $oTrans->listTranslations($newLang);

$newPage = $oFeatTrans->getURLTranslation($oldLang, $newLang, $oldPage);
header("Location: ".$newPage);
