check if existing contact employer matches profile employer
authorlarssandergreen <lars@wildsight.ca>
Wed, 5 Jul 2023 20:23:14 +0000 (14:23 -0600)
committerlarssandergreen <lars@wildsight.ca>
Wed, 5 Jul 2023 20:23:14 +0000 (14:23 -0600)
CRM/Contact/BAO/Contact/Utils.php
templates/CRM/Contact/Form/Task/Batch.tpl

index c771a8aef8d166b6168ad5104280cf01fc35f8a1..38ea0e360b3ca997c8ee68fe84508f031783c70d 100644 (file)
@@ -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();
index 2cd61d93b1a1f1df9ccae907fac67afd2491ac50..20193493543aaadfb44c927cbab494f1914f2161 100644 (file)
       {if $field.skipDisplay}
         {continue}
       {/if}
-      <td>{copyIcon name=$field.name title=$field.title}{$field.title}</td>
+      <td>
+      {if !$field.is_view}
+        {copyIcon name=$field.name title=$field.title}
+      {/if}
+        {$field.title}
+      </td>
     {/foreach}
     </tr>
     </thead>