mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-22 01:12:17 +00:00
- Added new constants in config/constants.php:
- LANG_SESSION_ACTIVE_LANGUAGES
- LANG_SESSION_CURRENT_LANGUAGE
- Added new functions in helper hlp_language_helper:
- setUserLanguage
- getDBActiveLanguages
- getActiveLanguages
- Adapted code to make use of them
- Added new controller controllers/widgets/Language
This commit is contained in:
@@ -64,7 +64,9 @@ define('LDAP_TOO_MANY_USER_DN', 11);
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
define('LANG_SESSION_NAME', 'LANGUAGE');
|
||||
define('LANG_SESSION_INDEXES', 'LANGUAGE_INDEXES');
|
||||
define('LANG_SESSION_INDEXES', 'INDEXES');
|
||||
define('LANG_SESSION_ACTIVE_LANGUAGES', 'ACTIVE_LANGUAGES');
|
||||
define('LANG_SESSION_CURRENT_LANGUAGE', 'sprache'); // NOTE: it is not under LANG_SESSION_NAME
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* This controller is used to set the current language for a user
|
||||
* It is part of the Language Switcher Widget
|
||||
*/
|
||||
class Language extends FHC_Controller
|
||||
{
|
||||
/**
|
||||
* Calls the parent's constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setSessionLanguage()
|
||||
{
|
||||
$language = $this->input->post('language');
|
||||
|
||||
$this->outputJson(setUserLanguage($language));
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,9 @@ function getUserLanguage($language = null)
|
||||
$language = DEFAULT_LANGUAGE;
|
||||
|
||||
// If the language is present in the session and it is valid
|
||||
if (isset($_SESSION['sprache']) && !isEmptyString($_SESSION['sprache']))
|
||||
if (isset($_SESSION[LANG_SESSION_CURRENT_LANGUAGE]) && !isEmptyString($_SESSION[LANG_SESSION_CURRENT_LANGUAGE]))
|
||||
{
|
||||
$language = $_SESSION['sprache']; // then use it
|
||||
$language = $_SESSION[LANG_SESSION_CURRENT_LANGUAGE]; // then use it
|
||||
}
|
||||
// Otherwise checks if the user is authenticated to retrieve the users's language
|
||||
// NOTE: this helper could be called when the user is NOT logged in the system
|
||||
@@ -82,31 +82,21 @@ function getPhraseByLanguage($phraseLanguagesArray, $language)
|
||||
$langArray = getSessionElement(LANG_SESSION_NAME, LANG_SESSION_INDEXES);
|
||||
if ($langArray == null) // If not already loaded in session
|
||||
{
|
||||
// Loads the Sprache_model to retrieve the language settings from the DB
|
||||
// NOTE: Stores the loaded model with the alias SpracheModelLanguage to avoid to overwrite
|
||||
// an already loaded SpracheModel used somewhere else
|
||||
$ci->load->model('system/Sprache_model', 'SpracheModelLanguage');
|
||||
|
||||
// Add order clause by index and select only the sprache column
|
||||
$ci->SpracheModelLanguage->addOrder('index');
|
||||
$ci->SpracheModelLanguage->addSelect('sprache');
|
||||
|
||||
// Retrieves from public.tbl_sprache
|
||||
$dbLanguages = $ci->SpracheModelLanguage->load();
|
||||
// Retrieves active languages
|
||||
$dbLanguages = getDBActiveLanguages();
|
||||
if (hasData($dbLanguages)) // If everything is ok and contains data
|
||||
{
|
||||
$index = 0; // Incremental integer
|
||||
$languageIndexes = array(); // Array that will contains languages and their indexes
|
||||
$langArray = array(); // Array that will contains languages and their indexes
|
||||
|
||||
// Loops through database results
|
||||
foreach (getData($dbLanguages) as $dbLanguage)
|
||||
{
|
||||
$languageIndexes[$dbLanguage->sprache] = $index++; // set $languageIndexes array elements
|
||||
$langArray[$dbLanguage->sprache] = $index++; // set $languageIndexes array elements
|
||||
}
|
||||
}
|
||||
|
||||
$langArray = $languageIndexes; // copy $languageIndexes to $langArray
|
||||
// Set session element $_SESSION['LANG']['LANG_INDEXES'] with $languageIndexes
|
||||
// Set session element $_SESSION['LANG']['LANG_INDEXES'] with $langArray
|
||||
setSessionElement(LANG_SESSION_NAME, LANG_SESSION_INDEXES, $langArray);
|
||||
}
|
||||
|
||||
@@ -120,3 +110,78 @@ function getPhraseByLanguage($phraseLanguagesArray, $language)
|
||||
|
||||
return $phrase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load active languages from session, if not present then loads them from database and stores them in session
|
||||
*/
|
||||
function getActiveLanguages()
|
||||
{
|
||||
$languagesArray = getSessionElement(LANG_SESSION_NAME, LANG_SESSION_ACTIVE_LANGUAGES);
|
||||
if ($languagesArray == null)
|
||||
{
|
||||
$languagesArray = array();
|
||||
|
||||
// Retrieves from public.tbl_sprache
|
||||
$dbLanguages = getDBActiveLanguages();
|
||||
if (hasData($dbLanguages))
|
||||
{
|
||||
// Loops through database results
|
||||
foreach (getData($dbLanguages) as $dbLanguage)
|
||||
{
|
||||
$languagesArray[$dbLanguage->sprache] = $dbLanguage->bezeichnung; // set $languageIndexes array elements
|
||||
}
|
||||
}
|
||||
|
||||
// Set session element $_SESSION['LANG']['LANG_SESSION_ACTIVE_LANGUAGES'] with $languagesArray
|
||||
setSessionElement(LANG_SESSION_NAME, LANG_SESSION_ACTIVE_LANGUAGES, $languagesArray);
|
||||
}
|
||||
|
||||
return $languagesArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads active languages from database
|
||||
*/
|
||||
function getDBActiveLanguages()
|
||||
{
|
||||
$ci =& get_instance(); // get CI instance
|
||||
|
||||
// Loads the Sprache_model to retrieve the language settings from the DB
|
||||
// NOTE: Stores the loaded model with the alias SpracheModelLanguage to avoid to overwrite
|
||||
// an already loaded SpracheModel used somewhere else
|
||||
$ci->load->model('system/Sprache_model', 'SpracheModelLanguage');
|
||||
|
||||
// Add order clause by index and select only the sprache column
|
||||
$ci->SpracheModelLanguage->addOrder('index');
|
||||
$ci->SpracheModelLanguage->addSelect('sprache, bezeichnung');
|
||||
|
||||
// Retrieves from public.tbl_sprache
|
||||
return $ci->SpracheModelLanguage->loadWhere(array('content' => true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current language to render the GUI in session
|
||||
*/
|
||||
function setUserLanguage($language)
|
||||
{
|
||||
$languageValid = false;
|
||||
|
||||
// Checks if the given language is valid (present between active languages)
|
||||
foreach (getActiveLanguages() as $languageName => $languageTranslation)
|
||||
{
|
||||
if ($language == $languageName)
|
||||
{
|
||||
$languageValid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($languageValid) // if the provided language is valid
|
||||
{
|
||||
$_SESSION[LANG_SESSION_CURRENT_LANGUAGE] = $language; // stores it in session
|
||||
|
||||
return success('Language successfully changed'); // return success!!
|
||||
}
|
||||
|
||||
return error('The given language is not valid'); // return an error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user