CRM-14758 fixes for 4.4
authormonishdeb <monish.deb@webaccessglobal.com>
Tue, 8 Jul 2014 20:20:29 +0000 (01:50 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Tue, 8 Jul 2014 20:20:29 +0000 (01:50 +0530)
----------------------------------------
* CRM-14758: API ( contact, create ) does not always create related membership
  https://issues.civicrm.org/jira/browse/CRM-14758

CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Contact/Utils.php

index 73666cdc6d323291c763f954404a6ad979802b05..07f9ae2c0e932d147d45fb34dde014110985b9db 100644 (file)
@@ -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
index af58ad11d5fca8c0332ecbe9b2ca056048bf8b01..1a4139a00c0e12ef437e17202acad9db0b13ed4d 100644 (file)
@@ -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) . ")";
     }
   }
 }
-