From b8652e259dff647e79c6c701065fcb0e983d9974 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 1 Dec 2014 13:02:13 -0600
Subject: [PATCH] search: Properly pagenate search results

Also re-implement the "exclude answered tickets" and "exclude assigned
tickets" from open queue options.
---
 include/class.orm.php         | 9 +++++++--
 include/class.pagenate.php    | 4 ++++
 include/class.staff.php       | 2 +-
 include/staff/tickets.inc.php | 6 +++++-
 4 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index 627338c4c..31084569e 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -1637,7 +1637,7 @@ class MySqlCompiler extends SqlCompiler {
      * (string) token to be placed into the compiled SQL statement. For
      * MySQL, this is always the string '?'.
      */
-    function input(&$what, $slot=false) {
+    function input($what, $slot=false) {
         if ($what instanceof QuerySet) {
             $q = $what->getQuery(array('nosort'=>true));
             $this->params = array_merge($q->params);
@@ -2066,7 +2066,12 @@ class MysqlExecutor {
     }
 
     function __toString() {
-        return $this->sql;
+        $self = $this;
+        $x = 0;
+        return preg_replace_callback('/\?/', function($m) use ($self, &$x) {
+            $p = $self->params[$x++];
+            return db_real_escape($p, is_string($p));
+        }, $this->sql);
     }
 }
 
diff --git a/include/class.pagenate.php b/include/class.pagenate.php
index 361d21f89..acec4fb6c 100644
--- a/include/class.pagenate.php
+++ b/include/class.pagenate.php
@@ -127,5 +127,9 @@ class PageNate {
         return $html;
     }
 
+    function paginate(QuerySet $qs) {
+        return $qs->limit($this->getLimit())->offset($this->getStart());
+    }
+
 }
 ?>
diff --git a/include/class.staff.php b/include/class.staff.php
index e5b2f7d22..c2c344544 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -366,7 +366,7 @@ implements AuthenticatedUser {
     }
 
     function showAssignedTickets() {
-        return $this->group->show_assigned_tickets;
+        return $this->show_assigned_tickets;
     }
 
     function getTeams() {
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index a1154dbcb..478d99c9c 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -42,7 +42,10 @@ default:
 case 'open':
     $status='open';
     $results_type=__('Open Tickets');
-    $tickets->filter(array('isanswered'=>0));
+    if (!$cfg->showAnsweredTickets())
+        $tickets->filter(array('isanswered'=>0));
+    if (!$cfg || !($cfg->showAssignedTickets() || $thisstaff->showAssignedTickets()))
+        $tickets->filter(array('staff_id'=>0));
     break;
 }
 
@@ -78,6 +81,7 @@ $tickets->select_related('lock', 'dept', 'staff', 'user', 'user__default_email',
 $pagelimit=($_GET['limit'] && is_numeric($_GET['limit']))?$_GET['limit']:PAGE_LIMIT;
 $page=($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
 $pageNav=new Pagenate($tickets->count(), $page,$pagelimit);
+$tickets = $pageNav->paginate($tickets);
 
 TicketForm::ensureDynamicDataView();
 
-- 
GitLab