}
foreach ($fields as $field) {
- $savedSearches = civicrm_api3('SavedSearch', 'get', [
- 'options' => ['limit' => 0],
- 'form_values' => ['LIKE' => "%{$field}%"],
- ])['values'];
- foreach ($savedSearches as $savedSearch) {
+ foreach ($this->getSearchesWithField($field) as $savedSearch) {
$formValues = $savedSearch['form_values'];
foreach ($formValues as $index => $formValue) {
if (in_array($formValue[0], $fieldPossibilities)) {
}
}
+ /**
+ * Conversion routine for a form change change from = string to IN array.
+ *
+ * For example a checkbox expected [$fieldName, '=', 1]
+ * whereas select expects [$fieldName, 'IN', [1]]
+ *
+ * @param string $field
+ */
+ public function convertEqualsStringToInArray($field) {
+ foreach ($this->getSearchesWithField($field) as $savedSearch) {
+ $formValues = $savedSearch['form_values'];
+ foreach ($formValues as $index => $formValue) {
+ if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
+ $formValues[$index][1] = 'IN';
+ $formValues[$index][2] = [$formValue[2]];
+ }
+ }
+
+ if ($formValues !== $savedSearch['form_values']) {
+ civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
+ }
+ }
+ }
+
/**
* Update message templates.
*/
return $dateValue;
}
+ /**
+ * @param $field
+ * @return mixed
+ */
+ protected function getSearchesWithField($field) {
+ $savedSearches = civicrm_api3('SavedSearch', 'get', [
+ 'options' => ['limit' => 0],
+ 'form_values' => ['LIKE' => "%{$field}%"],
+ ])['values'];
+ return $savedSearches;
+ }
+
}
public function upgrade_5_11_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
$this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', $rev);
+ if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
+ $this->addTask('Update any on hold groups to reflect field change', 'updateOnHold', $rev);
+ }
+ }
+
+ /**
+ * Upgrade function.
+ *
+ * @param string $rev
+ */
+ public function upgrade_5_11_beta1($rev) {
+ if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
+ $this->addTask('Update any on hold groups to reflect field change', 'updateOnHold', $rev);
+ }
+ }
+
+ /**
+ * Update on hold groups -note the core function layout for this sort of upgrade changed in 5.12 - don't copy this.
+ */
+ public function updateOnHold($ctx, $version) {
+ $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups($version);
+ $groupUpdateObject->convertEqualsStringToInArray('on_hold');
+ return TRUE;
}
}
*/
class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
+ public function tearDown() {
+ $this->quickCleanup(['civicrm_saved_search']);
+ }
+
/**
* Test message upgrade process.
*/
$this->assertEquals('2019-01-22 00:00:00', $savedSearch['form_values'][1][2]);
}
+ /**
+ * Test conversion of on hold group.
+ */
+ public function testOnHoldConversion() {
+ $this->callAPISuccess('SavedSearch', 'create', [
+ 'form_values' => [
+ ['on_hold', '=', '1'],
+ ]
+ ]);
+ $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups('5.11.alpha1');
+ $smartGroupConversionObject->convertEqualsStringToInArray('on_hold');
+ $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
+ $this->assertEquals('IN', $savedSearch['form_values'][0][1]);
+ $this->assertEquals(['1'], $savedSearch['form_values'][0][2]);
+ }
+
}