Skip to content

Commit

Permalink
Add a get_current_language() helper function
Browse files Browse the repository at this point in the history
This can be used instead of accessing the global "current_language" variable.

Replace various accesses to the "authenticated_user_language" session var
with a call to get_current_language().

Adds some basic unit tests.
  • Loading branch information
lazka committed Jul 11, 2019
1 parent fd6a345 commit 55e0d46
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 58 deletions.
7 changes: 2 additions & 5 deletions data/SugarBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -3295,11 +3295,8 @@ public function create_notification_email($notify_user)
$locale->translateCharsetMIME(trim($notify_name), 'UTF-8', $OBCharset)
);

if (empty($_SESSION['authenticated_user_language'])) {
$current_language = $sugar_config['default_language'];
} else {
$current_language = $_SESSION['authenticated_user_language'];
}

$current_language = get_current_language();
$xtpl = new XTemplate(get_notify_template_file($current_language));
if ($this->module_dir == "Cases") {
//we should use Case, you can refer to the en_us.notify_template.html.
Expand Down
12 changes: 2 additions & 10 deletions include/MVC/SugarApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,7 @@ public function handleAccessControl()
*/
public static function preLoadLanguages()
{
if (!empty($_SESSION['authenticated_user_language'])) {
$GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
} else {
$GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
}
$GLOBALS['current_language'] = get_current_language();
$GLOBALS['log']->debug('current_language is: ' . $GLOBALS['current_language']);
//set module and application string arrays based upon selected language
$GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
Expand All @@ -361,11 +357,7 @@ public static function preLoadLanguages()
*/
public function loadLanguages()
{
if (!empty($_SESSION['authenticated_user_language'])) {
$GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
} else {
$GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
}
$GLOBALS['current_language'] = get_current_language();
$GLOBALS['log']->debug('current_language is: ' . $GLOBALS['current_language']);
//set module and application string arrays based upon selected language
$GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
Expand Down
16 changes: 16 additions & 0 deletions include/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,22 @@ function get_language_display($key)
return $sugar_config['languages'][$key];
}

/**
* Returns the currently active language string.
*
* @return string
*/
function get_current_language()
{
global $sugar_config;

if (!empty($_SESSION['authenticated_user_language'])) {
return $_SESSION['authenticated_user_language'];
} else {
return $sugar_config['default_language'];
}
}

function get_assigned_user_name($assigned_user_id, $is_group = '')
{
static $saved_user_list = null;
Expand Down
7 changes: 2 additions & 5 deletions modules/Activities/EmailReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
require_once("modules/Users/User.php");
require_once("modules/Contacts/Contact.php");
require_once("modules/Leads/Lead.php");
require_once("include/utils.php");

/**
* Class for sending email reminders of meetings and call to invitees
Expand Down Expand Up @@ -129,11 +130,7 @@ public function process()
*/
public function sendReminders(SugarBean $bean, Administration $admin, $recipients)
{
if (empty($_SESSION['authenticated_user_language'])) {
$current_language = $GLOBALS['sugar_config']['default_language'];
} else {
$current_language = $_SESSION['authenticated_user_language'];
}
$current_language = get_current_language();

if (!empty($bean->created_by)) {
$user_id = $bean->created_by;
Expand Down
3 changes: 2 additions & 1 deletion modules/Emails/EmailUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
die('Not A Valid Entry Point');
}

require_once("include/utils.php");
require_once("include/ytree/Tree.php");
require_once("include/ytree/ExtNode.php");
require_once("include/SugarFolders/SugarFolders.php");
Expand Down Expand Up @@ -952,7 +953,7 @@ public function getEditContact($id, $module)
$this->smarty->assign("app_strings", $app_strings);
$this->smarty->assign(
"contact_strings",
return_module_language($_SESSION['authenticated_user_language'], 'Contacts')
return_module_language(get_current_language(), 'Contacts')
);
$this->smarty->assign("contact", $contactMeta);

Expand Down
7 changes: 2 additions & 5 deletions modules/Home/QuickSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

require_once('include/SugarObjects/templates/person/Person.php');
require_once('include/MVC/SugarModule.php');
require_once('include/utils.php');

/**
* quicksearchQuery class, handles AJAX calls from quicksearch.js
Expand Down Expand Up @@ -286,11 +287,7 @@ protected function formatResults($results, $args)

// get fields to match enum vals
if (empty($app_list_strings)) {
if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}
$current_language = get_current_language();
$app_list_strings = return_app_list_strings_language($current_language);
}

Expand Down
19 changes: 7 additions & 12 deletions modules/Home/sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/

require_once("include/utils.php");

$sm = sm_build_array();
$sm_smarty = new Sugar_Smarty();

global $sugar_config;
if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}
$current_language = get_current_language();

$mod_strings = return_module_language($current_language, 'Home');
$sm_smarty->assign('CLOSE', isset($mod_strings['LBL_CLOSE_SITEMAP']) ? $mod_strings['LBL_CLOSE_SITEMAP'] : '');
Expand Down Expand Up @@ -87,19 +84,17 @@ function sm_build_array()


include("include/modules.php");
global $sugar_config,$mod_strings;
global $mod_strings;


// Need to set up mod_strings when we iterate through module menus.
$orig_modstrings = array();
if (!empty($mod_strings)) {
$orig_modstrings = $mod_strings;
}
if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}

$current_language = get_current_language();

$exclude= array(); // in case you want to exclude any.
$mstr_array = array();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ public function sendEmailPassword($user_id, $password)
$notify_mail->CharSet = $sugar_config['default_charset'];
$notify_mail->AddAddress(((!empty($row['email1']))?$row['email1']: $row['email2']), $locale->translateCharsetMIME(trim($row['first_name'] . ' ' . $row['last_name']), 'UTF-8', $OBCharset));

if (empty($_SESSION['authenticated_user_language'])) {
$current_language = $sugar_config['default_language'];
} else {
$current_language = $_SESSION['authenticated_user_language'];
}
$notify_mail->Subject = 'Sugar Token';
$notify_mail->Body = 'Your sugar session authentication token is: ' . $password;
$notify_mail->setMailerForSystem();
Expand Down
7 changes: 2 additions & 5 deletions service/JsonRPCServer/JsonRPCServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

require_once __DIR__ . '/../../soap/SoapHelperFunctions.php';
require_once __DIR__ . '/../../include/json_config.php';
require_once __DIR__ . '/../../include/utils.php';
require_once __DIR__ . '/JsonRPCServerUtils.php';
require_once __DIR__ . '/JsonRPCServerCalls.php';

Expand Down Expand Up @@ -99,11 +100,7 @@ public function run()
session_start();
$log->debug('JSON_SERVER:session started');

if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] !== '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}
$current_language = get_current_language();

$log->debug('JSON_SERVER: current_language:' . $current_language);

Expand Down
7 changes: 2 additions & 5 deletions service/JsonRPCServer/JsonRPCServerCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

require_once __DIR__ . '/../../soap/SoapHelperFunctions.php';
require_once __DIR__ . '/../../include/json_config.php';
require_once __DIR__ . '/../../include/utils.php';
require_once __DIR__ . '/JsonRPCServerUtils.php';

/**
Expand Down Expand Up @@ -174,11 +175,7 @@ public function query($request_id, $params)

// get fields to match enum vals
if (empty($app_list_strings)) {
if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] !== '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}
$current_language = get_current_language();
$app_list_strings = return_app_list_strings_language($current_language);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/phpunit/include/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ public function testunencodeMultienumValue()
$this->assertEquals(array('foo'), unencodeMultienum('^foo^'));
$this->assertEquals(array('foo', 'bar'), unencodeMultienum('^foo^,^bar^'));
}

public function testget_languages()
{
$this->assertEquals(get_languages(), ['en_us' => 'English (US)']);
$this->assertEquals(get_all_languages(), ['en_us' => 'English (US)']);
$this->assertEquals(get_language_display('en_us'), 'English (US)');
}

public function testget_current_language()
{
global $sugar_config;
$state = new StateSaver();
$state->pushGlobals();

$_SESSION['authenticated_user_language'] = 'foo';
$this->assertEquals(get_current_language(), 'foo');
$this->assertEquals(get_current_language(), 'foo');

$sugar_config['default_language'] = 'bar';
$this->assertEquals(get_current_language(), 'foo');
unset($_SESSION['authenticated_user_language']);
$this->assertEquals(get_current_language(), 'bar');

$state->popGlobals();
}
}
7 changes: 2 additions & 5 deletions vCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@


require_once('include/vCard.php');
require_once('include/utils.php');

if (isset($_SESSION['authenticated_user_language']) && $_SESSION['authenticated_user_language'] != '') {
$current_language = $_SESSION['authenticated_user_language'];
} else {
$current_language = $sugar_config['default_language'];
}

$current_language = get_current_language();
//set module and application string arrays based upon selected language
$app_strings = return_application_language($current_language);
$app_list_strings = return_app_list_strings_language($current_language);
Expand Down

0 comments on commit 55e0d46

Please sign in to comment.