From 1922920ac474a55e3f147925e38db20b16485f3a Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 5 Jan 2015 09:28:20 -0600
Subject: [PATCH] oops: Fix several small, general issues

  * Fix fatal errors adding a new FAQ (fixes #1648)
  * Fix removal of attachments when deleting a thread (fixes #1650)
  * Fix crash discovering department members for alerts (fixes #1652)
---
 include/class.dept.php   |  6 +++---
 include/class.faq.php    | 15 +++++++--------
 include/class.thread.php |  8 ++++----
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index 3f623b411..98d3fd80e 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -121,15 +121,15 @@ class Dept extends VerySimpleModel {
                 ->filter(Q::any(array(
                     'dept_id' => $this->getId(),
                     new Q(array(
-                        'group__depts__id' => $this->getId(),
-                        'group__depts__group_membership' => self::ALERTS_DEPT_AND_GROUPS,
+                        'group__depts__dept_id' => $this->getId(),
+                        'group__depts__dept__group_membership' => self::ALERTS_DEPT_AND_GROUPS,
                     )),
                     'staff_id' => $this->manager_id
                 )));
 
             if ($criteria && $criteria['available'])
                 $members->filter(array(
-                    'group__group_enabled' => 1,
+                    'group__flags__hasbit' => Group::FLAG_ENABLED,
                     'isactive' => 1,
                     'onvacation' => 0,
                 ));
diff --git a/include/class.faq.php b/include/class.faq.php
index 4e0fe8f45..f4bb27b00 100644
--- a/include/class.faq.php
+++ b/include/class.faq.php
@@ -14,6 +14,7 @@
 **********************************************************************/
 require_once('class.file.php');
 require_once('class.category.php');
+require_once('class.thread.php');
 
 class FAQ extends VerySimpleModel {
 
@@ -28,15 +29,14 @@ class FAQ extends VerySimpleModel {
                 'constraint' => array(
                     'category_id' => 'Category.category_id'
                 ),
-            ),
+            ), /* XXX: Not yet implemented
             'attachments' => array(
                 'constraint' => array(
                     "'F'" => 'GenericAttachment.type',
                     'faq_id' => 'GenericAttachment.object_id',
                 ),
                 'list' => true,
-                'null' => true,
-            ),
+            ), */
             'topics' => array(
                 'constraint' => array(
                     'faq_id' => 'FaqTopic.faq_id'
@@ -76,7 +76,7 @@ class FAQ extends VerySimpleModel {
         return Format::truncate(Format::striptags($this->answer), 150);
     }
     function getSearchableAnswer() {
-        return ThreadBody::fromFormattedText($this->answer, 'html')
+        return ThreadEntryBody::fromFormattedText($this->answer, 'html')
             ->getSearchable();
     }
     function getNotes() { return $this->notes; }
@@ -378,12 +378,11 @@ class FAQ extends VerySimpleModel {
     }
 
     static function findIdByQuestion($question) {
-        $object = self::objects()->filter(array(
+        $row = self::objects()->filter(array(
             'question'=>$question
-        ))->values_flat('faq_id')->one();
+        ))->values_flat('faq_id')->first();
 
-        if ($object)
-            return $object[0];
+        return ($row) ? $row[0] : null;
     }
 
     static function findByQuestion($question) {
diff --git a/include/class.thread.php b/include/class.thread.php
index 71dc5525f..55273469c 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -159,10 +159,10 @@ class Thread {
     function deleteAttachments() {
 
         // Clear reference table
-        $sql = 'DELETE FROM '.ATTACHMENT_TABLE. ' a '
-             . 'INNER JOIN '.THREAD_ENTRY_TABLE.' e
-                    ON(e.id = a.object_id AND a.`type`= "H") '
-             . ' WHERE e.thread_id='.db_input($this->getId());
+        $sql = 'DELETE `a`.* FROM '.ATTACHMENT_TABLE. ' `a` '
+             . 'INNER JOIN '.THREAD_ENTRY_TABLE.' `e`
+                    ON(`e`.id = `a`.object_id AND `a`.`type`= "H") '
+             . ' WHERE `e`.thread_id='.db_input($this->getId());
 
         $deleted=0;
         if (($res=db_query($sql)) && ($deleted=db_affected_rows()))
-- 
GitLab