From 2d261af2e256f66a9c9f2ba560bc04a5101e2f4d Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Sat, 31 Mar 2012 23:02:22 -0400
Subject: [PATCH] Add ticket count to search by email

---
 include/ajax.tickets.php | 50 ++++++++++++++++++++++------------------
 scp/js/scp.js            |  1 -
 2 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index ab44bd3e3..f9151951c 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -22,37 +22,43 @@ class TicketsAjaxAPI extends AjaxController {
    
     function search() {
 
+        if(!is_numeric($_REQUEST['q']))
+            return self::searchByEmail();
+
+
         $limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
-        $items=array();
+        $tickets=array();
 
         $sql='SELECT DISTINCT ticketID, email'
-            .' FROM '.TICKET_TABLE;
-
-        $emailSearch=false;
-        if(is_numeric($_REQUEST['q']))
-            $sql.=' WHERE ticketID LIKE \''.db_input($_REQUEST['q'], false).'%\'';
-        else {
-            $emailSearch=true;
-            $sql.=' WHERE email LIKE \'%'.db_input(strtolower($_REQUEST['q']), false).'%\' ';
+            .' FROM '.TICKET_TABLE
+            .' WHERE ticketID LIKE \''.db_input($_REQUEST['q'], false).'%\''
+            .' ORDER BY created  LIMIT '.$limit;
+
+        if(($res=db_query($sql)) && db_num_rows($res)) {
+            while(list($id, $email)=db_fetch_row($res))
+                $tickets[] = array('id'=>$id, 'email'=>$email, 'value'=>$id, 'info'=>"$id - $email");
         }
 
-        $sql.=' ORDER BY created  LIMIT '.$limit;
+        return $this->json_encode($tickets);
+    }
+
+    function searchByEmail() {
+
+        $limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
+        $tickets=array();
+
+        $sql='SELECT email, count(ticket_id) as tickets '
+            .' FROM '.TICKET_TABLE
+            .' WHERE email LIKE \'%'.db_input(strtolower($_REQUEST['q']), false).'%\' '
+            .' GROUP BY email '
+            .' ORDER BY created  LIMIT '.$limit;
 
         if(($res=db_query($sql)) && db_num_rows($res)) {
-            while(list($id,$email,$name)=db_fetch_row($res)) {
-                if($emailSearch) {
-                    $info = "$email - $id";
-                    $value = $email;
-                } else {
-                    $info = "$id -$email";
-                    $value = $id;
-                }
-
-                $items[] = array('id'=>$id, 'email'=>$email, 'value'=>$value, 'info'=>$info);
-            }
+            while(list($email, $count)=db_fetch_row($res))
+                $tickets[] = array('email'=>$email, 'value'=>$email, 'info'=>"$email ($count)");
         }
 
-        return $this->json_encode($items);
+        return $this->json_encode($tickets);
     }
 
     function acquireLock($tid) {
diff --git a/scp/js/scp.js b/scp/js/scp.js
index 08da6525e..7a9e0e218 100644
--- a/scp/js/scp.js
+++ b/scp/js/scp.js
@@ -210,7 +210,6 @@ $(document).ready(function(){
             });
         },
         onselect: function (obj) {
-            $('#basic-ticket-search').val(obj.id); /*overwriting email*/
             $('#basic-ticket-search').closest('form').submit();
         },
         property: "value"
-- 
GitLab