You are here

function wf_crm_find_contact in Webform CiviCRM Integration 7.3

Find an existing contact based on matching criteria Used to autopopulate a webform existing contact field

Parameters

$node: Webform node object

$component: Webform component of type 'civicrm_contact'

$ids: Reference all known contact ids for this form

1 call to wf_crm_find_contact()
_wf_crm_frontend_form_alter in ./webform_civicrm_forms.inc
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

./contact_component.inc, line 809

Code

function wf_crm_find_contact($node, $component, &$ids) {
  list(, $c, ) = explode('_', $component['form_key'], 3);
  $filters = wf_crm_search_filters($node, $component);

  // Start with the url - that trumps everything.
  if (isset($_GET["cid{$c}"]) || $c == 1 && isset($_GET['cid'])) {
    $cid = isset($_GET["cid{$c}"]) ? $_GET["cid{$c}"] : $_GET['cid'];
    if (is_numeric($cid)) {
      $cid = (int) $cid;
      if ($cid === 0) {
        $ids[$c] = $cid;
      }
      else {
        if (wf_crm_contact_access($component, $filters, $cid) !== FALSE) {
          $ids[$c] = $cid;
        }
      }
    }
  }
  if (!isset($ids[$c])) {
    $found = array();
    switch ($component['extra']['default']) {
      case 'user':
        $cid = wf_crm_user_cid();
        $found = $c == 1 && $cid ? array(
          $cid,
        ) : array();
        break;
      case 'contact_id':
        if ($component['extra']['default_contact_id']) {
          $found = array(
            $component['extra']['default_contact_id'],
          );
        }
        break;
      case 'relationship':
        if (!empty($ids[1])) {
          $found = wf_crm_find_relations($ids[1], $component['extra']['default_relationship']);
        }
        break;
      case 'auto':
        $component['extra']['allow_create'] = FALSE;
        $found = array_keys(wf_crm_contact_search($node, $component, $filters));
        break;
    }
    if ($component['extra']['randomize']) {
      shuffle($found);
    }
    if (in_array($component['extra']['default'], array(
      'user',
      'contact_id',
    ))) {
      $dupes_allowed = TRUE;
    }
    else {
      $dupes_allowed = $component['extra']['dupes_allowed'];
    }
    foreach ($found as $cid) {

      // Don't pick the same contact twice unless explicitly told to do so
      if (!in_array($cid, $ids) || $dupes_allowed) {

        // Check filters except for 'auto' which already applied them
        if ($component['extra']['default'] == 'auto' || wf_crm_contact_access($component, $filters, $cid) !== FALSE) {
          $ids[$c] = $cid;
          break;
        }
      }
    }
  }

  // Identify contact 1 as acting user if not already logged in
  if (!empty($ids[1]) && $c == 1 && user_is_anonymous()) {
    CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', array(
      1 => array(
        $ids[1],
        'Integer',
      ),
    ));
  }
}