You are here

function support_autocomplete_assigned in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \support_autocomplete_assigned()

Autocomplete usernames to assign to ticket.

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

File

./support.module, line 434
support.module

Code

function support_autocomplete_assigned($clid = 0, $string = '') {
  $matches = array();
  if ($string) {
    $result = array();
    if ($clid) {
      $client = db_query('SELECT name FROM {support_client} WHERE clid = :clid', array(
        ':clid' => $clid,
      ))
        ->fetchField();

      // retrieve all roles giving permission to access current tickets
      $result = db_query('SELECT rid FROM {role_permission} WHERE permission = :perm', array(
        ':perm' => "access {$client} tickets",
      ));
    }
    $roles = array();
    foreach ($result as $role) {
      $roles[$role->rid] = $role->rid;
    }

    // also get people with administer support permissions
    $result = db_query('SELECT rid FROM {role_permission} WHERE permission = :perm', array(
      ':perm' => 'administer support',
    ));
    foreach ($result as $role) {
      $roles[$role->rid] = $role->rid;
    }
    if (!empty($roles)) {
      $query = db_select('users', 'u');
      $query
        ->join('users_roles', 'r', 'u.uid = r.uid');
      $query
        ->fields('u', array(
        'name',
      ))
        ->condition('r.rid', $roles, 'IN')
        ->condition('u.status', 1)
        ->condition('u.name', db_like($string) . '%', 'LIKE')
        ->range(0, 10);
      if (variable_get('support_filter_uid1', FALSE)) {
        $query
          ->condition('u.uid', 1, '<>');
      }
      $result = $query
        ->execute();
      foreach ($result as $user) {
        $matches[$user->name] = check_plain($user->name);
      }
    }
  }
  drupal_json_output($matches);
}