From 37f7ae8870dfdef58d9a2c88d0d5f7c0c5e3c378 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 24 Jul 2017 13:51:45 +1200 Subject: [PATCH] CRM-19933 proposed fix for import wiping out preferred comm method --- CRM/Contact/BAO/Contact.php | 11 +++------ CRM/Contact/Form/Contact.php | 4 ++++ .../Form/Inline/CommunicationPreferences.php | 3 +++ api/v3/Contact.php | 5 ---- tests/phpunit/api/v3/ContactTest.php | 24 ++++++++++++++++++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b7bae7d7f709..df8eaf5929a3 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -151,17 +151,12 @@ public static function add(&$params) { $params['source'] = $params['contact_source']; } - // Fix for preferred communication method. - $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params, ''); - if ($prefComm && is_array($prefComm)) { + if (isset($params['preferred_communication_method']) && is_array($params['preferred_communication_method'])) { + CRM_Utils_Array::formatArrayKeys($params['preferred_communication_method']); + $contact->preferred_communication_method = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); unset($params['preferred_communication_method']); - - CRM_Utils_Array::formatArrayKeys($prefComm); - $prefComm = CRM_Utils_Array::implodePadded($prefComm); } - $contact->preferred_communication_method = $prefComm; - $allNull = $contact->copyValues($params); $contact->id = CRM_Utils_Array::value('contact_id', $params); diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 7102c404d9c3..2a14de16e94e 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -853,6 +853,10 @@ public function postProcess() { //get the submitted values in an array $params = $this->controller->exportValues($this->_name); + if (!isset($params['preferred_communication_method'])) { + // If this field is empty QF will trim it so we have to add it in. + $params['preferred_communication_method'] = 'null'; + } $group = CRM_Utils_Array::value('group', $params); if (!empty($group) && is_array($group)) { diff --git a/CRM/Contact/Form/Inline/CommunicationPreferences.php b/CRM/Contact/Form/Inline/CommunicationPreferences.php index dc9d321ea0dc..ce67560a2b66 100644 --- a/CRM/Contact/Form/Inline/CommunicationPreferences.php +++ b/CRM/Contact/Form/Inline/CommunicationPreferences.php @@ -98,6 +98,9 @@ public function postProcess() { $params['contact_sub_type'] = $this->_contactSubType; } + if (!isset($params['preferred_communication_method'])) { + $params['preferred_communication_method'] = 'null'; + } CRM_Contact_BAO_Contact::create($params); $this->response(); diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 2d1da21a38d1..4e2f95db237e 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -524,11 +524,6 @@ function _civicrm_api3_contact_check_params(&$params) { break; } - // Fixme: This really needs to be handled at a lower level. @See CRM-13123 - if (isset($params['preferred_communication_method'])) { - $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); - } - if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) { if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { throw new API_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $params['contact_sub_type'])); diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 961d0e0a5bb8..3daf6bd58c18 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -237,7 +237,6 @@ public function testGetMultipleContactSubTypes() { } - /** * Verify that attempt to create contact with empty params fails. */ @@ -1715,6 +1714,29 @@ public function testContactGetEmail() { $this->callAPISuccess('contact', 'delete', $contact); } + /** + * Ensure consistent return format for option group fields. + */ + public function testSetPreferredCommunicationNull() { + $contact = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array( + 'preferred_communication_method' => array('Phone', 'SMS'), + ))); + $preferredCommunicationMethod = $this->callAPISuccessGetValue('Contact', array( + 'id' => $contact['id'], + 'return' => 'preferred_communication_method', + )); + $this->assertNotEmpty($preferredCommunicationMethod); + $contact = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array( + 'preferred_communication_method' => 'null', + 'id' => $contact['id'], + ))); + $preferredCommunicationMethod = $this->callAPISuccessGetValue('Contact', array( + 'id' => $contact['id'], + 'return' => 'preferred_communication_method', + )); + $this->assertEmpty($preferredCommunicationMethod); + } + /** * Ensure consistent return format for option group fields. */