From 234d8f09414678e7b8e94cada91637de1f89e582 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 27 Aug 2015 19:29:24 -0700 Subject: [PATCH] CRM-16373, CRM-14349 - Config - Disintermediate localeCustomStrings localeCustomStrings has a pretty w.e.i.r.d. lifecycle (involving table `civicrm_word_replacements` and column `civicrm_domain.locale_cusotm_Strings`). This commit does not unweird it, but the weirdness no longer involves special-case logic on `CRM_Core_Config*` -- all the weirdness is concentrated in `CRM_Core_BAO_WordReplacements` and `CRM_Admin_Form_WordReplacements`. --- CRM/Admin/Form/WordReplacements.php | 60 +++++--------------- CRM/Core/BAO/ConfigSetting.php | 20 +------ CRM/Core/BAO/WordReplacement.php | 85 ++++++++++++++++++++--------- CRM/Core/Config/Variables.php | 7 --- CRM/Core/I18n.php | 13 +++-- 5 files changed, 83 insertions(+), 102 deletions(-) diff --git a/CRM/Admin/Form/WordReplacements.php b/CRM/Admin/Form/WordReplacements.php index 070de44dee..82ff6df5ee 100644 --- a/CRM/Admin/Form/WordReplacements.php +++ b/CRM/Admin/Form/WordReplacements.php @@ -57,7 +57,7 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { * @return array */ public function setDefaultValues() { - if ($this->_defaults !== NULL) { + if (!empty($this->_defaults)) { return $this->_defaults; } @@ -65,7 +65,7 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { $config = CRM_Core_Config::singleton(); - $values = $config->localeCustomStrings[$config->lcMessages]; + $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages); $i = 1; $enableDisable = array( @@ -89,20 +89,6 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { } } - $name = $this->_stringName = "custom_string_override_{$config->lcMessages}"; - if (isset($config->$name) && - is_array($config->$name) - ) { - $this->_numStrings = 1; - foreach ($config->$name as $old => $newValues) { - $this->_numStrings++; - $this->_numStrings += 9; - } - } - else { - $this->_numStrings = 10; - } - return $this->_defaults; } @@ -111,7 +97,7 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { */ public function buildQuickForm() { $config = CRM_Core_Config::singleton(); - $values = $config->localeCustomStrings[$config->lcMessages]; + $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages); //CRM-14179 $instances = 0; @@ -237,37 +223,17 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { ); $config = CRM_Core_Config::singleton(); + CRM_Core_BAO_WordReplacement::setLocaleCustomStrings($config->lcMessages, $overrides); - $domain = new CRM_Core_DAO_Domain(); - $domain->find(TRUE); - - if ($domain->locales && $config->localeCustomStrings) { - // for multilingual - $addReplacements = $config->localeCustomStrings; - $addReplacements[$config->lcMessages] = $overrides; - $stringOverride = serialize($addReplacements); - } - else { - // for single language - $stringOverride = serialize(array($config->lcMessages => $overrides)); - } - - $params = array('locale_custom_strings' => $stringOverride); - $id = CRM_Core_Config::domainID(); - - $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); - - if ($wordReplacementSettings) { - // This controller was originally written to CRUD $config->locale_custom_strings, - // but that's no longer the canonical store. Sync changes to canonical store. - // This is inefficient - at some point, we should rewrite this UI. - CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable(); - - CRM_Core_Session::setStatus("", ts("Settings Saved"), "success"); - CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements', - "reset=1" - )); - } + // This controller was originally written to CRUD $config->locale_custom_strings, + // but that's no longer the canonical store. Sync changes to canonical store. + // This is inefficient - at some point, we should rewrite this UI. + CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable(); + + CRM_Core_Session::setStatus("", ts("Settings Saved"), "success"); + CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements', + "reset=1" + )); } } diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 4868c5ce82..9d6813bcf6 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -69,13 +69,6 @@ class CRM_Core_BAO_ConfigSetting { self::formatParams($params, $values); } - // CRM-6151 - if (isset($params['localeCustomStrings']) && - is_array($params['localeCustomStrings']) - ) { - $domain->locale_custom_strings = serialize($params['localeCustomStrings']); - } - // unset any of the variables we read from file that should not be stored in the database // the username and certpath are stored flat with _test and _live // check CRM-1470 @@ -189,11 +182,8 @@ class CRM_Core_BAO_ConfigSetting { if (CRM_Core_Config::isUpgradeMode()) { $domain->selectAdd('config_backend'); } - elseif (CRM_Utils_Array::value($urlVar, $_GET) == 'admin/modules/list/confirm') { - $domain->selectAdd('config_backend', 'locales'); - } else { - $domain->selectAdd('config_backend, locales, locale_custom_strings'); + $domain->selectAdd('config_backend, locales'); } $domain->id = CRM_Core_Config::domainID(); @@ -212,14 +202,6 @@ class CRM_Core_BAO_ConfigSetting { } } - // check if there are any locale strings - if ($domain->locale_custom_strings) { - $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings); - } - else { - $defaults['localeCustomStrings'] = NULL; - } - // are we in a multi-language setup? $multiLang = $domain->locales ? TRUE : FALSE; diff --git a/CRM/Core/BAO/WordReplacement.php b/CRM/Core/BAO/WordReplacement.php index df2e8d2e20..cddb1c78c0 100644 --- a/CRM/Core/BAO/WordReplacement.php +++ b/CRM/Core/BAO/WordReplacement.php @@ -172,16 +172,11 @@ WHERE domain_id = %1 $domain = new CRM_Core_DAO_Domain(); $domain->find(TRUE); - if ($domain->locales && $config->localeCustomStrings) { - // for multilingual - $addReplacements = $config->localeCustomStrings; - $addReplacements[$config->lcMessages] = $overrides; - $stringOverride = $addReplacements; - } - else { - // for single language - $stringOverride = array($config->lcMessages => $overrides); - } + // So. Weird. Some bizarre/probably-broken multi-lingual thing where + // data isn't really stored in civicrm_word_replacements. Probably + // shouldn't exist. + $stringOverride = self::_getLocaleCustomStrings($id); + $stringOverride[$config->lcMessages] = $overrides; return $stringOverride; } @@ -195,24 +190,17 @@ WHERE domain_id = %1 */ public static function rebuild($clearCaches = TRUE) { $id = CRM_Core_Config::domainID(); - $stringOverride = self::getAllAsConfigArray($id); - $params = array('locale_custom_strings' => serialize($stringOverride)); - $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); - if ($wordReplacementSettings) { - CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride; - - // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally - if ($clearCaches) { - // Reset navigation - CRM_Core_BAO_Navigation::resetNavigation(); - // Clear js localization - CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); - } + self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id)); - return TRUE; + // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally + if ($clearCaches) { + // Reset navigation + CRM_Core_BAO_Navigation::resetNavigation(); + // Clear js localization + CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); } - return FALSE; + return TRUE; } /** @@ -291,4 +279,51 @@ WHERE domain_id = %1 CRM_Core_BAO_WordReplacement::rebuild(); } + /** + * Get WordReplacements for a locale. + * + * @param string $locale + * @return array + * List of word replacements (enabled/disabled) for the given locale. + */ + public static function getLocaleCustomStrings($locale, $domainId = NULL) { + if ($domainId === NULL) { + $domainId = CRM_Core_Config::domainID(); + } + + return CRM_Utils_Array::value($locale, self::_getLocaleCustomStrings($domainId)); + } + + private static function _getLocaleCustomStrings($domainId) { + // TODO: Would it be worthwhile using memcache here? + $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array( + 1 => array($domainId, 'Integer'), + )); + while ($domain->fetch()) { + return empty($domain->locale_custom_strings) ? array() : unserialize($domain->locale_custom_strings); + } + } + + public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) { + if ($domainId === NULL) { + $domainId = CRM_Core_Config::domainID(); + } + + $lcs = self::_getLocaleCustomStrings($domainId); + $lcs[$locale] = $values; + + self::_setLocaleCustomStrings($domainId, $lcs); + } + + /** + * @param $domainId + * @param $lcs + */ + private static function _setLocaleCustomStrings($domainId, $lcs) { + CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array( + 1 => array(serialize($lcs), 'String'), + 2 => array($domainId, 'Integer'), + )); + } + } diff --git a/CRM/Core/Config/Variables.php b/CRM/Core/Config/Variables.php index e94ffad414..cce398e767 100644 --- a/CRM/Core/Config/Variables.php +++ b/CRM/Core/Config/Variables.php @@ -247,13 +247,6 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { public $maxFileSize = 2; - /** - * The custom locale strings. Note that these locale strings are stored - * in a separate column in civicrm_domain - * @var array - */ - public $localeCustomStrings = NULL; - /** * Map Provider * diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 9822d440c7..b0a8ad5ed3 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -337,10 +337,15 @@ class CRM_Core_I18n { // do all wildcard translations first $config = CRM_Core_Config::singleton(); - $stringTable = CRM_Utils_Array::value( - $config->lcMessages, - $config->localeCustomStrings - ); + if (!isset(Civi::$statics[__CLASS__][$config->lcMessages])) { + if ($config->dsn && !CRM_Core_Config::isUpgradeMode()) { + Civi::$statics[__CLASS__][$config->lcMessages] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages); + } + else { + Civi::$statics[__CLASS__][$config->lcMessages] = array(); + } + } + $stringTable = Civi::$statics[__CLASS__][$config->lcMessages]; $exactMatch = FALSE; if (isset($stringTable['enabled']['exactMatch'])) { -- 2.25.1