CRM-15908 fix - saved searches that use multi-select custom groups break during 4...
authormonishdeb <monish.deb@webaccessglobal.com>
Thu, 12 Feb 2015 14:51:21 +0000 (20:21 +0530)
committerJamie McClelland <jm@mayfirst.org>
Fri, 6 Mar 2015 19:39:26 +0000 (14:39 -0500)
https://issues.civicrm.org/jira/browse/CRM-15908

CRM/Upgrade/Incremental/php/FourFive.php [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index db9e389..c001277
@@ -302,6 +302,57 @@ DROP KEY `{$dao->CONSTRAINT_NAME}`";
     return TRUE;
   }
 
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_4_5_6($rev) {
+    // Task to process sql.
+    $this->addTask(ts('Upgrade DB to 4.5.6: Fix saved searches consisting of multi-choice custom field(s)'), 'updateSavedSearch');
+
+    return TRUE;
+  }
+
+  /**
+   * Update saved search for multi-select custom fields on DB upgrade
+   *
+   * @param CRM_Queue_TaskContext $ctx
+   *
+   * @return bool TRUE for success
+   */
+  static function updateSavedSearch(CRM_Queue_TaskContext $ctx) {
+    $sql = "SELECT id, form_values FROM civicrm_saved_search";
+    $dao = CRM_Core_DAO::executeQuery($sql);
+    while ($dao->fetch()) {
+      $formValues = unserialize($dao->form_values);
+      while (list($field, $data_value) = each($formValues)) {
+        if (preg_match('/^custom_/', $field) && is_array($data_value)) {
+          // This indicates old-style data format. We need to fix it.
+          $op = 'and';
+          if ($key = array_search('CiviCRM_OP_OR', $data_value)) {
+            $op = 'or';
+            unset($formValues[$field][$key]);
+          }
+
+          // Add new key for the operator.
+          $formValues["${field}_operator"] = $op;
+
+          $sql = "UPDATE civicrm_saved_search SET form_values = %0 WHERE id = %1";
+          CRM_Core_DAO::executeQuery($sql,
+            array(
+              array(serialize($formValues), 'String'),
+              array($dao->id, 'Integer'),
+            )
+          );
+        }
+      }
+    }
+
+    return TRUE;
+  }
+
+
   /**
    * (Queue Task Callback)
    */