You are here

function _drupalchat_ur_autocomplete in DrupalChat 7

Same name and namespace in other branches
  1. 6.2 drupalchat.module \_drupalchat_ur_autocomplete()
  2. 7.2 drupalchat.module \_drupalchat_ur_autocomplete()

Implements autocomplete feature for UR Integration.

1 string reference to '_drupalchat_ur_autocomplete'
drupalchat_menu in ./drupalchat.module
Implements hook_menu().

File

./drupalchat.module, line 371
Module code for DrupalChat.

Code

function _drupalchat_ur_autocomplete($string) {
  $array = drupal_explode_tags($string);

  // Fetch last value
  $last_string = drupal_strtolower(array_pop($array));
  $matches = array();
  $query = db_select('user_relationship_types', 'u');

  // Select rows that match the string
  $return = $query
    ->fields('u', array(
    'name',
  ))
    ->condition('u.name', '%' . db_like($last_string) . '%', 'LIKE')
    ->range(0, 10)
    ->execute();
  $prefix = count($array) ? drupal_implode_tags($array) . ', ' : '';

  // add matches to $matches
  foreach ($return as $row) {
    if (!in_array($row->name, $array)) {
      $matches[$prefix . $row->name] = check_plain($row->name);
    }
  }

  // return for JS
  drupal_json_output($matches);
}