CRM-14758 fix - API ( contact, create ) does not always create related membership
authormonishdeb <monish.deb@webaccessglobal.com>
Thu, 3 Jul 2014 12:55:51 +0000 (18:25 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 3 Jul 2014 12:55:51 +0000 (18:25 +0530)
https://issues.civicrm.org/jira/browse/CRM-14758

CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Contact/Utils.php
CRM/Contact/Form/Edit/Individual.php

index 0c04192c6019e77ce1857c63ca42c339ce1cf067..54fe7206ce99869dc7f2b2261f643a2c4d4b1e9b 100644 (file)
@@ -237,7 +237,11 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact {
       $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params);
       // create current employer
       if ($newEmployer) {
-        CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId);
+        $newContact = FALSE;
+        if (empty($params['contact_id'])) {
+          $newContact = TRUE;
+        }
+        CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact);
       }
       else {
         //unset if employer id exits
index e59da7a1bc38ef24efc7515e0841434d338f262e..3b1001edb5466df04a12c03ee9ee44442450026b 100644 (file)
@@ -284,7 +284,7 @@ UNION
    * @access public
    * @static
    */
-  static function createCurrentEmployerRelationship($contactID, $organizationId, $previousEmployerID = NULL) {
+  static function createCurrentEmployerRelationship($contactID, $organizationId, $previousEmployerID = NULL, $newContact = FALSE) {
     if ($organizationId && is_numeric($organizationId)) {
       $cid = array('contact' => $contactID);
 
@@ -305,8 +305,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 &&
index 8df832970ca9c33cab91e0d9ab13eb3a7824f4a6..190e1c2bec17ae22e9cc56de81255f2de6a7a0a3 100644 (file)
@@ -170,4 +170,3 @@ class CRM_Contact_Form_Edit_Individual {
     return empty($errors) ? TRUE : $errors;
   }
 }
-