You are here

function support_autocomplete_autosubscribe in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 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 477
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_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 OR permission = :admin', array(
      ':perm' => "access {$client} tickets",
      ':admin' => 'administer support',
    ));
    foreach ($result as $role) {
      $roles[$role->rid] = $role->rid;
    }
    $result = array();
    if (!$clid) {
      $result = db_select('users', 'u')
        ->fields('u', array(
        'name',
      ))
        ->condition('u.status', 1)
        ->condition('u.name', db_like($last_string) . '%', 'LIKE')
        ->range(0, 10)
        ->execute();
    }
    else {
      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($last_string) . '%', 'LIKE')
          ->range(0, 10);
        $result = $query
          ->execute();
      }
    }
    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
    foreach ($result as $account) {
      $a = $account->name;
      $matches[$prefix . $a] = check_plain($account->name);
    }
  }
  drupal_json_output($matches);
}