From f179cf155ce298dbda092d85b5f038755ea372a9 Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Wed, 8 Aug 2018 14:44:28 -0500
Subject: [PATCH] issue: Old Attachment Names
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This addresses an issue where uploading a file you’ve uploaded before but
renamed is not saving the new name in the Attachment table.
---
 include/class.attachment.php | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/class.attachment.php b/include/class.attachment.php
index 15f3fd9b1..bcda6d08a 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -108,15 +108,23 @@ extends InstrumentedList {
      */
     function keepOnlyFileIds($ids, $inline=false, $lang=false) {
         if (!$ids) $ids = array();
-        $new = array_fill_keys($ids, 1);
+        $new = array_flip($ids);
         foreach ($this as $A) {
             if (!isset($new[$A->file_id]) && $A->lang == $lang && $A->inline == $inline)
                 // Not in the $ids list, delete
                 $this->remove($A);
             unset($new[$A->file_id]);
         }
-        // Everything remaining in $new is truly new
-        $this->upload(array_keys($new), $inline, $lang);
+        $attachments = array();
+        // Format $new for upload() with new name
+        foreach ($new as $id=>$name) {
+            $attachments[] = array(
+                    'id' => $id,
+                    'name' => $name
+                );
+        }
+        // Everything remaining in $attachments is truly new
+        $this->upload($attachments, $inline, $lang);
     }
 
     function upload($files, $inline=false, $lang=false) {
-- 
GitLab