Skip to content

Commit

Permalink
Merge pull request #10731 from eileenmcnaughton/pref_comm
Browse files Browse the repository at this point in the history
CRM-19933 proposed fix for import wiping out preferred comm method (possibly CRM-20035 too)
  • Loading branch information
monishdeb authored Aug 31, 2017
2 parents be23378 + 37f7ae8 commit da3249e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
11 changes: 3 additions & 8 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions CRM/Contact/Form/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions CRM/Contact/Form/Inline/CommunicationPreferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions api/v3/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down
24 changes: 23 additions & 1 deletion tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public function testGetMultipleContactSubTypes() {

}


/**
* Verify that attempt to create contact with empty params fails.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit da3249e

Please sign in to comment.