CRM-15368
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 26 Sep 2014 08:41:26 +0000 (14:11 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 26 Sep 2014 08:41:26 +0000 (14:11 +0530)
CRM/Contact/BAO/Contact/Utils.php

index 94b2dc79df7236775f609f2a9e956abb15220f01..e7161fd5d38869635990b55d3cd3769b05985690 100644 (file)
@@ -276,7 +276,7 @@ UNION
    * Create Current employer relationship for a individual
    *
    * @param int $contactID contact id of the individual
-   * @param $organizationId
+   * @param $organization (id or name)
    * @param null $previousEmployerID
    *
    * @internal param string $organization it can be name or id of organization
@@ -284,8 +284,34 @@ UNION
    * @access public
    * @static
    */
-  static function createCurrentEmployerRelationship($contactID, $organizationId, $previousEmployerID = NULL, $newContact = FALSE) {
-    if ($organizationId && is_numeric($organizationId)) {
+  static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL, $newContact = FALSE) {
+    //if organization name is passed. CRM-15368
+    if ($newContact && is_string($organization)) {
+      $organizationParams['organization_name'] = $organization;
+      $dedupeParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
+
+      $dedupeParams['check_permission'] = FALSE;
+      $dupeIDs = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Organization', 'Supervised');
+
+      if (is_array($dupeIDs) && !empty($dupeIDs)) {
+        // we should create relationship only w/ first org CRM-4193
+        foreach ($dupeIDs as $orgId) {
+          $organization = $orgId;
+          break;
+        }
+      }
+      else {
+        //create new organization
+        $newOrg = array(
+          'contact_type' => 'Organization',
+          'organization_name' => $organization,
+        );
+        $org = CRM_Contact_BAO_Contact::create($newOrg);
+        $organization = $org->id;
+      }
+    }
+
+    if ($organization && is_numeric($organization)) {
       $cid = array('contact' => $contactID);
 
       // get the relationship type id of "Employee of"
@@ -298,7 +324,7 @@ UNION
       $relationshipParams = array(
         'is_active' => TRUE,
         'relationship_type_id' => $relTypeId . '_a_b',
-        'contact_check' => array($organizationId => TRUE),
+        'contact_check' => array($organization => TRUE),
       );
       list($valid, $invalid, $duplicate,
         $saved, $relationshipIds
@@ -310,17 +336,17 @@ UNION
         $previousEmployerID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id');
       }
       if ($previousEmployerID &&
-        $previousEmployerID != $organizationId
+        $previousEmployerID != $organization
       ) {
         self::clearCurrentEmployer($contactID, $previousEmployerID);
       }
 
       // set current employer
-      self::setCurrentEmployer(array($contactID => $organizationId));
+      self::setCurrentEmployer(array($contactID => $organization));
 
       $relationshipParams['relationship_ids'] = $relationshipIds;
       // handle related meberships. CRM-3792
-      self::currentEmployerRelatedMembership($contactID, $organizationId, $relationshipParams, $duplicate, $previousEmployerID);
+      self::currentEmployerRelatedMembership($contactID, $organization, $relationshipParams, $duplicate, $previousEmployerID);
     }
   }