- Added support for language in session in function getUserLanguage from helper hlp_language_helper

- If the language is loaded from database then it is checked that the language is enabled
This commit is contained in:
Paolo
2019-11-22 12:05:07 +01:00
parent 6bf7d46ace
commit ceb3f212c9
2 changed files with 34 additions and 30 deletions
+31 -11
View File
@@ -18,28 +18,48 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
/** /**
* Function to retrieve the language of the logged user * Function to retrieve the language of the logged user
* If is not possible to retrieve it, then the default system language is returnd * If is not possible to retrieve it, then the default system language is returned
* If as parameter is given a valid language the it's returned useful to avoid * NOTE: If the given parameter is a valid language then it is returned
* to write the same control structures for the language * It is useful to avoid to write a lot of "if else" structures
*/ */
function getUserLanguage($language = null) function getUserLanguage($language = null)
{ {
// If the given parameter is a valid language then return it
if (!isEmptyString($language)) return $language; if (!isEmptyString($language)) return $language;
$ci =& get_instance(); // get CI instance // Use the default system language as fallback
// Use the default system language, if it's possible retrieves the language for the logged user
$language = DEFAULT_LANGUAGE; $language = DEFAULT_LANGUAGE;
// 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 // If the language is present in the session and it is valid
// so this is why is checked if the function getAuthUID exists if (isset($_SESSION['sprache']) && !isEmptyString($_SESSION['sprache']))
if (function_exists('getAuthUID'))
{ {
$language = $_SESSION['sprache']; // 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
// therefore is checked if the user is logged
elseif (isLogged())
{
$ci =& get_instance(); // get CI instance
// NOTE: Stores the loaded model with the alias PersonModelLanguage to avoid to overwrite // NOTE: Stores the loaded model with the alias PersonModelLanguage to avoid to overwrite
// an already loaded PersonModel used somewhere else // an already loaded PersonModel used somewhere else
$ci->load->model('person/Person_model', 'PersonModelLanguage'); $ci->load->model('person/Person_model', 'PersonModelLanguage');
$language = $ci->PersonModelLanguage->getLanguage(getAuthUID()); // Retrieves language/s for the logged user
$languagesDB = $ci->PersonModelLanguage->getLanguage(getAuthUID());
if (hasData($languagesDB))
{
// Looks for the first valid language
foreach (getData($languagesDB) as $languageDB)
{
if (!isEmptyString($languageDB->sprache))
{
$language = $languageDB->sprache;
break;
}
}
}
} }
return $language; return $language;
+3 -19
View File
@@ -207,28 +207,12 @@ class Person_model extends DB_Model
*/ */
public function getLanguage($uid) public function getLanguage($uid)
{ {
$language = DEFAULT_LANGUAGE; $this->addSelect('public.tbl_person.sprache');
$this->addJoin('public.tbl_benutzer', 'person_id'); $this->addJoin('public.tbl_benutzer', 'person_id');
$this->addJoin('public.tbl_sprache', 'sprache');
$this->addOrder('public.tbl_person.updateamum', 'DESC'); $this->addOrder('public.tbl_person.updateamum', 'DESC');
$this->addOrder('public.tbl_person.insertvon', 'DESC'); $this->addOrder('public.tbl_person.insertvon', 'DESC');
$persons = $this->loadWhere(array('uid' => $uid)); return $this->loadWhere(array('uid' => $uid, 'content' => true));
if (hasData($persons))
{
for ($i = 0; $i < count($persons->retval); $i++)
{
$person = $persons->retval[$i];
if (!isEmptyString($person->sprache))
{
$language = $person->sprache;
break;
}
}
}
return $language;
} }
} }