From b2b00a4073feb837a4e78ab3a7198a3210204603 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 1 Jun 2015 15:29:07 -0500
Subject: [PATCH] search: Fix search for tix #, add search keywords

---
 include/staff/tickets.inc.php | 16 +++++++++++++---
 scp/js/scp.js                 |  2 +-
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index 9fe9fe5bc..d3a570237 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -77,18 +77,28 @@ case 'search':
     if ($_REQUEST['query']) {
         $results_type=__('Search Results');
         // Use an index if possible
-        if ($_REQUEST['search-type'] == 'email') {
+        if ($_REQUEST['search-type'] == 'typeahead' && Validator::is_email($_REQUEST['query'])) {
             $tickets = $tickets->filter(array(
                 'user__emails__address' => $_REQUEST['query'],
             ));
         }
         else {
-            $tickets = $tickets->filter(Q::any(array(
+            $basic_search = Q::any(array(
                 'number__startswith' => $_REQUEST['query'],
                 'user__name__contains' => $_REQUEST['query'],
                 'user__emails__address__contains' => $_REQUEST['query'],
                 'user__org__name__contains' => $_REQUEST['query'],
-            )));
+            ));
+            if (!$_REQUEST['search-type']) {
+                // [Search] click, consider keywords too. This is a
+                // relatively ugly hack. SearchBackend::find() add in a
+                // constraint for the search. We need to pop that off and
+                // include it as an OR with the above constraints
+                $tickets = $ost->searcher->find($_REQUEST['query'], $tickets);
+                $keywords = array_pop($tickets->constraints);
+                $basic_search->add($keywords);
+            }
+            $tickets->filter($basic_search);
         }
         break;
     } elseif (isset($_SESSION['advsearch'])) {
diff --git a/scp/js/scp.js b/scp/js/scp.js
index fad761841..dfa6ed4e0 100644
--- a/scp/js/scp.js
+++ b/scp/js/scp.js
@@ -261,7 +261,7 @@ var scp_prep = function() {
         },
         onselect: function (obj) {
             var form = $('#basic-ticket-search').closest('form');
-            form.find('input[name=search-type]').val('email');
+            form.find('input[name=search-type]').val('typeahead');
             $('#basic-ticket-search').val(obj.value);
             form.submit();
         },
-- 
GitLab