From f6ef2e15968350bf8e382980242a17436669b23d Mon Sep 17 00:00:00 2001
From: Allen Shaw
Date: Tue, 16 Oct 2018 09:07:34 -0500
Subject: [PATCH] Toward CRM-19751: conditionally change On-Hold criteria to
select on Advanced Search form.
---
CRM/Admin/Form/Preferences/Mailing.php | 2 +-
CRM/Contact/BAO/Query.php | 5 +-
CRM/Contact/Form/Search/Criteria.php | 10 +-
CRM/Upgrade/Incremental/php/FiveNine.php | 94 +++++++++++++++++++
CRM/Upgrade/Incremental/sql/5.9.0.mysql.tpl | 1 +
.../Fields/preferred_communication_method.tpl | 8 +-
.../CRM/Contact/Form/Search/CriteriaTest.php | 62 ++++++++++++
7 files changed, 175 insertions(+), 7 deletions(-)
create mode 100644 CRM/Upgrade/Incremental/php/FiveNine.php
create mode 100644 CRM/Upgrade/Incremental/sql/5.9.0.mysql.tpl
create mode 100644 tests/phpunit/CRM/Contact/Form/Search/CriteriaTest.php
diff --git a/CRM/Admin/Form/Preferences/Mailing.php b/CRM/Admin/Form/Preferences/Mailing.php
index e26a56a57fe1..28bb409a38b5 100644
--- a/CRM/Admin/Form/Preferences/Mailing.php
+++ b/CRM/Admin/Form/Preferences/Mailing.php
@@ -70,7 +70,7 @@ public function preProcess() {
'html_type' => 'checkbox',
'title' => ts('Enable multiple bulk email address for a contact.'),
'weight' => 5,
- 'description' => ts('CiviMail will deliver a copy of the email to each bulk email listed for the contact.'),
+ 'description' => ts('CiviMail will deliver a copy of the email to each bulk email listed for the contact. Enabling this setting will also change the options for the "Email on Hold" field in Advanced Search.'),
),
'civimail_server_wide_lock' => array(
'html_type' => 'checkbox',
diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php
index 027d8165dfdf..ccbbafb72383 100644
--- a/CRM/Contact/BAO/Query.php
+++ b/CRM/Contact/BAO/Query.php
@@ -1635,8 +1635,9 @@ public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals
}
}
elseif ($id == 'email_on_hold') {
- if ($formValues['email_on_hold']['on_hold']) {
- $params[] = array('on_hold', '=', $formValues['email_on_hold']['on_hold'], 0, 0);
+ if ($onHoldValue = CRM_Utils_Array::value('email_on_hold', $formValues)) {
+ $onHoldValue = (array) $onHoldValue;
+ $params[] = array('on_hold', 'IN', $onHoldValue, 0, 0);
}
}
elseif (substr($id, 0, 7) == 'custom_'
diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php
index 845ab7c9a541..012500df3a47 100644
--- a/CRM/Contact/Form/Search/Criteria.php
+++ b/CRM/Contact/Form/Search/Criteria.php
@@ -231,9 +231,13 @@ public static function basic(&$form) {
$form->addRadio('privacy_toggle', ts('Privacy Options'), $options, array('allowClear' => FALSE));
// preferred communication method
-
- $onHold[] = $form->createElement('advcheckbox', 'on_hold', NULL, '');
- $form->addGroup($onHold, 'email_on_hold', ts('Email On Hold'));
+ if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
+ $form->addSelect('email_on_hold',
+ array('entity' => 'email', 'multiple' => 'multiple', 'label' => ts('Email On Hold'), 'options' => CRM_Core_PseudoConstant::emailOnHoldOptions()));
+ }
+ else {
+ $form->add('advcheckbox', 'email_on_hold', ts('Email On Hold'));
+ }
$form->addSelect('preferred_communication_method',
array('entity' => 'contact', 'multiple' => 'multiple', 'label' => ts('Preferred Communication Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')));
diff --git a/CRM/Upgrade/Incremental/php/FiveNine.php b/CRM/Upgrade/Incremental/php/FiveNine.php
new file mode 100644
index 000000000000..e1f51b5441d4
--- /dev/null
+++ b/CRM/Upgrade/Incremental/php/FiveNine.php
@@ -0,0 +1,94 @@
+' . ts('A new permission, "%1", has been added. This permission is now used to control access to the Manage Tags screen.', array(1 => ts('manage tags'))) . '
';
+ // }
+ }
+
+ /**
+ * Compute any messages which should be displayed after upgrade.
+ *
+ * @param string $postUpgradeMessage
+ * alterable.
+ * @param string $rev
+ * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
+ */
+ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
+ if ($rev == '5.9.0') {
+ $args = array(
+ 1 => ts('Enable multiple bulk email address for a contact'),
+ 2 => ts('Email on Hold'),
+ );
+ $postUpgradeMessage .= '' . ts('If the setting "%1" is enabled, you should update any smart groups based on the "%2" field.', $args) . '
';
+ }
+ // Example: Generate a post-upgrade message.
+ // if ($rev == '5.12.34') {
+ // $postUpgradeMessage .= '
' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
+ // }
+ }
+
+ /*
+ * Important! All upgrade functions MUST add a 'runSql' task.
+ * Uncomment and use the following template for a new upgrade version
+ * (change the x in the function name):
+ */
+
+ // /**
+ // * Upgrade function.
+ // *
+ // * @param string $rev
+ // */
+ // public function upgrade_5_0_x($rev) {
+ // $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+ // $this->addTask('Do the foo change', 'taskFoo', ...);
+ // // Additional tasks here...
+ // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
+ // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
+ // }
+
+ // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) {
+ // return TRUE;
+ // }
+
+}
diff --git a/CRM/Upgrade/Incremental/sql/5.9.0.mysql.tpl b/CRM/Upgrade/Incremental/sql/5.9.0.mysql.tpl
new file mode 100644
index 000000000000..c290852e8d3e
--- /dev/null
+++ b/CRM/Upgrade/Incremental/sql/5.9.0.mysql.tpl
@@ -0,0 +1 @@
+{* file to handle db changes in 5.9.0 during upgrade *}
diff --git a/templates/CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl b/templates/CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl
index 3e94b37b2d42..3edd73cb3b22 100644
--- a/templates/CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl
+++ b/templates/CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl
@@ -3,7 +3,13 @@
{$form.preferred_communication_method.html}
-{if $form.email_on_hold}
+{if $form.email_on_hold.type == 'select'}
+
+ {$form.email_on_hold.label}
+
+ {$form.email_on_hold.html}
+
+{elseif $form.email_on_hold.type == 'checkbox'}
{$form.email_on_hold.html}
{$form.email_on_hold.label}
diff --git a/tests/phpunit/CRM/Contact/Form/Search/CriteriaTest.php b/tests/phpunit/CRM/Contact/Form/Search/CriteriaTest.php
new file mode 100644
index 000000000000..60f284e31bf9
--- /dev/null
+++ b/tests/phpunit/CRM/Contact/Form/Search/CriteriaTest.php
@@ -0,0 +1,62 @@
+set('civimail_multiple_bulk_emails', 1);
+ $form = new CRM_Contact_Form_Search_Advanced();
+ $form->controller = new CRM_Contact_Controller_Search();
+ $form->preProcess();
+ $form->buildQuickForm();
+ $onHoldElemenClass = (get_class($form->_elements[$form->_elementIndex['email_on_hold']]));
+ $this->assertEquals('HTML_QuickForm_select', $onHoldElemenClass, 'civimail_multiple_bulk_emails setting = 1, so email_on_hold should be a select element.');
+
+ // If setting is disabled, criteria should be a checkbox.
+ Civi::settings()->set('civimail_multiple_bulk_emails', 0);
+ $form = new CRM_Contact_Form_Search_Advanced();
+ $form->controller = new CRM_Contact_Controller_Search();
+ $form->preProcess();
+ $form->buildQuickForm();
+ $onHoldElemenClass = (get_class($form->_elements[$form->_elementIndex['email_on_hold']]));
+ $this->assertEquals('HTML_QuickForm_advcheckbox', $onHoldElemenClass, 'civimail_multiple_bulk_emails setting = 0, so email_on_hold should be a checkbox.');
+ }
+
+}