Merge pull request #2329 from davecivicrm/CRM-13726
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
index 4968492a4efb755105ff08a34334e2f9fe4dfae5..3becc858d1de8c99e8e7bf9a3250028606d5c9a8 100644 (file)
@@ -40,10 +40,10 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
 
   function __construct() {
     parent::__construct();
-  }  
+  }
   /**
    * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. 
+   * retrieves the relevant objects.
    *
    * @param array $params   (reference ) an assoc array of name/value pairs
    * @param array $defaults (reference ) an assoc array to hold the flattened values
@@ -52,7 +52,7 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
    * @access public
    * @static
    */
-    
+
   static function retrieve(&$params, &$defaults) {
     return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
   }
@@ -64,10 +64,10 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
    * @access public
    * @static
    */
-  static function getWordReplacement($reset = null) {
+  static function getWordReplacement($reset = NULL) {
     static $wordReplacement = NULL;
     if (!$wordReplacement || $reset) {
-      $wordReplacement = new CRM_Core_BAO_WordRepalcement();
+      $wordReplacement = new CRM_Core_BAO_WordReplacement();
       $wordReplacement->id = CRM_Core_Config::wordReplacementID();
       if (!$wordReplacement->find(TRUE)) {
         CRM_Core_Error::fatal();
@@ -88,7 +88,9 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
     $wordReplacement->id = $id;
     $wordReplacement->copyValues($params);
     $wordReplacement->save();
-    self::rebuild();
+    if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
+      self::rebuild();
+    }
     return $wordReplacement;
   }
 
@@ -99,16 +101,18 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
    * @access public
    */
   static function create($params) {
-    if(array_key_exists("domain_id",$params) === false) {
-      $params["domain_id"] = CRM_Core_Config::domainID();  
-    }  
+    if(array_key_exists("domain_id",$params) === FALSE) {
+      $params["domain_id"] = CRM_Core_Config::domainID();
+    }
     $wordReplacement = new CRM_Core_DAO_WordReplacement();
     $wordReplacement->copyValues($params);
     $wordReplacement->save();
-    self::rebuild();
+    if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
+      self::rebuild();
+    }
     return $wordReplacement;
   }
-  
+
   /**
    * Delete website
    *
@@ -121,26 +125,39 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
     $dao = new CRM_Core_DAO_WordReplacement();
     $dao->id = $id;
     $dao->delete();
-    self::rebuild();
+    if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
+      self::rebuild();
+    }
     return $dao;
   }
-  
-  /*
-   *   Rebuild 
+
+  /**
+   * Get all word-replacements in the form of an array
+   *
+   * @param int $id domain ID
+   * @return array
+   * @see civicrm_domain.locale_custom_strings
    */
+  public static function getAllAsConfigArray($id) {
+    $query = "
+SELECT find_word,replace_word,is_active,match_type
+FROM   civicrm_word_replacement
+WHERE  domain_id = %1
+";
+    $params = array( 1 => array($id, 'Integer'));
+
+    $dao = CRM_Core_DAO::executeQuery($query, $params);
+
+    $overrides = array();
 
-  static function rebuild() {
-    $id = CRM_Core_Config::domainID();  
-    $query = "SELECT find_word,replace_word FROM civicrm_word_replacement WHERE is_active = 1 AND domain_id = ".CRM_Utils_Type::escape($id, 'Integer');
-    $dao = CRM_Core_DAO::executeQuery($query);
-    $wordReplacement = array(); 
-    
     while ($dao->fetch()) {
-      $wordReplacement[$dao->find_word] = $dao->replace_word;
+      if ($dao->is_active==1) {
+        $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
+      }
+      else {
+        $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
+      }
     }
-    
-    $overrides['enabled']['wildcardMatch'] = $wordReplacement;
-    
     $config = CRM_Core_Config::singleton();
     $domain = new CRM_Core_DAO_Domain();
     $domain->find(TRUE);
@@ -149,30 +166,105 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
       // for multilingual
       $addReplacements = $config->localeCustomStrings;
       $addReplacements[$config->lcMessages] = $overrides;
-      $stringOverride = serialize($addReplacements);
+      $stringOverride = $addReplacements;
     }
     else {
       // for single language
-      $stringOverride = serialize(array($config->lcMessages => $overrides));
+      $stringOverride = array($config->lcMessages => $overrides);
     }
 
-    $params = array('locale_custom_strings' => $stringOverride);
-    
+    return $stringOverride;
+  }
 
+  /**
+   * Rebuild
+   */
+  static function rebuild() {
+    $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;
+
       // Reset navigation
       CRM_Core_BAO_Navigation::resetNavigation();
       // Clear js string cache
       CRM_Core_Resources::singleton()->flushStrings();
-      
-      return true;
+
+      return TRUE;
+    }
+
+    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["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();
+          // Traverse Language array
+          foreach ($localeCustomArray as $localCustomData) {
+          // Traverse status array "enabled" "disabled"
+            foreach ($localCustomData as $status => $matchTypes) {
+              $params["is_active"] = ($status == "enabled")?TRUE:FALSE;
+              // Traverse Match Type array "wildcardMatch" "exactMatch"
+              foreach ($matchTypes as $matchType => $words) {
+                $params["match_type"] = $matchType;
+                foreach ($words as $word => $replace) {
+                  $params["find_word"] = $word;
+                  $params["replace_word"] = $replace;
+                  $wordReplacementCreateParams[] = $params;
+                }
+              }
+            }
+          }
+        }
+      }
     }
-    
-    return false;
-   
+    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();
   }
-    
 }