From 6b28495231b5c42f42ef4c837b88042c17faba3e Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Sun, 16 Aug 2015 14:05:50 +0200 Subject: [PATCH] CRM-13725 - Check all custom fields to prevent dupe relationships. ---------------------------------------- * CRM-13725: Make check for duplicate relationship aware of custom fields https://issues.civicrm.org/jira/browse/CRM-13725 --- CRM/Contact/BAO/Relationship.php | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 6283a56282..cbd7c23e8d 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -914,9 +914,42 @@ WHERE relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer'); $relationship = new CRM_Contact_BAO_Relationship(); $relationship->query($queryString); - $relationship->fetch(); + while ($relationship->fetch()) { + // Check whether the custom field values are identical. + $result = self::checkDuplicateCustomFields($params, $relationship->id); + if ($result) { + $relationship->free(); + return TRUE; + } + } $relationship->free(); - return ($relationship->id) ? TRUE : FALSE; + return FALSE; + } + + /** + * this function checks whether the values of the custom fields in $params are + * the same as the values of the custom fields of the relation with given + * $relationshipId. + * + * @param array $params (reference) an assoc array of name/value pairs + * @param int $relationshipId ID of an existing duplicate relation + * + * @return boolean true if custom field values are identical + * @access private + * @static + */ + private static function checkDuplicateCustomFields(&$params, $relationshipId) { + $existingValues = CRM_Core_BAO_CustomValueTable::getEntityValues($relationshipId, 'Relationship'); + // 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; + } + } + } + return TRUE; } /** -- 2.25.1