From fe18a93c163c38c233b5d9660555f72efbf3a1e9 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 13 Aug 2013 20:59:10 -0700 Subject: [PATCH] Fix api preferred_communication_method CRM-12548 ---------------------------------------- * CRM-12548: API not saving preferred_communication_method http://issues.civicrm.org/jira/browse/CRM-12548 --- CRM/Utils/Array.php | 24 ++++++++++++++++-------- api/v3/Contact.php | 5 +++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index b685b9482f..849d52e71f 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -570,29 +570,37 @@ class CRM_Utils_Array { /** * Like explode() but assumes that the $value is padded with $delim on left and right * - * @param string|NULL $value + * @param mixed $values * @param string $delim * @return array|NULL */ - static function explodePadded($value, $delim = CRM_Core_DAO::VALUE_SEPARATOR) { - if ($value === NULL) { + static function explodePadded($values, $delim = CRM_Core_DAO::VALUE_SEPARATOR) { + if ($values === NULL) { return NULL; } - return explode($delim, trim($value, $delim)); + // If we already have an array, no need to continue + if (is_array($values)) { + return $values; + } + return explode($delim, trim((string) $values, $delim)); } /** - * Like implode() but assumes that the $value is padded with $delim on left and right + * Like implode() but creates a string that is padded with $delim on left and right * - * @param string|NULL $value + * @param mixed $values * @param string $delim - * @return array|NULL + * @return string|NULL */ static function implodePadded($values, $delim = CRM_Core_DAO::VALUE_SEPARATOR) { if ($values === NULL) { return NULL; } - return $delim . implode($delim, $values) . $delim; + // If we already have a string, strip $delim off the ends so it doesn't get added twice + if (is_string($values)) { + $values = trim($values, $delim); + } + return $delim . implode($delim, (array) $values) . $delim; } /** diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 39b6926ebb..e138091a78 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -297,6 +297,11 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE 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 (CRM_Utils_Array::value('contact_sub_type', $params) && CRM_Utils_Array::value('contact_type', $params)) { 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'])); -- 2.25.1