rewrote language handling + language is now stored in the db and the value is based on the users language flag clicked (or default) or the ingame language

This commit is contained in:
Quitta 2013-08-25 06:27:44 +02:00
parent 845719fd29
commit e9f3a560a8
7 changed files with 121 additions and 15 deletions

View file

@ -1,13 +1,19 @@
<?php
class Helpers{
static public function loadTemplate( $template, $vars = array (), $forcelibrender = false )
class Helpers{
public static function loadTemplate( $template, $vars = array (), $forcelibrender = false )
{
global $AMS_LIB;
global $SITEBASE;
global $AMS_TRANS;
global $INGAME_LAYOUT;
define('SMARTY_SPL_AUTOLOAD',1);
require_once $AMS_LIB . '/smarty/libs/Smarty.class.php';
spl_autoload_register('__autoload');
$smarty = new Smarty;
// turn smarty debugging on/off
@ -34,7 +40,7 @@ class Helpers{
$smarty -> assign( $key, $value );
}
$variables = Helpers::handle_language();
foreach ( $variables[$template] as $key => $value ){
$smarty -> assign( $key, $value );
@ -87,7 +93,49 @@ class Helpers{
global $DEFAULT_LANGUAGE;
global $AMS_TRANS;
//if language get param is given = set cookie
//if user wants to change the language
if(isset($_GET['Language']) && isset($_GET['setLang'])){
//The ingame client sometimes sends full words, derive those!
switch($_GET['Language']){
case "English":
$lang = "en";
break;
case "French":
$lang = "fr";
break;
default:
$lang = $_GET['Language'];
}
//if the file exists en the setLang = true
if( file_exists( $AMS_TRANS . '/' . $lang . '.ini' ) && $_GET['setLang'] == "true"){
//set a cookie & session var and incase logged in write it to the db!
setcookie( 'Language', $lang , time() + 60*60*24*30 );
$_SESSION['Language'] = $lang;
if(WebUsers::isLoggedIn()){
WebUsers::setLanguage($_SESSION['id'],$lang);
}
}else{
$_SESSION['Language'] = $DEFAULT_LANGUAGE;
}
}else{
//if the session var is not set yet
if(!isset($_SESSION['Language'])){
//check if a cookie already exists for it
if ( isset( $_COOKIE['Language'] ) ) {
$_SESSION['Language'] = $_COOKIE['Language'];
//else use the default language
}else{
$_SESSION['Language'] = $DEFAULT_LANGUAGE;
}
}
}
return parse_ini_file( $AMS_TRANS . '/' . $_SESSION['Language'] . '.ini', true );
/*/if language get param is given = set cookie
//else if no get param is given and a cookie is set, use that language, else use default.
if ( isset( $_GET['language'] ) ) {
//check if the language is supported
@ -110,7 +158,7 @@ class Helpers{
}
}
return parse_ini_file( $AMS_TRANS . '/' . $language . '.ini', true );
return parse_ini_file( $AMS_TRANS . '/' . $language . '.ini', true );*/
}

View file

@ -10,6 +10,7 @@ class WebUsers extends Users{
private $gender;
private $country;
private $receiveMail;
private $language;
function __construct($UId = 0) {
$this->uId = $UId;
@ -24,6 +25,7 @@ class WebUsers extends Users{
$this->gender = $values['Gender'];
$this->country = $values['Country'];
$this->receiveMail = $values['ReceiveMail'];
$this->language = $values['Language'];
}
/**
@ -136,6 +138,16 @@ class WebUsers extends Users{
return $this->receiveMail;
}
public function getLanguage(){
$dbw = new DBLayer("web");
if(! isset($this->language) || $this->language == ""){
$statement = $dbw->execute("SELECT * FROM ams_user WHERE UId=:id", array('id' => $this->uId));
$row = $statement->fetch();
$this->set($row);
}
return $this->language;
}
public function isLoggedIn(){
if(isset($_SESSION['user'])){
return true;
@ -183,6 +195,18 @@ class WebUsers extends Users{
}
}
public static function setLanguage($user, $language){
$values = Array('user' => $user, 'language' => $language);
try {
//make connection with and put into shard db
$dbw = new DBLayer("web");
$dbw->execute("UPDATE ams_user SET Language = :language WHERE UId = :user ",$values);
}
catch (PDOException $e) {
//ERROR: the web DB is offline
}
}
public function getUsers(){
$dbl = new DBLayer("web");
$data = $dbl->executeWithoutParams("SELECT * FROM ams_user");
@ -193,4 +217,25 @@ class WebUsers extends Users{
return "SELECT * FROM ams_user";
}
public static function createWebuser($name, $pass, $mail){
//register account with the correct language (check if cookie is already set)!
if ( isset( $_COOKIE['Language'] ) ) {
$lang = $_COOKIE['Language'];
}else{
global $DEFAULT_LANGUAGE;
$lang = $DEFAULT_LANGUAGE;
}
$values = Array('name' => $name, 'pass' => $pass, 'mail' => $mail, 'lang' => $lang);
try {
$dbw = new DBLayer("web");
return $dbw->executeReturnId("INSERT INTO ams_user (Login, Password, Email, Language) VALUES (:name, :pass, :mail, :lang)",$values);
}
catch (PDOException $e) {
//ERROR: the web DB is offline
}
}
}

View file

@ -47,9 +47,8 @@ function write_user($newUser){
);
try{
//make connection with web db and put it in there
$dbw = new DBLayer("web");
$user_id = $dbw->executeReturnId("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params);
//make new webuser
createWebuser($params['name'], $params['pass'], $params['mail']);
//Create the user on the shard + in case shard is offline put copy of query in query db
//returns: ok, shardoffline or liboffline

View file

@ -11,6 +11,8 @@ function login(){
$_SESSION['user'] = $username;
$_SESSION['id'] = $result['UId'];
$_SESSION['ticket_user'] = Ticket_User::constr_ExternId($result['UId']);
$user = new WebUsers($_SESSION['id']);
$_SESSION['Language'] = $user->getLanguage();
//go back to the index page.
header( 'Location: index.php' );

View file

@ -59,5 +59,5 @@ if($page == 'error'){
$return['permission'] = 0;
$return['no_visible_elements'] = 'FALSE';
}
//print_r($return);
helpers :: loadTemplate( $page , $return );

View file

@ -26,6 +26,7 @@
`Gender` tinyint(1) unsigned NOT NULL DEFAULT '0',
`Country` char(2) NOT NULL DEFAULT '',
`ReceiveMail` int(1) NOT NULL DEFAULT 1,
`Language` varchar(3) DEFAULT NULL,
PRIMARY KEY (`UId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='contains all users information for ryzom_ams';
@ -391,11 +392,12 @@
'name' => "admin",
'pass' => $hashpass,
'mail' => "admin@admin.com",
'permission' => 3
'permission' => 3,
'lang' => "en"
);
try{
$dbw = new DBLayer("web");
$user_id = $dbw->executeReturnId("INSERT INTO ams_user (Login, Password, Email, Permission) VALUES (:name, :pass, :mail, :permission)",$params);
$user_id = $dbw->executeReturnId("INSERT INTO ams_user (Login, Password, Email, Permission, Language) VALUES (:name, :pass, :mail, :permission, :lang)",$params);
Users::createUser($params, $user_id);
$dbl = new DBLayer("lib");
$dbl->execute("UPDATE ticket_user SET Permission = 3 WHERE TUserId = :user_id",array('user_id' => $user_id));

View file

@ -103,8 +103,8 @@
{/if}
<div class="btn-group pull-right">
<div class="flags">
<img src="img/en.png" onclick="document.cookie='language=en';document.location.reload(true);"/>
<img src="img/fr.png" onclick="document.cookie='language=fr';document.location.reload(true);"/>
<img src="img/en.png" onclick="reloadPageWithLanguage('en');"/>
<img src="img/fr.png" onclick="reloadPageWithLanguage('fr');"/>
</div>
</div>
<!-- user dropdown ends -->
@ -141,8 +141,8 @@
{if isset($no_visible_elements) and $no_visible_elements eq "TRUE"}
<div class="flags_no_visible_elements">
<img src="img/en.png" onclick="document.cookie='language=en';document.location.reload(true);"/>
<img src="img/fr.png" onclick="document.cookie='language=fr';document.location.reload(true);"/>
<img src="img/en.png" onclick="reloadPageWithLanguage('en');"/>
<img src="img/fr.png" onclick="reloadPageWithLanguage('fr');"/>
</div>
{/if}
@ -181,6 +181,16 @@
<!-- external javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script>
function reloadPageWithLanguage(language){
if(window.location.search == ""){
var url = window.location.href + "?setLang=true&Language=" + language;
}else{
var url = window.location.href + "&setLang=true&Language=" + language;
}
window.location.href = url;
}
</script>
<!-- jQuery -->
<script src="js/jquery-1.7.2.min.js"></script>