From 82ffeed50465fbee3a2f9a6122ce470e49b9e5f8 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Thu, 11 Jun 2015 21:18:30 +0530 Subject: [PATCH] CRM-16588 fix - Changes to contacts not saving when "Use another contact's address" is checked https://issues.civicrm.org/jira/browse/CRM-16588 --- CRM/Core/BAO/Address.php | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 67e2d753ca..227bc194ca 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -185,7 +185,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { // call the function to create shared relationships // we only create create relationship if address is shared by Individual - if ($address->master_id != 'null') { + if (!CRM_Utils_System::isNull($address->master_id)) { self::processSharedAddressRelationship($address->master_id, $params); } @@ -1115,9 +1115,6 @@ SELECT is_primary, * @return void */ public static function processSharedAddressRelationship($masterAddressId, $params) { - if (!$masterAddressId) { - return; - } // get the contact type of contact being edited / created $currentContactType = CRM_Contact_BAO_Contact::getContactType($params['contact_id']); $currentContactId = $params['contact_id']; @@ -1129,13 +1126,11 @@ SELECT is_primary, // get the contact id and contact type of shared contact // check the contact type of shared contact, return if it is of type Individual - $query = 'SELECT cc.id, cc.contact_type FROM civicrm_contact cc INNER JOIN civicrm_address ca ON cc.id = ca.contact_id WHERE ca.id = %1'; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($masterAddressId, 'Integer'))); - $dao->fetch(); // if current contact is not of type individual return, since we don't create relationship between @@ -1150,25 +1145,31 @@ SELECT is_primary, if ($sharedContactType == 'Organization') { return CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($currentContactId, $sharedContactId); } - else { - // get the relationship type id of "Household Member of" - $relationshipType = 'Household Member of'; - } - $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationshipType, 'id', 'name_a_b'); + // get the relationship type id of "Household Member of" + $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Household Member of', 'id', 'name_a_b'); if (!$relTypeId) { - CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type '%1'", array(1 => $relationshipType))); + CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Household Member of'")); + } + + $relParam = array( + 'is_active' => TRUE, + 'relationship_type_id' => $relTypeId, + 'contact_id_a' => $currentContactId, + 'contact_id_b' => $sharedContactId, + ); + + // If already there is a relationship record of $relParam criteria, avoid creating relationship again or else + // it will casue CRM-16588 as the Duplicate Relationship Exception will revert other contact field values on update + $relationship = civicrm_api3('relationship', 'get', $relParam); + if (!empty($relationship['values'])) { + return; } try { // create relationship - civicrm_api3('relationship', 'create', array( - 'is_active' => TRUE, - 'relationship_type_id' => $relTypeId, - 'contact_id_a' => $currentContactId, - 'contact_id_b' => $sharedContactId, - )); + civicrm_api3('relationship', 'create', $relParam); } catch (CiviCRM_API3_Exception $e) { // We catch and ignore here because this has historically been a best-effort relationship create call. -- 2.25.1