From a63215f2991280d818e61ac21d75d3c04e6097fe Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 11 Jul 2014 14:47:41 +0000 Subject: [PATCH] Replace HOME_LOCALE const with get_home_locale() func --- .../Controller/Settings/CommonController.php | 2 +- .../Mibew/Controller/WidgetController.php | 2 +- .../RequestProcessor/ThreadProcessor.php | 2 +- src/mibew/libs/classes/Mibew/Settings.php | 2 +- src/mibew/libs/classes/Mibew/Thread.php | 4 +- src/mibew/libs/common/locale.php | 43 +++++++++++++------ src/mibew/libs/groups.php | 7 ++- src/mibew/libs/operator.php | 2 +- 8 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Controller/Settings/CommonController.php b/src/mibew/libs/classes/Mibew/Controller/Settings/CommonController.php index e1dbcc44..6dbe75fb 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Settings/CommonController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Settings/CommonController.php @@ -148,7 +148,7 @@ class CommonController extends AbstractController $params['left_messages_locale'] = $request->request->get('leftmessageslocale'); if (!in_array($params['left_messages_locale'], get_available_locales())) { - $params['left_messages_locale'] = HOME_LOCALE; + $params['left_messages_locale'] = get_home_locale(); } if ($params['email'] && !is_valid_email($params['email'])) { diff --git a/src/mibew/libs/classes/Mibew/Controller/WidgetController.php b/src/mibew/libs/classes/Mibew/Controller/WidgetController.php index f5539f6d..379b8548 100644 --- a/src/mibew/libs/classes/Mibew/Controller/WidgetController.php +++ b/src/mibew/libs/classes/Mibew/Controller/WidgetController.php @@ -108,7 +108,7 @@ class WidgetController extends AbstractController // Get operator info $operator = operator_by_id($thread->agentId); $locale = $request->query->get('locale', ''); - $operator_name = ($locale == HOME_LOCALE) + $operator_name = ($locale == get_home_locale()) ? $operator['vclocalename'] : $operator['vccommonname']; diff --git a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php index ad73879b..df3a3deb 100644 --- a/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php +++ b/src/mibew/libs/classes/Mibew/RequestProcessor/ThreadProcessor.php @@ -597,7 +597,7 @@ class ThreadProcessor extends ClientSideProcessor // Get message locale $message_locale = Settings::get('left_messages_locale'); if (!locale_exists($message_locale)) { - $message_locale = HOME_LOCALE; + $message_locale = get_home_locale(); } // Create thread diff --git a/src/mibew/libs/classes/Mibew/Settings.php b/src/mibew/libs/classes/Mibew/Settings.php index 2b6a564d..dcf3003a 100644 --- a/src/mibew/libs/classes/Mibew/Settings.php +++ b/src/mibew/libs/classes/Mibew/Settings.php @@ -81,7 +81,7 @@ class Settings 'max_connections_from_one_host' => 10, 'thread_lifetime' => 600, 'email' => '', /* inbox for left messages */ - 'left_messages_locale' => HOME_LOCALE, + 'left_messages_locale' => get_home_locale(), 'sendmessagekey' => 'center', 'enableban' => '0', 'enablessl' => '0', diff --git a/src/mibew/libs/classes/Mibew/Thread.php b/src/mibew/libs/classes/Mibew/Thread.php index d8a71951..52425815 100644 --- a/src/mibew/libs/classes/Mibew/Thread.php +++ b/src/mibew/libs/classes/Mibew/Thread.php @@ -698,7 +698,7 @@ class Thread */ public function checkForReassign($operator) { - $operator_name = ($this->locale == HOME_LOCALE) + $operator_name = ($this->locale == get_home_locale()) ? $operator['vclocalename'] : $operator['vccommonname']; @@ -922,7 +922,7 @@ class Thread { $take_thread = false; $message = ''; - $operator_name = ($this->locale == HOME_LOCALE) + $operator_name = ($this->locale == get_home_locale()) ? $operator['vclocalename'] : $operator['vccommonname']; diff --git a/src/mibew/libs/common/locale.php b/src/mibew/libs/common/locale.php index 04e6f3b3..63256e6d 100644 --- a/src/mibew/libs/common/locale.php +++ b/src/mibew/libs/common/locale.php @@ -23,19 +23,6 @@ use Symfony\Component\Translation\Loader\PoFileLoader; */ define('LOCALE_COOKIE_NAME', 'mibew_locale'); -// Test and set default locales - -/** - * Verified value of the $home_locale configuration parameter (see - * "configs/default_config.yml" for details) - */ -define( - 'HOME_LOCALE', - (locale_pattern_check($configs['home_locale']) && locale_exists($configs['home_locale']) - ? $configs['home_locale'] - : 'en') -); - function locale_exists($locale) { return file_exists(MIBEW_FS_ROOT . "/locales/$locale/translation.po"); @@ -136,7 +123,8 @@ function get_user_locale() /** * Returns a value of the default locale. * - * Generally it should be used if a user does not provide known lang. + * Generally, the locale returned by the function, should be used as a user + * locale if does not provide known lang. * * In fact the function returns verified value of "default_locale" variable from * the system configurations file. @@ -159,6 +147,33 @@ function get_default_locale() return $default_locale; } +/** + * Returns a value of the home locale. + * + * Generally, the locale returned by the function, should be used as a locale + * for operators' native names. + * + * In fact the function returns verified value of "home_locale" variable from + * the system configurations file. + * + * @return string Locale code. + */ +function get_home_locale() +{ + static $home_locale = null; + + if (is_null($home_locale)) { + $configs = load_system_configs(); + $is_correct = !empty($configs['home_locale']) + && locale_pattern_check($configs['home_locale']) + && locale_exists($configs['home_locale']); + + $home_locale = $is_correct ? $configs['home_locale'] : 'en'; + } + + return $home_locale; +} + /** * Retrieves locale for the current request. * diff --git a/src/mibew/libs/groups.php b/src/mibew/libs/groups.php index 439f3bd7..e08a520c 100644 --- a/src/mibew/libs/groups.php +++ b/src/mibew/libs/groups.php @@ -72,7 +72,10 @@ function group_by_name($name) */ function get_group_name($group) { - if (HOME_LOCALE == get_current_locale() || !isset($group['vccommonname']) || !$group['vccommonname']) { + $use_local_name = (get_home_locale() == get_current_locale()) + || !isset($group['vccommonname']) + || !$group['vccommonname']; + if ($use_local_name) { return $group['vclocalname']; } else { return $group['vccommonname']; @@ -242,7 +245,7 @@ function group_is_away($group) */ function get_group_description($group) { - $use_local_description = HOME_LOCALE == get_current_locale() + $use_local_description = (get_home_locale() == get_current_locale()) || !isset($group['vccommondescription']) || !$group['vccommondescription']; diff --git a/src/mibew/libs/operator.php b/src/mibew/libs/operator.php index c7cb589b..fbb02d2a 100644 --- a/src/mibew/libs/operator.php +++ b/src/mibew/libs/operator.php @@ -509,7 +509,7 @@ function is_operator_online($operator_id) */ function get_operator_name($operator) { - if (HOME_LOCALE == get_current_locale()) { + if (get_home_locale() == get_current_locale()) { return $operator['vclocalename']; } else { return $operator['vccommonname'];