From 7f744012dde76485d938fbfea5f403853dae66f3 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sun, 1 Jan 2017 16:42:25 -0600
Subject: [PATCH] queue: Fix search for assigned team or staff

Fixes the SQL generated for scans by staff_id and team_id on the ticket
table. The fields are not nullable. Instead, the value `0` represents not
having a value.
---
 include/class.search.php | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/include/class.search.php b/include/class.search.php
index 1cb8ac3d1..f95c5cb1b 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -832,7 +832,29 @@ class AssigneeChoiceField extends ChoiceField {
     }
 }
 
+/**
+ * Simple trait which changes the SQL for "has a value" and "does not have a
+ * value" to check for zero or non-zero. Useful for not nullable fields.
+ */
+trait ZeroMeansUnset {
+    function getSearchQ($method, $value, $name=false) {
+        $name = $name ?: $this->get('name');
+        switch ($method) {
+        // osTicket commonly uses `0` to represent an unset state, so
+        // the set and unset checks need to check for both not null and
+        // nonzero
+        case 'nset':
+            return new Q([$name => 0]);
+        case 'set':
+            return Q::not([$name => 0]);
+        }
+        return parent::getSearchQ($method, $value, $name);
+    }
+}
+
 class AgentSelectionField extends ChoiceField {
+    use ZeroMeansUnset;
+
     function getChoices($verbose=false) {
         return Staff::getStaffMembers();
     }
@@ -858,6 +880,8 @@ class AgentSelectionField extends ChoiceField {
 }
 
 class TeamSelectionField extends ChoiceField {
+    use ZeroMeansUnset;
+
     function getChoices($verbose=false) {
         return Team::getTeams();
     }
-- 
GitLab