You are here

function crm_core_user_sync_contact_autocomplete in CRM Core 7

Same name and namespace in other branches
  1. 8.2 modules/crm_core_user_sync/crm_core_user_sync.admin.inc \crm_core_user_sync_contact_autocomplete()

User autocomplete for user->contact relation form.

1 string reference to 'crm_core_user_sync_contact_autocomplete'
crm_core_user_sync_menu in modules/crm_core_user_sync/crm_core_user_sync.module
Implements hook_menu()

File

modules/crm_core_user_sync/crm_core_user_sync.admin.inc, line 494

Code

function crm_core_user_sync_contact_autocomplete($search_string) {
  $matches = array();
  $query = db_select('crm_core_contact', 'c');
  $query
    ->range(0, 20);
  $on_cond = "endpoints.entity_type = 'crm_core_contact'";
  $on_cond .= ' AND ';
  $on_cond .= 'endpoints.entity_id = c.contact_id';
  $on_cond .= ' AND ';
  $on_cond .= "endpoints.bundle = 'crm_core_user_sync'";
  $query
    ->leftJoin('field_data_endpoints', 'endpoints', $on_cond);
  $query
    ->isNull('entity_id');
  $query
    ->condition('c.name', '%' . db_like($search_string) . '%', 'LIKE');
  $query
    ->addField('c', 'contact_id');
  $query
    ->addField('c', 'name');
  $contacts = $query
    ->execute()
    ->fetchAllKeyed();
  if (!empty($contacts)) {
    foreach ($contacts as $contact_id => $contact_name) {
      $name = check_plain($contact_name);
      $matches[$name . " [contact_id:{$contact_id}]"] = $name;
    }
  }
  drupal_json_output($matches);
}