From: larssandergreen Date: Wed, 5 Jul 2023 20:23:14 +0000 (-0600) Subject: check if existing contact employer matches profile employer X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cfbeb4813c5c4c3b65b28a33d96099c1c5ed0a50;p=civicrm-core.git check if existing contact employer matches profile employer --- diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index c771a8aef8..38ea0e360b 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -241,27 +241,45 @@ WHERE id IN ( $idString ) * * @param int $contactID * Contact id of the individual. - * @param int|string $employerID + * @param int|string $employerIDorName * (id or name). * @param int|null $previousEmployerID * @param bool $newContact * * @throws \CRM_Core_Exception */ - public static function createCurrentEmployerRelationship($contactID, $employerID, $previousEmployerID = NULL, $newContact = FALSE): void { - if (!$employerID) { + public static function createCurrentEmployerRelationship($contactID, $employerIDorName, $previousEmployerID = NULL, $newContact = FALSE): void { + if (!$employerIDorName) { // This function is not called in core with no organization & should not be // Refs CRM-15368,CRM-15547 CRM_Core_Error::deprecatedWarning('calling this function with no organization is deprecated'); return; } - if (!is_numeric($employerID)) { - $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts(['organization_name' => $employerID], 'Organization', 'Unsupervised', [], FALSE); - $employerID = (int) (reset($dupeIDs) ?: Contact::create(FALSE) - ->setValues([ - 'contact_type' => 'Organization', - 'organization_name' => $employerID, - ])->execute()->first()['id']); + if (is_numeric($employerIDorName)) { + $employerID = $employerIDorName; + } + else { + $employerName = $employerIDorName; + $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts(['organization_name' => $employerName], 'Organization', 'Unsupervised', [], FALSE); + if (!empty($dupeIDs)) { + $employerID = (int) (reset($dupeIDs)); + } + else { + $contact = \Civi\Api4\Contact::get(FALSE) + ->addSelect('employer_id.organization_name', 'employer_id') + ->addWhere('id', '=', $contactID) + ->execute()->first(); + if ($contact && (mb_strtolower($contact['employer_id.organization_name']) === mb_strtolower($employerName))) { + $employerID = $contact['employer_id']; + } + else { + $employerID = Contact::create(FALSE) + ->setValues([ + 'contact_type' => 'Organization', + 'organization_name' => $employerName, + ])->execute()->first()['id']; + } + } } $relationshipTypeID = CRM_Contact_BAO_RelationshipType::getEmployeeRelationshipTypeID(); diff --git a/templates/CRM/Contact/Form/Task/Batch.tpl b/templates/CRM/Contact/Form/Task/Batch.tpl index 2cd61d93b1..2019349354 100644 --- a/templates/CRM/Contact/Form/Task/Batch.tpl +++ b/templates/CRM/Contact/Form/Task/Batch.tpl @@ -19,7 +19,12 @@ {if $field.skipDisplay} {continue} {/if} - {copyIcon name=$field.name title=$field.title}{$field.title} + + {if !$field.is_view} + {copyIcon name=$field.name title=$field.title} + {/if} + {$field.title} + {/foreach}