You are here

protected function wf_crm_webform_base::findContact in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_webform_base.inc \wf_crm_webform_base::findContact()

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

Parameters

array $component: Webform component of type 'civicrm_contact'

2 calls to wf_crm_webform_base::findContact()
wf_crm_webform_ajax::contactAjax in includes/wf_crm_webform_ajax.inc
Load one or more contacts via ajax
wf_crm_webform_preprocess::alterForm in includes/wf_crm_webform_preprocess.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

includes/wf_crm_webform_base.inc, line 238

Class

wf_crm_webform_base
Class wf_crm_webform_base

Code

protected function findContact($component) {
  module_load_include('inc', 'webform_civicrm', 'includes/contact_component');
  list(, $c, ) = explode('_', $component['form_key'], 3);
  $filters = wf_crm_search_filters($this->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 (wf_crm_is_positive($cid) || $cid === '0' || $cid === 0) {
      $cid = (int) $cid;
      if ($cid === 0) {
        $this->ent['contact'][$c]['id'] = $cid;
        return;
      }
      elseif (wf_crm_aval($component['extra'], 'allow_url_autofill', TRUE, TRUE)) {
        if (wf_crm_contact_access($component, $filters, $cid) !== FALSE) {
          $this->ent['contact'][$c]['id'] = $cid;
        }
      }
    }
  }
  if (empty($this->ent['contact'][$c]['id'])) {
    $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':
        $to = $component['extra']['default_relationship_to'];
        if (!empty($this->ent['contact'][$to]['id']) && !empty($component['extra']['default_relationship'])) {
          $found = wf_crm_find_relations($this->ent['contact'][$to]['id'], $component['extra']['default_relationship']);
        }
        break;
      case 'custom_ref':
        $to = $component['extra']['default_relationship_to'];
        if (!empty($this->ent['contact'][$to]['id'])) {
          $found = wf_crm_find_custom_refs($this->ent['contact'][$to]['id'], $component['extra']['default_custom_ref']);
        }
        break;
      case 'case_roles':
        $to = $component['extra']['default_relationship_to'];
        $case = $component['extra']['default_for_case'];
        if (empty($this->ent['contact'][$c]['id'])) {

          // load case client of case (if passed in URL) as default contact
          $this
            ->findCaseClientForDefaultContact($c);
        }
        if (!empty($this->ent['contact'][$to]['id']) && !empty($this->ent['case'][$case]['id'])) {
          $found = wf_crm_find_case_roles($this->ent['contact'][$to]['id'], $this->ent['case'][$case]['id'], $component['extra']['default_case_roles']);
          unset($this->ent['contact'][$c]['reload']);
        }
        else {
          $this->ent['contact'][$c]['reload'] = TRUE;
        }
        break;
      case 'auto':
        $component['extra']['allow_create'] = FALSE;
        $found = array_keys(wf_crm_contact_search($this->node, $component, $filters, wf_crm_aval($this->ent, 'contact', array())));
        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 (!$dupes_allowed) {
        foreach ($this->ent['contact'] as $contact) {
          if (!empty($contact['id']) && $cid == $contact['id']) {
            continue 2;
          }
        }
      }

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