From: monishdeb Date: Tue, 8 Jul 2014 20:20:29 +0000 (+0530) Subject: CRM-14758 fixes for 4.4 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f06fc7d2695d870395b8bb4892cb3f038bbdfd65;p=civicrm-core.git CRM-14758 fixes for 4.4 ---------------------------------------- * CRM-14758: API ( contact, create ) does not always create related membership https://issues.civicrm.org/jira/browse/CRM-14758 --- diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 73666cdc6d..07f9ae2c0e 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -229,15 +229,14 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { ) ) { // create current employer - if (isset($params['employer_id'])) { - CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, - $params['employer_id'], $employerId - ); + $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params); + + $newContact = FALSE; + if (empty($params['contact_id'])) { + $newContact = TRUE; } - elseif ($params['current_employer']) { - CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, - $params['current_employer'] - ); + if ($newEmployer) { + CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact); } else { //unset if employer id exits diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index af58ad11d5..1a4139a00c 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -259,7 +259,7 @@ UNION * @access public * @static */ - static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL) { + static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL, $newContact = FALSE) { $organizationId = NULL; // if organization id is passed. @@ -316,8 +316,8 @@ UNION ) = CRM_Contact_BAO_Relationship::create($relationshipParams, $cid); - // In case we change employer, clean prveovious employer related records. - if (!$previousEmployerID) { + // In case we change employer, clean previous employer related records. + if (!$previousEmployerID && !$newContact) { $previousEmployerID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id'); } if ($previousEmployerID && @@ -1057,4 +1057,3 @@ WHERE id IN (" . implode(',', $contactIds) . ")"; } } } -