From b878bd4d2af2b296ca22cc9b61b1452f07cf9379 Mon Sep 17 00:00:00 2001 From: Quitta Date: Sun, 25 Aug 2013 06:27:44 +0200 Subject: [PATCH] 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 --- .../ryzom_ams/ams_lib/autoload/helpers.php | 58 +++++++++++++++++-- .../ryzom_ams/www/html/autoload/webusers.php | 45 ++++++++++++++ .../ryzom_ams/www/html/func/add_user.php | 5 +- .../server/ryzom_ams/www/html/func/login.php | 2 + .../tools/server/ryzom_ams/www/html/index.php | 2 +- .../server/ryzom_ams/www/html/sql/install.php | 6 +- .../ryzom_ams/www/html/templates/layout.tpl | 18 ++++-- 7 files changed, 121 insertions(+), 15 deletions(-) diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php index 27f2fd80f..935d1974b 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php @@ -1,13 +1,19 @@ 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 );*/ } diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/autoload/webusers.php b/code/ryzom/tools/server/ryzom_ams/www/html/autoload/webusers.php index d819c89c5..2311916fb 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/autoload/webusers.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/autoload/webusers.php @@ -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 + } + } + } \ No newline at end of file diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/func/add_user.php b/code/ryzom/tools/server/ryzom_ams/www/html/func/add_user.php index 7e81b2751..75f04cc82 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/func/add_user.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/func/add_user.php @@ -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 diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/func/login.php b/code/ryzom/tools/server/ryzom_ams/www/html/func/login.php index 49ef2a435..da3b29478 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/func/login.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/func/login.php @@ -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' ); diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/index.php b/code/ryzom/tools/server/ryzom_ams/www/html/index.php index b14cbb40c..6ce06739e 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/index.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/index.php @@ -59,5 +59,5 @@ if($page == 'error'){ $return['permission'] = 0; $return['no_visible_elements'] = 'FALSE'; } -//print_r($return); + helpers :: loadTemplate( $page , $return ); diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/sql/install.php b/code/ryzom/tools/server/ryzom_ams/www/html/sql/install.php index 2c0ffb6d5..0bf6c640c 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/sql/install.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/sql/install.php @@ -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)); diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout.tpl b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout.tpl index f8fb5078c..fa97211d7 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout.tpl +++ b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout.tpl @@ -103,8 +103,8 @@ {/if}
- - + +
@@ -141,8 +141,8 @@ {if isset($no_visible_elements) and $no_visible_elements eq "TRUE"}
- - + +
{/if} @@ -181,6 +181,16 @@ +