You are here

function support_autocomplete_autosubscribe in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \support_autocomplete_autosubscribe()

Autocomplete usernames to assign to ticket.

1 string reference to 'support_autocomplete_autosubscribe'
support_menu in ./support.module
Implementation of hook_menu().

File

./support.module, line 444
support.module

Code

function support_autocomplete_autosubscribe($clid, $string = '') {

  // The user enters a comma-separated list of users.  We only autocomplete the
  // last user.
  $array = drupal_explode_tags($string);

  // Fetch last user.
  $last_string = trim(array_pop($array));
  $matches = array();
  if ($last_string) {
    $roles = array();
    $client = db_result(db_query('SELECT name FROM {support_client} WHERE clid = %d', $clid));

    // retrieve all roles giving permission to access current tickets
    $result = db_query("SELECT rid FROM {permission} WHERE perm LIKE '%%%s%%' OR perm LIKE '%%%s%%'", "access {$client} tickets", 'administer support');
    while ($role = db_fetch_object($result)) {
      $roles[$role->rid] = $role->rid;
    }
    if (!$clid) {
      $result = db_query_range("SELECT name FROM {users} WHERE status = 1 AND LOWER(name) LIKE LOWER('%s%%')", $last_string, 0, 10);
    }
    else {
      if (!empty($roles)) {
        $result = db_query_range("SELECT u.name FROM {users} u LEFT JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s) AND u.status = 1 AND LOWER(u.name) LIKE LOWER('%s%%')", implode(', ', $roles), $last_string, 0, 10);
      }
    }
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
    while ($account = db_fetch_object($result)) {
      $a = $account->name;
      $matches[$prefix . $a] = check_plain($account->name);
    }
  }
  drupal_json($matches);
}