From bb606fef9600ebb51407138c3d558ac088bffb22 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Thu, 12 Feb 2015 20:21:21 +0530 Subject: [PATCH] CRM-15908 fix - saved searches that use multi-select custom groups break during 4.4 -> 4.5 upgrade https://issues.civicrm.org/jira/browse/CRM-15908 --- CRM/Upgrade/Incremental/php/FourFive.php | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) mode change 100644 => 100755 CRM/Upgrade/Incremental/php/FourFive.php diff --git a/CRM/Upgrade/Incremental/php/FourFive.php b/CRM/Upgrade/Incremental/php/FourFive.php old mode 100644 new mode 100755 index db9e389184..c001277db0 --- a/CRM/Upgrade/Incremental/php/FourFive.php +++ b/CRM/Upgrade/Incremental/php/FourFive.php @@ -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) */ -- 2.25.1