From 21bb5a78eafe4c76fa2c161786b5da8db8786024 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 24 Jun 2015 16:31:52 -0500
Subject: [PATCH] faq: Fix crash adding attachments to FAQ article

---
 include/class.attachment.php | 5 +----
 include/class.faq.php        | 8 ++------
 include/class.forms.php      | 2 +-
 include/class.orm.php        | 4 ++++
 4 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/include/class.attachment.php b/include/class.attachment.php
index 6a9040154..c0e9761eb 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -109,8 +109,7 @@ extends InstrumentedList {
     function keepOnlyFileIds($ids, $inline=false, $lang=false) {
         $new = array_fill_keys($ids, 1);
         foreach ($this as $A) {
-            $idx = array_search($A->file_id, $ids);
-            if ($idx === false && (!$A->lang || $A->lang == $lang))
+            if (!isset($new[$A->file_id]) && $A->lang == $lang)
                 // Not in the $ids list, delete
                 $this->remove($A);
             unset($new[$A->file_id]);
@@ -184,8 +183,6 @@ extends InstrumentedList {
 
         if ($lang)
             $base = $base->filter(array('lang' => $lang));
-        else
-            $base = $base->filter(array('lang__isnull' => true));
 
         return $base;
     }
diff --git a/include/class.faq.php b/include/class.faq.php
index 9f0b83ccd..a551beefa 100644
--- a/include/class.faq.php
+++ b/include/class.faq.php
@@ -312,12 +312,8 @@ class FAQ extends VerySimpleModel {
         return true;
     }
 
-    function getAttachments($lang=false) {
-        $att = $this->attachments;
-        if ($lang)
-            $att = $att->window(array('lang'=>$lang));
-
-        return $att;
+    function getAttachments($lang=null) {
+        return $this->attachments->window(array('lang'=>$lang));
     }
 
     function getAttachmentsLinks($separator=' ',$target='') {
diff --git a/include/class.forms.php b/include/class.forms.php
index f5f3ce86f..55f62a66b 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2323,7 +2323,7 @@ class FileUploadField extends FormField {
                 sprintf('%u', crc32('E'.$this->get('id').$e->get('id'))),
                 'E');
         }
-        return $this->attachments ? $this->attachments->getAll() : array();
+        return $this->attachments ?: array();
     }
 
     function setAttachments(GenericAttachments $att) {
diff --git a/include/class.orm.php b/include/class.orm.php
index 8c0315811..516d714da 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -304,6 +304,10 @@ class VerySimpleModel {
         if ($this->__new__)
             return null;
 
+        // Check to see if the column referenced is actually valid
+        if (in_array($field, static::getMeta('fields')))
+            return null;
+
         // TODO: Inspect fields from database before throwing this error
         throw new OrmException(sprintf(__('%s: %s: Field not defined'),
             get_class($this), $field));
-- 
GitLab