From 2afff6a2db783379cc7cd7693f7ba18ed930c192 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sat, 21 May 2016 07:37:17 -0500
Subject: [PATCH] queues: Fix several issues reported by @kest874

- Fix crash on new ticket
- Fix crash on user and org dialog popup from ticket
- Fix missing queue mass-action buttons
---
 include/class.orm.php                         |  9 ++++-
 .../staff/templates/queue-tickets.tmpl.php    | 40 +++++++++++--------
 scp/tickets.php                               |  4 +-
 3 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index ae82a7911..53970b9d6 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -124,8 +124,10 @@ class ModelMeta implements ArrayAccess {
     function extend(ModelMeta $child, $meta) {
         $this->subclasses[$child->model] = $child;
         // Merge 'joins' settings (instead of replacing)
-        if (isset($this->meta['joins']))
-            $meta['joins'] += $this->meta['joins'];
+        if (isset($this->meta['joins'])) {
+            $meta['joins'] = array_merge($meta['joins'] ?: array(),
+                $this->meta['joins']);
+        }
         return $meta + $this->meta + self::$base;
     }
 
@@ -1358,6 +1360,9 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
      * after this is run, the changes should be made in a clone.
      */
     function total() {
+        if (isset($this->total))
+            return $this->total;
+
         // Optimize the query with the CALC_FOUND_ROWS if
         // - the compiler supports it
         // - the iterator hasn't yet been built, that is, the query for this
diff --git a/include/staff/templates/queue-tickets.tmpl.php b/include/staff/templates/queue-tickets.tmpl.php
index d7a180bbc..212f72541 100644
--- a/include/staff/templates/queue-tickets.tmpl.php
+++ b/include/staff/templates/queue-tickets.tmpl.php
@@ -72,6 +72,29 @@ elseif ($queue_sort = $queue->getDefaultSort()) {
         'dir' => (int) $_GET['dir'] ?: 0,
     );
 }
+
+// Handle current sorting preferences
+
+$sorted = false;
+foreach ($columns as $C) {
+    // Sort by this column ?
+    if (isset($sort['col']) && $sort['col'] == $C->id) {
+        $tickets = $C->applySort($tickets, $sort['dir']);
+        $sorted = true;
+    }
+}
+if (!$sorted && isset($sort['queuesort'])) {
+    // Apply queue sort-dropdown selected preference
+    $sort['queuesort']->applySort($tickets, $sort['dir']);
+}
+
+// Apply pagination
+
+$page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
+$count = $tickets->total();
+$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
+$pageNav->setURL('tickets.php', $args);
+$tickets = $pageNav->paginate($tickets);
 ?>
 
 <!-- SEARCH FORM START -->
@@ -190,8 +213,6 @@ if ($canManageTickets) { ?>
         <th style="width:12px"></th>
 <?php 
 }
-
-$sorted = false;
 foreach ($columns as $C) {
     $heading = Format::htmlchars($C->getLocalHeading());
     if ($C->isSortable()) {
@@ -204,27 +225,12 @@ foreach ($columns as $C) {
     }
     echo sprintf('<th width="%s" data-id="%d">%s</th>',
         $C->getWidth(), $C->id, $heading);
-
-    // Sort by this column ?
-    if (isset($sort['col']) && $sort['col'] == $C->id) {
-        $tickets = $C->applySort($tickets, $sort['dir']);
-        $sorted = true;
-    }
-}
-if (!$sorted && isset($sort['queuesort'])) {
-    $sort['queuesort']->applySort($tickets, $sort['dir']);
 }
 ?>
     </tr>
   </thead>
   <tbody>
 <?php
-$page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
-$count = $tickets->total();
-$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
-$pageNav->setURL('tickets.php', $args);
-$tickets = $pageNav->paginate($tickets);
-
 foreach ($tickets as $T) {
     echo '<tr>';
     if ($canManageTickets) { ?>
diff --git a/scp/tickets.php b/scp/tickets.php
index 783a27830..23475ee9f 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -385,7 +385,9 @@ if($_POST && !$errors):
                              __('Contact admin for such access'));
                 } else {
                     $vars = $_POST;
-                    $vars['uid'] = $user? $user->getId() : 0;
+
+                    if ($vars['uid'] && (!User::lookup($vars['uid'])))
+                        $vars['uid'] = 0;
 
                     $vars['cannedattachments'] = $response_form->getField('attachments')->getClean();
 
-- 
GitLab