From: Johan Vervloet Date: Sun, 6 Sep 2015 19:08:22 +0000 (+0200) Subject: CRM-13725 This should cover the case of missing custom fields. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e312bf627757145dfca7ee9fa60c7dda30d5aaf2;p=civicrm-core.git CRM-13725 This should cover the case of missing custom fields. ---------------------------------------- * CRM-13725: Make check for duplicate relationship aware of custom fields https://issues.civicrm.org/jira/browse/CRM-13725 --- diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 8dbf5976d4..fac06c9422 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -939,20 +939,27 @@ WHERE relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer'); * @static */ private static function checkDuplicateCustomFields(&$params, $relationshipId) { + // Get the custom values of the existing relationship. $existingValues = CRM_Core_BAO_CustomValueTable::getEntityValues($relationshipId, 'Relationship'); - if (!array_key_exists('custom', $params)) { - return TRUE; - } - // iterate through $params['custom'], and check if the custom - // values are the same as the one of the existing relationship. - foreach ($params['custom'] as $group) { - foreach ($group as $field) { - if ($existingValues[$field['custom_field_id']] != $field['value']) { - return FALSE; - } + // Create a similar array for the new relationship. + $newValues = array(); + // $params['custom'] seems to be an array. Each value is again an array. + // This array contains one value (key -1), and this value seems to be + // an array with the information about the custom value. + foreach ($params['custom'] as $value) { + foreach ($value as $customValue) { + $newValues[$customValue['custom_field_id']] = $customValue['value']; } } - return TRUE; + + // Calculate difference between arrays. If the only key-value pairs + // that are in one array but not in the other are empty, the + // custom fields are considered to be equal. + // See https://github.com/civicrm/civicrm-core/pull/6515#issuecomment-137985667 + $diff1 = array_diff_assoc($existingValues, $newValues); + $diff2 = array_diff_assoc($newValues, $existingValues); + + return !array_filter($diff1) && !array_filter($diff2); } /**