From 0f65e8343ca2b8dbcf4beb675c0880a2fbb61b53 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 30 Aug 2013 23:18:54 -0700 Subject: [PATCH] CRM-13187 - CRM_Admin_Form_WordReplacements Patch the admin UI for word-replacements so that it reads and writes records in the civicrm_word_replacement table. To make this work without a total rewrite, we add two steps: * At the beginning, call the "civicrm_word_replacement=>civicrm_domain" logic (This logic is already written -- it's in the BAO.) * At the end, call the the "civicrm_domain=>civicrm_word_replacement" logic (This logic is already written -- copied from the upgrader to the BAO.) This is inefficient but much simpler than rewriting the UI. ---------------------------------------- * CRM-13187: hrui: Change breadcrumb from "CiviCRM" to "CiviHR" http://issues.civicrm.org/jira/browse/CRM-13187 --- CRM/Admin/Form/WordReplacements.php | 14 +++-- CRM/Core/BAO/WordReplacement.php | 67 ++++++++++++++++++++++++ CRM/Upgrade/Incremental/php/FourFour.php | 12 +++++ 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/CRM/Admin/Form/WordReplacements.php b/CRM/Admin/Form/WordReplacements.php index f10d68701c..884cc6526a 100644 --- a/CRM/Admin/Form/WordReplacements.php +++ b/CRM/Admin/Form/WordReplacements.php @@ -40,6 +40,12 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { protected $_defaults = NULL; function preProcess() { + // This controller was originally written to CRUD $config->locale_custom_strings, + // but that's no longer the canonical store. Re-sync from canonical store to ensure + // that we display that latest data. This is inefficient - at some point, we + // should rewrite this UI. + CRM_Core_BAO_WordReplacement::rebuild(); + $this->_soInstance = CRM_Utils_Array::value('instance', $_GET); $this->assign('soInstance', $this->_soInstance); $breadCrumbUrl = CRM_Utils_System::url('civicrm/admin/options/wordreplacements', @@ -256,10 +262,10 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); if ($wordReplacementSettings) { - // Reset navigation - CRM_Core_BAO_Navigation::resetNavigation(); - // Clear js string cache - CRM_Core_Resources::singleton()->flushStrings(); + // 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', diff --git a/CRM/Core/BAO/WordReplacement.php b/CRM/Core/BAO/WordReplacement.php index 9c101dca23..5cab289efe 100644 --- a/CRM/Core/BAO/WordReplacement.php +++ b/CRM/Core/BAO/WordReplacement.php @@ -177,6 +177,8 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); if ($wordReplacementSettings) { + CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride; + // Reset navigation CRM_Core_BAO_Navigation::resetNavigation(); // Clear js string cache @@ -187,5 +189,70 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { return FALSE; } + + /** + * Get all the word-replacements stored in config-arrays + * and convert them to params for the WordReplacement.create API. + * + * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and + * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade + * step behaves consistently even as the BAO evolves in future versions. + * However, if there's a bug in here prior to 4.4.0, we should apply the + * bugfix in both places. + * + * @param bool $rebuildEach whether to perform rebuild after each individual API call + * @return array Each item is $params for WordReplacement.create + * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams + */ + static function getConfigArraysAsAPIParams($rebuildEach) { + $wordReplacementCreateParams = array(); + // get all domains + $result = civicrm_api3('domain', 'get', array( + 'return' => array('locale_custom_strings'), + )); + if (!empty($result["values"])) { + foreach ($result["values"] as $value) { + $params = array(); + $params["is_active"] = TRUE; + $params["domain_id"] = $value["id"]; + $params["options"] = array('wp-rebuild' => $rebuildEach); + // unserialize word match string + $localeCustomArray = unserialize($value["locale_custom_strings"]); + if (!empty($localeCustomArray)) { + $wordMatchArray = array(); + foreach ($localeCustomArray as $localCustomData) { + $wordMatchArray = $localCustomData["enabled"]["wildcardMatch"]; + } + + if (!empty($wordMatchArray)) { + foreach ($wordMatchArray as $word => $replace) { + $params["find_word"] = $word; + $params["replace_word"] = $replace; + $wordReplacementCreateParams[] = $params; + } + } + } + } + } + return $wordReplacementCreateParams; + } + + /** + * Get all the word-replacements stored in config-arrays + * and write them out as records in civicrm_word_replacement. + * + * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and + * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade + * step behaves consistently even as the BAO evolves in future versions. + * However, if there's a bug in here prior to 4.4.0, we should apply the + * bugfix in both places. + */ + public static function rebuildWordReplacementTable() { + civicrm_api3('word_replacement', 'replace', array( + 'options' => array('match' => array('domain_id', 'find_word')), + 'values' => self::getConfigArraysAsAPIParams(FALSE), + )); + CRM_Core_BAO_WordReplacement::rebuild(); + } } diff --git a/CRM/Upgrade/Incremental/php/FourFour.php b/CRM/Upgrade/Incremental/php/FourFour.php index 6ad741a72a..1d412112d9 100644 --- a/CRM/Upgrade/Incremental/php/FourFour.php +++ b/CRM/Upgrade/Incremental/php/FourFour.php @@ -267,6 +267,12 @@ CREATE TABLE IF NOT EXISTS civicrm_word_replacement ( * Get all the word-replacements stored in config-arrays * and convert them to params for the WordReplacement.create API. * + * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and + * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade + * step behaves consistently even as the BAO evolves in future versions. + * However, if there's a bug in here prior to 4.4.0, we should apply the + * bugfix in both places. + * * @param bool $rebuildEach whether to perform rebuild after each individual API call * @return array Each item is $params for WordReplacement.create * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams @@ -307,6 +313,12 @@ CREATE TABLE IF NOT EXISTS civicrm_word_replacement ( /** * Get all the word-replacements stored in config-arrays * and write them out as records in civicrm_word_replacement. + * + * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and + * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade + * step behaves consistently even as the BAO evolves in future versions. + * However, if there's a bug in here prior to 4.4.0, we should apply the + * bugfix in both places. */ public static function rebuildWordReplacementTable() { civicrm_api3('word_replacement', 'replace', array( -- 2.25.1