From 0319d0cc2aead3884193e71c36157f07c5a75f5b Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 26 Nov 2019 15:24:26 +0100 Subject: [PATCH] - 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 --- application/config/constants.php | 4 +- application/controllers/widgets/Language.php | 31 ++++++ application/helpers/hlp_language_helper.php | 99 ++++++++++++++++---- 3 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 application/controllers/widgets/Language.php diff --git a/application/config/constants.php b/application/config/constants.php index ac2ecc649..621f58ecb 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -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 /* |-------------------------------------------------------------------------- diff --git a/application/controllers/widgets/Language.php b/application/controllers/widgets/Language.php new file mode 100644 index 000000000..8269d3b2d --- /dev/null +++ b/application/controllers/widgets/Language.php @@ -0,0 +1,31 @@ +input->post('language'); + + $this->outputJson(setUserLanguage($language)); + } +} diff --git a/application/helpers/hlp_language_helper.php b/application/helpers/hlp_language_helper.php index 8090e1e8b..1678a4102 100644 --- a/application/helpers/hlp_language_helper.php +++ b/application/helpers/hlp_language_helper.php @@ -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 +}