Skip to content

Commit

Permalink
Add upgrade routine to convert on_hold to an array for sites with
Browse files Browse the repository at this point in the history
civimail_multiple_bulk_emails set.

with that set we get a select box which needs an array rather than a checkbox.

Note from my testing there is problem loading the defaults due to the field name being
wrong - this aligns groups created before & after the 5.9 upgrade but does not resolve that.

    // preferred communication method
    if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
      ->addSelect('email_on_hold',
        array('entity' => 'email', 'multiple' => 'multiple', 'label' => ts('Email On Hold'), 'options' => CRM_Core_PseudoConstant::emailOnHoldOptions()));
    }
    else {
      ->add('advcheckbox', 'email_on_hold', ts('Email On Hold')
  • Loading branch information
eileenmcnaughton committed Mar 1, 2019
1 parent 08cacc3 commit abe5b62
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
42 changes: 37 additions & 5 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ public function datePickerConversion($fields) {
}

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)) {
Expand All @@ -118,6 +114,30 @@ public function datePickerConversion($fields) {
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -162,4 +182,16 @@ protected function getConvertedDateValue($dateValue) {
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;
}

}
23 changes: 23 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveEleven.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
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;
}

}
20 changes: 20 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {

public function tearDown() {
$this->quickCleanup(['civicrm_saved_search']);
}

/**
* Test message upgrade process.
*/
Expand Down Expand Up @@ -99,4 +103,20 @@ public function testSmartGroupDatePickerConversion() {
$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]);
}

}

0 comments on commit abe5b62

Please sign in to comment.