From d477e9fa5cd9953f124b7bb564c6d863e5ea9c0f Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Fri, 26 Sep 2014 14:11:26 +0530 Subject: [PATCH] CRM-15368 --- CRM/Contact/BAO/Contact/Utils.php | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index 94b2dc79df..e7161fd5d3 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -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); } } -- 2.25.1