From da13458bffcbecdf788d5d1721af5671d33df7bd Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Fri, 6 Jul 2018 07:42:21 +0000
Subject: [PATCH] Referral: Check if an agent has a thread referral

This commit addresses a bug where "referral check" filters were not being
considered when checking if a thread is referred to one of the agent's
departments or teams.
---
 include/class.thread.php | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index 93774d922..3393043e6 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -273,7 +273,7 @@ implements Searchable {
     function isReferred($to=null, $strict=false) {
 
         if (is_null($to) || !$this->referrals)
-            return ($this->referrals);
+            return ($this->referrals && $this->referrals->count());
 
         switch (true) {
         case $to instanceof Staff:
@@ -287,21 +287,24 @@ implements Searchable {
                 return false;
 
             // Referred to staff's department
-            if ($to->getDepts() && $this->referrals->filter(array(
-                            'object_id__in' => $to->getDepts(),
-                            'object_type'   => ObjectModel::OBJECT_TYPE_DEPT)))
+            if ($this->referrals->findFirst(array(
+                   'object_id__in' => $to->getDepts(),
+                   'object_type'   => ObjectModel::OBJECT_TYPE_DEPT)))
                 return true;
-            // Referred to staff's  team
-            if ($to->getTeams() && $this->referrals->filter(array(
+
+            // Referred to staff's teams
+            if ($to->getTeams() && $this->referrals->findFirst(array(
                             'object_id__in' => $to->getTeams(),
-                            'object_type'   => ObjectModel::OBJECT_TYPE_TEAM)))
+                            'object_type'   => ObjectModel::OBJECT_TYPE_TEAM
+                            )))
                 return true;
+
+            return false;
             break;
         case $to instanceof Dept:
             // Refered to the dept
-            if ($this->getReferral($to->getId(),
-                        ObjectModel::OBJECT_TYPE_DEPT))
-                return true;
+            return ($this->getReferral($to->getId(),
+                        ObjectModel::OBJECT_TYPE_DEPT));
             break;
         }
 
-- 
GitLab