Skip to content
Snippets Groups Projects
Commit 7d620f5e authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #426 from greezybacon/issue/directory-search


search: Improve typeahead search results

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents 1f665429 e69c0fd1
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,17 @@ class UsersAjaxAPI extends AjaxController {
$limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
$users=array();
$emails=array();
foreach (StaffAuthenticationBackend::searchUsers($_REQUEST['q']) as $u) {
$name = "{$u['first']} {$u['last']}";
$users[] = array('email' => $u['email'], 'name'=>$name,
'info' => "{$u['email']} - $name (remote)",
'id' => "auth:".$u['id'], "/bin/true" => $_REQUEST['q']);
$emails[] = $u['email'];
}
$remote_emails = ($emails)
? ' OR email.address IN ('.implode(',',db_input(array_filter($emails))).') '
: '';
$escaped = db_input(strtolower($_REQUEST['q']), false);
$sql='SELECT DISTINCT user.id, email.address, name '
......@@ -39,26 +50,25 @@ class UsersAjaxAPI extends AjaxController {
LEFT JOIN '.FORM_ANSWER_TABLE.' value ON (value.entry_id=entry.id) '
.' WHERE email.address LIKE \'%'.$escaped.'%\'
OR user.name LIKE \'%'.$escaped.'%\'
OR value.value LIKE \'%'.$escaped.'%\'
ORDER BY user.created '
OR value.value LIKE \'%'.$escaped.'%\''.$remote_emails
.' ORDER BY user.created '
.' LIMIT '.$limit;
if(($res=db_query($sql)) && db_num_rows($res)){
while(list($id,$email,$name)=db_fetch_row($res)) {
foreach ($users as $i=>$u) {
if ($u['email'] == $email) {
unset($users[$i]);
break;
}
}
$name = Format::htmlchars($name);
$users[] = array('email'=>$email, 'name'=>$name, 'info'=>"$email - $name",
"id" => $id, "/bin/true" => $_REQUEST['q']);
}
}
foreach (StaffAuthenticationBackend::searchUsers($_REQUEST['q']) as $u) {
$name = "{$u['first']} {$u['last']}";
$users[] = array('email' => $u['email'], 'name'=>$name,
'info' => "{$u['email']} - $name (remote)",
'id' => "auth:".$u['id'], "/bin/true" => $_REQUEST['q']);
}
return $this->json_encode($users);
return $this->json_encode(array_values($users));
}
......
......@@ -68,9 +68,11 @@ if ($info['error']) {
</div>
<script type="text/javascript">
$(function() {
var last_req;
$('#user-search').typeahead({
source: function (typeahead, query) {
$.ajax({
if (last_req) last_req.abort();
last_req = $.ajax({
url: "ajax.php/users?q="+query,
dataType: 'json',
success: function (data) {
......
......@@ -308,9 +308,11 @@ $(document).ready(function(){
});
/* Typeahead tickets lookup */
var last_req;
$('#basic-ticket-search').typeahead({
source: function (typeahead, query) {
$.ajax({
if (last_req) last_req.abort();
last_req = $.ajax({
url: "ajax.php/tickets/lookup?q="+query,
dataType: 'json',
success: function (data) {
......@@ -329,7 +331,8 @@ $(document).ready(function(){
$('.email.typeahead').typeahead({
source: function (typeahead, query) {
if(query.length > 2) {
$.ajax({
if (last_req) last_req.abort();
last_req = $.ajax({
url: "ajax.php/users?q="+query,
dataType: 'json',
success: function (data) {
......@@ -348,7 +351,8 @@ $(document).ready(function(){
$('.staff-username.typeahead').typeahead({
source: function (typeahead, query) {
if(query.length > 2) {
$.ajax({
if (last_req) last_req.abort();
last_req = $.ajax({
url: "ajax.php/users/staff?q="+query,
dataType: 'json',
success: function (data) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment