CRM-13655 - WordReplacement.xml - Update UNIQUE key for new installations
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
index dadc4f00de0b15ef07d289eb5af0b610a0051087..e7c915b19987bcc9d4345d26f32d8b822e58e844 100644 (file)
@@ -62,27 +62,27 @@ class CRM_Upgrade_Incremental_php_FourFour {
     if ($rev == '4.4.alpha1') {
       $config = CRM_Core_Config::singleton();
       if (!empty($config->useIDS)) {
-        $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system");
+        $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system.");
       }
     }
   }
 
   function upgrade_4_4_alpha1($rev) {
     // task to process sql
-    $this->addTask(ts('Upgrade DB to 4.4.alpha1: SQL'), 'task_4_4_x_runSql', $rev);
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.alpha1')), 'task_4_4_x_runSql', $rev);
 
     // Consolidate activity contacts CRM-12274.
-    $this->addTask(ts('Consolidate activity contacts'), 'activityContacts');
+    $this->addTask('Consolidate activity contacts', 'activityContacts');
 
     return TRUE;
   }
 
   function upgrade_4_4_beta1($rev) {
-    $this->addTask(ts('Upgrade DB to 4.4.beta1: SQL'), 'task_4_4_x_runSql', $rev);
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.beta1')), 'task_4_4_x_runSql', $rev);
 
     // add new 'data' column in civicrm_batch
     $query = 'ALTER TABLE civicrm_batch ADD data LONGTEXT NULL COMMENT "cache entered data"';
-    CRM_Core_DAO::executeQuery($query);
+    CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
 
     // check if batch entry data exists in civicrm_cache table
     $query = 'SELECT path, data FROM civicrm_cache WHERE group_name = "batch entry"';
@@ -99,6 +99,48 @@ class CRM_Upgrade_Incremental_php_FourFour {
     // delete entries from civicrm_cache table
     $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"';
     CRM_Core_DAO::executeQuery($query);
+
+    $this->addTask('Migrate custom word-replacements', 'wordReplacements');
+  }
+
+  function upgrade_4_4_1($rev) {
+    // CRM-13327 upgrade handling for the newly added name badges
+    $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'name_badge', 'id', 'name');
+    $nameBadges = array_flip(array_values(CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('name_badge')));
+    unset($nameBadges['Avery 5395']);
+    if (!empty($nameBadges)) {
+      $dimension = '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}';
+      $query = "UPDATE civicrm_option_value
+        SET value = '{$dimension}'
+        WHERE option_group_id = %1 AND name = 'Fattorini Name Badge 100x65'";
+
+      CRM_Core_DAO::executeQuery($query, array(1 => array($ogID, 'Integer')));
+    }
+    else {
+      $dimensions = array(
+        1 => '{"paper-size":"a4","orientation":"landscape","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":1,"metric":"mm","lMargin":25,"tMargin":27,"SpaceX":0,"SpaceY":35,"width":106,"height":150,"lPadding":5,"tPadding":5}',
+        2 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}',
+        3 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":2,"metric":"mm","lMargin":10,"tMargin":28,"SpaceX":0,"SpaceY":0,"width":96,"height":121,"lPadding":5,"tPadding":5}',
+      );
+      $insertStatements = array(
+        1 => "($ogID, %1, '{$dimensions[1]}', %1, NULL, 0, NULL, 2, NULL, 0, 0, 1, NULL, NULL)",
+        2 => "($ogID, %2, '{$dimensions[2]}', %2, NULL, 0, NULL, 3, NULL, 0, 0, 1, NULL, NULL)",
+        3 => "($ogID, %3, '{$dimensions[3]}', %3, NULL, 0, NULL, 4, NULL, 0, 0, 1, NULL, NULL)",
+      );
+
+      $queryParams = array(
+        1 => array('A6 Badge Portrait 150x106', 'String'),
+        2 => array('Fattorini Name Badge 100x65', 'String'),
+        3 => array('Hanging Badge 3-3/4" x 4-3"/4', 'String'),
+      );
+      
+      foreach ($insertStatements as $values) {
+        $query = 'INSERT INTO civicrm_option_value (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`) VALUES' . $values;
+        CRM_Core_DAO::executeQuery($query, $queryParams);
+      }
+    }
+
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.1')), 'task_4_4_x_runSql', $rev);
   }
 
   /**
@@ -203,6 +245,32 @@ WHERE       source_contact_id IS NOT NULL";
    return TRUE;
   }
 
+  /**
+   * Migrate word-replacements from $config to civicrm_word_replacement
+   *
+   * @return bool TRUE for success
+   * @see http://issues.civicrm.org/jira/browse/CRM-13187
+   */
+  static function wordReplacements(CRM_Queue_TaskContext $ctx) {
+    $query = "
+CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` (
+     `id` int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Word replacement ID',
+     `find_word` varchar(255)    COMMENT 'Word which need to be replaced',
+     `replace_word` varchar(255)    COMMENT 'Word which will replace the word in find',
+     `is_active` tinyint    COMMENT 'Is this entry active?',
+     `match_type` enum('wildcardMatch', 'exactMatch')   DEFAULT 'wildcardMatch',
+     `domain_id` int unsigned    COMMENT 'FK to Domain ID. This is for Domain specific word replacement',
+    PRIMARY KEY ( `id` ),
+    UNIQUE INDEX `UI_find`(find_word),
+    CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
+)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;
+    ";
+    $dao = CRM_Core_DAO::executeQuery($query);
+
+    self::rebuildWordReplacementTable();
+    return TRUE;
+  }
+
   /**
    * (Queue Task Callback)
    */
@@ -236,4 +304,73 @@ WHERE       source_contact_id IS NOT NULL";
     );
     $queue->createItem($task, array('weight' => -1));
   }
+
+  /**
+   * 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 $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();
+  }
 }