You are here

private function WebformCivicrmPostProcess::findDuplicateContact in Webform CiviCRM Integration 8.5

Search for an existing contact using configured deupe rule

Parameters

array $contact:

Return value

int

2 calls to WebformCivicrmPostProcess::findDuplicateContact()
WebformCivicrmPostProcess::createBillingContact in src/WebformCivicrmPostProcess.php
Create contact 1 if not already existing (required by contribution.transact)
WebformCivicrmPostProcess::preSave in src/WebformCivicrmPostProcess.php
Process webform submission when it is about to be saved. Called by the following hook:

File

src/WebformCivicrmPostProcess.php, line 653
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function findDuplicateContact($contact) {

  // This is either a default type (Unsupervised or Supervised) or the id of a specific rule
  $rule = wf_crm_aval($contact, 'matching_rule', 'Unsupervised', TRUE);
  $utils = \Drupal::service('webform_civicrm.utils');
  if ($rule) {
    $contact['contact'][1]['contact_type'] = ucfirst($contact['contact'][1]['contact_type']);
    $params = [
      'check_permission' => FALSE,
      'sequential' => TRUE,
      'rule_type' => is_numeric($rule) ? NULL : $rule,
      'dedupe_rule_id' => is_numeric($rule) ? $rule : NULL,
      'match' => [],
    ];

    // If sharing an address, use the master
    if (!empty($contact['address'][1]['master_id'])) {
      $m = $contact['address'][1]['master_id'];

      // If master address is exposed to the form, use it
      if (!empty($this->data['contact'][$m]['address'][1])) {
        $contact['address'][1] = $this->data['contact'][$m]['address'][1];
      }
      elseif (!empty($this->existing_contacts[$m])) {
        $masters = $utils
          ->wf_crm_apivalues('address', 'get', [
          'contact_id' => $this->ent['contact'][$m]['id'],
          'sort' => 'is_primary DESC',
          'limit' => 1,
        ]);
        if (!empty($masters)) {
          $contact['address'][1] = reset($masters);
        }
      }
    }

    // Translate state abbr to id (skip if using $masters address which would have returned id not abbr from the api)
    if (empty($masters) && !empty($contact['address'][1]['state_province_id'])) {
      $contact['address'][1]['state_province_id'] = $utils
        ->wf_crm_state_abbr($contact['address'][1]['state_province_id'], 'id', $contact['address'][1]['country_id'] ?? NULL);
    }
    foreach ($contact as $table => $fields) {
      if (is_array($fields) && !empty($fields[1])) {
        $params['match'] += $fields[1];
      }
    }

    // Pass custom params to deduper
    if ($params['match']) {
      $dupes = $utils
        ->wf_crm_apivalues('Contact', 'duplicatecheck', $params);
    }
  }
  return $dupes[0]['id'] ?? 0;
}