You are here

function crm_core_user_sync_user_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_user_autocomplete()

User autocomplete for user->contact relation form.

1 string reference to 'crm_core_user_sync_user_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 466

Code

function crm_core_user_sync_user_autocomplete($search_string) {
  $matches = array();
  $query = db_select('users', 'u');
  $on_cond = "endpoints.entity_type = 'user'";
  $on_cond .= ' AND ';
  $on_cond .= 'u.uid = endpoints.entity_id';
  $on_cond .= ' AND ';
  $on_cond .= "endpoints.bundle = 'crm_core_user_sync'";
  $query
    ->leftJoin('field_data_endpoints', 'endpoints', $on_cond);
  $query
    ->addField('u', 'uid');
  $query
    ->isNull('endpoints_entity_id');
  $query
    ->condition('name', '%' . db_like($search_string) . '%', 'LIKE');
  $uids = $query
    ->range(0, 20)
    ->execute()
    ->fetchCol();
  if (!empty($uids)) {
    $users = entity_load('user', $uids);
    foreach ($users as $user) {
      $matches[$user->name . " [uid:{$user->uid}]"] = $user->name;
    }
  }
  drupal_json_output($matches);
}