You are here

function wf_crm_contact_search in Webform CiviCRM Integration 7.3

Same name and namespace in other branches
  1. 7.5 includes/contact_component.inc \wf_crm_contact_search()
  2. 7.4 includes/contact_component.inc \wf_crm_contact_search()

Returns a list of contacts based on component settings.

Parameters

$node: Node object

$component: Webform component

$params: Contact get params (filters)

$str: Search string (used during autocomplete)

Return value

array

3 calls to wf_crm_contact_search()
wf_crm_ajax in ./contact_component.inc
Drupal page callback to serve AJAX requests.
wf_crm_find_contact in ./contact_component.inc
Find an existing contact based on matching criteria Used to autopopulate a webform existing contact field
_webform_render_civicrm_contact in ./contact_component.inc
Implements _webform_render_component().

File

./contact_component.inc, line 499

Code

function wf_crm_contact_search($node, $component, $params, $str = NULL) {
  if (empty($node->webform_civicrm)) {
    return array();
  }
  $limit = $str ? 12 : 200;
  $ret = array();
  $display = $component['extra']['results_display'];
  $params += array(
    'rowCount' => $limit,
    'sort' => 'sort_name',
  );
  if ($str) {
    require_once 'CRM/Utils/Type.php';
    $params['display_name'] = str_replace(' ', '%', CRM_Utils_Type::escape($str, 'String'));
  }
  $result = wf_civicrm_api('contact', 'get', $params);

  // Autocomplete results
  if ($str) {
    foreach (wf_crm_aval($result, 'values', array()) as $contact) {
      if ($name = wf_crm_format_contact($contact, $display)) {
        $ret[] = array(
          'id' => $contact['id'],
          'name' => $name,
        );
      }
    }
    if (count($ret) < $limit && $component['extra']['allow_create']) {

      // HTML hack to get prompt to show up different than search results
      $ret[] = array(
        'id' => "-{$str}",
        'name' => '<em><i>' . $component['extra']['none_prompt'] . '</i></em>',
      );
    }
  }
  else {
    if ($component['extra']['allow_create']) {
      $ret['-'] = $component['extra']['none_prompt'];
    }
    foreach (wf_crm_aval($result, 'values', array()) as $contact) {
      if ($name = wf_crm_format_contact($contact, $display)) {
        $ret[$contact['id']] = $name;
      }
    }
  }
  return $ret;
}