From 0226dc4c60c8394c524bbd6c66bce798c70e1552 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 21 Oct 2015 21:34:30 -0500
Subject: [PATCH] queue: oops: Fix crash searching fields on custom forms

---
 include/class.search.php                       | 13 +++++--------
 include/staff/templates/queue-tickets.tmpl.php |  2 +-
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/include/class.search.php b/include/class.search.php
index c89234029..908bb7fa8 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -905,7 +905,8 @@ class SavedSearch extends VerySimpleModel {
                     ->filter(array('form__type' => 'G'))
                     ->select_related('form');
                 foreach ($dfs as $field) {
-                    $otherFields[$field->getId()] = array($field->form, $field);
+                    $otherFields[$field->getId()] = array($field->form,
+                        $field->getImpl());
                 }
             }
             foreach ($otherFields as $id=>$F) {
@@ -1154,24 +1155,20 @@ class SavedSearch extends VerySimpleModel {
                 // Ensure the special join is created to support custom data joins
                 $name = @static::getOrmPath($name, $qs);
 
-                $name2 = null;
                 if (preg_match('/__answers!\d+__/', $name)) {
-                    // Ensure that only one record is returned from the join through
-                    // the entry and answers joins
-                    $name2 = $this->getAnnotationName().'2';
-                    $query->annotate(array($name2 => SqlAggregate::MAX($name)));
+                    $qs->annotate(array($name2 => SqlAggregate::MAX($name)));
                 }
 
                 // Fetch a criteria Q for the query
                 if (list(,$field) = $searchable[$name])
-                    if ($q = $field->getSearchQ($method, $value, $name2 ?: $name))
+                    if ($q = $field->getSearchQ($method, $value, $name))
                         $qs = $qs->filter($q);
             }
         }
         return $qs;
     }
 
-    function getOrmPath($name, $query=null) {
+    static function getOrmPath($name, $query=null) {
         // Special case for custom data `__answers!id__value`. Only add the
         // join and constraint on the query the first pass, when the query
         // being mangled is received.
diff --git a/include/staff/templates/queue-tickets.tmpl.php b/include/staff/templates/queue-tickets.tmpl.php
index 57a136b88..e0ed19817 100644
--- a/include/staff/templates/queue-tickets.tmpl.php
+++ b/include/staff/templates/queue-tickets.tmpl.php
@@ -27,9 +27,9 @@ if (!$view_all_tickets) {
 }
 
 $page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
+$count = count($tickets);
 $pageNav = new Pagenate($count, $page, PAGE_LIMIT);
 $pageNav->setURL('tickets.php', $args);
-$count = count($tickets);
 $tickets = $pageNav->paginate($tickets);
 
 // Make sure the cdata materialized view is available
-- 
GitLab