From c456684c608021ada48562f3f4aecce390d0f06e Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Wed, 8 Aug 2018 17:05:49 -0500
Subject: [PATCH] issue: FAQ & Canned Attachments Dropping

This addresses an issue where FAQ & Canned Response attachments are dropping
when another agent edits the FAQ or Canned Response.
---
 scp/canned.php |  7 +++++++
 scp/faq.php    | 30 +++++++++++++++---------------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/scp/canned.php b/scp/canned.php
index ae8ded5fe..88fdf4b81 100644
--- a/scp/canned.php
+++ b/scp/canned.php
@@ -37,6 +37,13 @@ $canned_form = new SimpleForm(array(
    )),
 ));
 
+// Set fields' attachments so exsting files stay put
+if ($canned
+    && $canned->attachments
+    && ($attachments = $canned_form->getField('attachments'))) {
+     $attachments->setAttachments($canned->attachments);
+}
+
 if ($_POST) {
     switch(strtolower($_POST['do'])) {
         case 'update':
diff --git a/scp/faq.php b/scp/faq.php
index 310809947..8b948c142 100644
--- a/scp/faq.php
+++ b/scp/faq.php
@@ -48,6 +48,21 @@ if ($langs = $cfg->getSecondaryLanguages()) {
 
 $faq_form = new SimpleForm($form_fields, $_POST);
 
+// Set fields' attachments so exsting files stay put
+if ($faq
+    && $faq->getAttachments()->window(array('inline' => false))
+    && ($common_attachments = $faq_form->getField('attachments'))) {
+     // Common attachments
+     $common_attachments->setAttachments($faq->getAttachments()->window(array('inline' => false)));
+}
+if ($langs && $faq) {
+    // Multi-lingual system
+    foreach ($langs as $lang) {
+        $attachments = $faq_form->getField('attachments.'.$lang);
+        $attachments->setAttachments($faq->getAttachments($lang)->window(array('inline' => false)));
+    }
+}
+
 if ($_POST) {
     $errors=array();
     // General attachments
@@ -126,21 +141,6 @@ if ($_POST) {
 
     }
 }
-else {
-    // Not a POST — load database-backed attachments to attachment fields
-    if ($langs && $faq) {
-        // Multi-lingual system
-        foreach ($langs as $lang) {
-            $attachments = $faq_form->getField('attachments.'.$lang);
-            $attachments->setAttachments($faq->getAttachments($lang)->window(array('inline' => false)));
-        }
-    }
-    if ($faq) {
-        // Common attachments
-        $attachments = $faq_form->getField('attachments');
-        $attachments->setAttachments($faq->getAttachments()->window(array('inline' => false)));
-    }
-}
 
 $inc='faq-categories.inc.php'; //FAQs landing page.
 if($faq) {
-- 
GitLab