From ba2ef65f3a9ecabc0a0f1fccaa861912b65ef01d Mon Sep 17 00:00:00 2001
From: Jared Hancock <gravydish@gmail.com>
Date: Tue, 17 Feb 2015 20:03:31 +0000
Subject: [PATCH] canned: Fixup canned response insertion by staff

---
 include/class.canned.php             | 21 +++++++++++++++++++--
 include/staff/cannedresponse.inc.php |  4 ++--
 scp/js/scp.js                        |  4 ++--
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/include/class.canned.php b/include/class.canned.php
index 31c8bf773..5a396b8bd 100644
--- a/include/class.canned.php
+++ b/include/class.canned.php
@@ -104,6 +104,16 @@ class Canned {
         return $this->_filters;
     }
 
+    function getAttachedFiles($inlines=false) {
+        return AttachmentFile::objects()
+            ->filter(array(
+                'attachments__type'=>'C',
+                'attachments__object_id'=>$this->getId(),
+                'attachments__inline' => $inlines,
+            ))
+            ->all();
+    }
+
     function getNumFilters() {
         return count($this->getFilters());
     }
@@ -148,11 +158,18 @@ class Canned {
                 if ($cb && is_callable($cb))
                     $resp = $cb($resp);
 
-                $resp['files'] = $this->attachments->getSeparates();
+                $resp['files'] = array();
+                foreach ($this->getAttachedFiles(!$html) as $file) {
+                    $resp['files'][] = array(
+                        'id' => $file->id,
+                        'name' => $file->name,
+                        'size' => $file->size,
+                        'download_url' => $file->getDownloadUrl(),
+                    );
+                }
                 // strip html
                 if (!$html) {
                     $resp['response'] = Format::html2text($resp['response'], 90);
-                    $resp['files'] += $this->attachments->getInlines();
                 }
                 return Format::json_encode($resp);
                 break;
diff --git a/include/staff/cannedresponse.inc.php b/include/staff/cannedresponse.inc.php
index 518ad1165..407c5ba89 100644
--- a/include/staff/cannedresponse.inc.php
+++ b/include/staff/cannedresponse.inc.php
@@ -93,10 +93,10 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                 </div>
                 <?php
                 $attachments = $canned_form->getField('attachments');
-                if ($canned && ($files=$canned->attachments->getSeparates())) {
+                if ($canned && ($files=$canned->getAttachedFiles())) {
                     $ids = array();
                     foreach ($files as $f)
-                        $ids[] = $f['id'];
+                        $ids[] = $f->id;
                     $attachments->value = $ids;
                 }
                 print $attachments->render(); ?>
diff --git a/scp/js/scp.js b/scp/js/scp.js
index 1dfeefd1d..edb2cf0c0 100644
--- a/scp/js/scp.js
+++ b/scp/js/scp.js
@@ -221,12 +221,12 @@ var scp_prep = function() {
                         redactor = box.data('redactor');
                     if(canned.response) {
                         if (redactor)
-                            redactor.insertHtml(canned.response);
+                            redactor.insert.html(canned.response);
                         else
                             box.val(box.val() + canned.response);
 
                         if (redactor)
-                            redactor.observeStart();
+                            redactor.observe.load();
                     }
                     //Canned attachments.
                     var ca = $('.attachments', fObj);
-- 
GitLab