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 <?php
class Helpers{ class Helpers{
static public function loadTemplate( $template, $vars = array (), $forcelibrender = false )
public static function loadTemplate( $template, $vars = array (), $forcelibrender = false )
{ {
global $AMS_LIB; global $AMS_LIB;
global $SITEBASE; global $SITEBASE;
global $AMS_TRANS; global $AMS_TRANS;
global $INGAME_LAYOUT; global $INGAME_LAYOUT;
define('SMARTY_SPL_AUTOLOAD',1);
require_once $AMS_LIB . '/smarty/libs/Smarty.class.php'; require_once $AMS_LIB . '/smarty/libs/Smarty.class.php';
spl_autoload_register('__autoload');
$smarty = new Smarty; $smarty = new Smarty;
// turn smarty debugging on/off // turn smarty debugging on/off
@ -87,7 +93,49 @@ class Helpers{
global $DEFAULT_LANGUAGE; global $DEFAULT_LANGUAGE;
global $AMS_TRANS; 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. //else if no get param is given and a cookie is set, use that language, else use default.
if ( isset( $_GET['language'] ) ) { if ( isset( $_GET['language'] ) ) {
//check if the language is supported //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 $gender;
private $country; private $country;
private $receiveMail; private $receiveMail;
private $language;
function __construct($UId = 0) { function __construct($UId = 0) {
$this->uId = $UId; $this->uId = $UId;
@ -24,6 +25,7 @@ class WebUsers extends Users{
$this->gender = $values['Gender']; $this->gender = $values['Gender'];
$this->country = $values['Country']; $this->country = $values['Country'];
$this->receiveMail = $values['ReceiveMail']; $this->receiveMail = $values['ReceiveMail'];
$this->language = $values['Language'];
} }
/** /**
@ -136,6 +138,16 @@ class WebUsers extends Users{
return $this->receiveMail; 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(){ public function isLoggedIn(){
if(isset($_SESSION['user'])){ if(isset($_SESSION['user'])){
return true; 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(){ public function getUsers(){
$dbl = new DBLayer("web"); $dbl = new DBLayer("web");
$data = $dbl->executeWithoutParams("SELECT * FROM ams_user"); $data = $dbl->executeWithoutParams("SELECT * FROM ams_user");
@ -193,4 +217,25 @@ class WebUsers extends Users{
return "SELECT * FROM ams_user"; 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{ try{
//make connection with web db and put it in there //make new webuser
$dbw = new DBLayer("web"); createWebuser($params['name'], $params['pass'], $params['mail']);
$user_id = $dbw->executeReturnId("INSERT INTO ams_user (Login, Password, Email) VALUES (:name, :pass, :mail)",$params);
//Create the user on the shard + in case shard is offline put copy of query in query db //Create the user on the shard + in case shard is offline put copy of query in query db
//returns: ok, shardoffline or liboffline //returns: ok, shardoffline or liboffline

View file

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

View file

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

View file

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

View file

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