From 2133c29f8a29ea716e79384a21ee5ca0dbc99963 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 20 May 2015 16:37:51 -0500
Subject: [PATCH] canned: Fix crash editing canned responses with attachments

---
 include/class.canned.php             |  3 +--
 include/class.forms.php              |  4 ++++
 include/staff/cannedresponse.inc.php | 11 ++++++-----
 scp/canned.php                       |  4 ++--
 4 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/include/class.canned.php b/include/class.canned.php
index 5a396b8bd..8c842776a 100644
--- a/include/class.canned.php
+++ b/include/class.canned.php
@@ -110,8 +110,7 @@ class Canned {
                 'attachments__type'=>'C',
                 'attachments__object_id'=>$this->getId(),
                 'attachments__inline' => $inlines,
-            ))
-            ->all();
+            ));
     }
 
     function getNumFilters() {
diff --git a/include/class.forms.php b/include/class.forms.php
index 0b6da8f27..be6c35e5b 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2241,6 +2241,10 @@ class FileUploadField extends FormField {
         return $this->attachments ? $this->attachments->getAll() : array();
     }
 
+    function setAttachments(GenericAttachments $att) {
+        $this->attachments = $att;
+    }
+
     function getConfiguration() {
         $config = parent::getConfiguration();
         $_types = self::getFileTypes();
diff --git a/include/staff/cannedresponse.inc.php b/include/staff/cannedresponse.inc.php
index 8e407d253..4679c14c0 100644
--- a/include/staff/cannedresponse.inc.php
+++ b/include/staff/cannedresponse.inc.php
@@ -93,11 +93,12 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                 </div>
                 <?php
                 $attachments = $canned_form->getField('attachments');
-                if ($canned && ($files=$canned->getAttachedFiles())) {
-                    $ids = array();
-                    foreach ($files as $f)
-                        $ids[] = $f->id;
-                    $attachments->value = $ids;
+                if ($canned) {
+                    $attachments->setAttachments($canned->attachments);
+                    if ($files = $canned->getAttachedFiles()) {
+                        $ids = $files->values_flat('id')->all();
+                        $attachments->value = $ids;
+                    }
                 }
                 print $attachments->render(); ?>
                 <br/>
diff --git a/scp/canned.php b/scp/canned.php
index 34edc39a0..129d1993b 100644
--- a/scp/canned.php
+++ b/scp/canned.php
@@ -50,8 +50,8 @@ if ($_POST) {
                 $keepers = $canned_form->getField('attachments')->getClean();
                 $attachments = $canned->attachments->getSeparates(); //current list of attachments.
                 foreach($attachments as $k=>$file) {
-                    if($file['id'] && !in_array($file['id'], $keepers)) {
-                        $canned->attachments->delete($file['id']);
+                    if(isset($file->id) && !in_array($file->id, $keepers)) {
+                        $canned->attachments->delete($file->id);
                     }
                 }
 
-- 
GitLab