From e305a5a0c5daa343ab97aefe664a5f696435fe53 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sat, 21 May 2016 21:31:27 -0500
Subject: [PATCH] queue: Fix pagination of tickets

Turns out the pagination system is somewhat chicken and egg with respect to
determining the number of pages. The query needs to be sent to the database
to fetch the page content so that the database can also count the total
number of records available.
---
 include/class.pagenate.php                     | 12 ++++++++++--
 include/staff/templates/queue-tickets.tmpl.php | 11 ++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/include/class.pagenate.php b/include/class.pagenate.php
index 69368393e..b20ad52a5 100644
--- a/include/class.pagenate.php
+++ b/include/class.pagenate.php
@@ -25,10 +25,15 @@ class PageNate {
 
 
     function __construct($total,$page,$limit=20,$url='') {
-        $this->total = intval($total);
         $this->limit = max($limit, 1 );
         $this->page  = max($page, 1 );
         $this->start = max((($page-1)*$this->limit),0);
+        $this->setURL($url);
+        $this->setTotal($total);
+    }
+
+    function setTotal($total) {
+        $this->total = intval($total);
         $this->pages = ceil( $this->total / $this->limit );
 
         if (($this->limit > $this->total) || ($this->page>ceil($this->total/$this->limit))) {
@@ -37,7 +42,6 @@ class PageNate {
         if (($this->limit-1)*$this->start > $this->total) {
             $this->start -= $this->start % $this->limit;
         }
-        $this->setURL($url);
     }
 
     function setURL($url='',$vars='') {
@@ -160,5 +164,9 @@ class PageNate {
         return $qs->limit($end-$start)->offset($start);
     }
 
+    function paginateSimple(QuerySet $qs) {
+        return $qs->limit($this->getLimit() + $this->slack)->offset($this->getStart());
+    }
+
 }
 ?>
diff --git a/include/staff/templates/queue-tickets.tmpl.php b/include/staff/templates/queue-tickets.tmpl.php
index 212f72541..22475ec6f 100644
--- a/include/staff/templates/queue-tickets.tmpl.php
+++ b/include/staff/templates/queue-tickets.tmpl.php
@@ -91,10 +91,11 @@ if (!$sorted && isset($sort['queuesort'])) {
 // Apply pagination
 
 $page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
+$pageNav = new Pagenate(PHP_INT_MAX, $page, PAGE_LIMIT);
+$tickets = $pageNav->paginateSimple($tickets);
 $count = $tickets->total();
-$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
+$pageNav->setTotal($count);
 $pageNav->setURL('tickets.php', $args);
-$tickets = $pageNav->paginate($tickets);
 ?>
 
 <!-- SEARCH FORM START -->
@@ -211,7 +212,7 @@ if (
 $canManageTickets = $thisstaff->canManageTickets();
 if ($canManageTickets) { ?>
         <th style="width:12px"></th>
-<?php 
+<?php
 }
 foreach ($columns as $C) {
     $heading = Format::htmlchars($C->getLocalHeading());
@@ -234,9 +235,9 @@ foreach ($columns as $C) {
 foreach ($tickets as $T) {
     echo '<tr>';
     if ($canManageTickets) { ?>
-        <td><input type="checkbox" class="ckb" name="tids[]" 
+        <td><input type="checkbox" class="ckb" name="tids[]"
             value="<?php echo $T['ticket_id']; ?>" /></td>
-<?php 
+<?php
     }
     foreach ($columns as $C) {
         list($contents, $styles) = $C->render($T);
-- 
GitLab