From b169de8fcde5587f49863ffe99c1f915a9e96ba8 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 05 Aug 2012 06:41:16 -0400
Subject: [PATCH] - Fix order of attachments in sent mail (#1488423)

---
 CHANGELOG                       |    1 
 program/steps/mail/compose.inc  |    1 
 program/steps/mail/sendmail.inc |   13 ++++++
 program/js/app.js               |   51 ++++++++++++-------------
 4 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 1f1acd9..d4eebef 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Fix order of attachments in sent mail (#1488423)
 - Don't show product version on login screen (can be enabled by config)
 - Renamed old default skin to 'classic'. Larry is the new default skin.
 - Support connections to memcached socket file (#1488577)
diff --git a/program/js/app.js b/program/js/app.js
index e0f3b23..afaebec 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -922,14 +922,8 @@
         break;
 
       case 'savedraft':
-        var form = this.gui_objects.messageform, msgid;
-
         // Reset the auto-save timer
         clearTimeout(this.save_timer);
-
-        // saving Drafts is disabled
-        if (!form)
-          break;
 
         // compose form did not change
         if (this.cmp_hash == this.compose_field_hash()) {
@@ -940,35 +934,17 @@
         // re-set keep-alive timeout
         this.start_keepalive();
 
-        msgid = this.set_busy(true, 'savingmessage');
-
-        form.target = "savetarget";
-        form._draft.value = '1';
-        form.action = this.add_url(form.action, '_unlock', msgid);
-        form.submit();
+        this.submit_messageform(true);
         break;
 
       case 'send':
-        if (!this.gui_objects.messageform)
-          break;
-
         if (!props.nocheck && !this.check_compose_input(command))
           break;
 
         // Reset the auto-save timer
         clearTimeout(this.save_timer);
 
-        // all checks passed, send message
-        var lang = this.spellcheck_lang(),
-          form = this.gui_objects.messageform,
-          msgid = this.set_busy(true, 'sendingmessage');
-
-        form.target = 'savetarget';
-        form._draft.value = '';
-        form.action = this.add_url(form.action, '_unlock', msgid);
-        form.action = this.add_url(form.action, '_lang', lang);
-        form.submit();
-
+        this.submit_messageform();
         break;
 
       case 'send-attachment':
@@ -3033,6 +3009,29 @@
       .attr('autocomplete', 'off');
   };
 
+  this.submit_messageform = function(draft)
+  {
+    var form = this.gui_objects.messageform;
+
+    if (!form)
+      return;
+
+    // all checks passed, send message
+    var msgid = this.set_busy(true, draft ? 'savingmessage' : 'sendingmessage'),
+      lang = this.spellcheck_lang(),
+      files = [];
+
+    // send files list
+    $('li', this.gui_objects.attachmentlist).each(function() { files.push(this.id.replace(/^rcmfile/, '')); });
+    $('input[name="_attachments"]', form).val(files.join());
+
+    form.target = 'savetarget';
+    form._draft.value = draft ? '1' : '';
+    form.action = this.add_url(form.action, '_unlock', msgid);
+    form.action = this.add_url(form.action, '_lang', lang);
+    form.submit();
+  };
+
   this.compose_recipient_select = function(list)
   {
     this.enable_command('add-recipient', list.selection.length > 0);
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index e3f8f57..3935ec3 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -1523,6 +1523,7 @@
     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
     $hiddenfields->add(array('name' => '_id', 'value' => $COMPOSE['id']));
+    $hiddenfields->add(array('name' => '_attachments'));
 
     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
     $form_start .= $hiddenfields->show();
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 4790d35..70f1af7 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -458,6 +458,19 @@
   $message_body .= "\r\n</body></html>\r\n";
 }
 
+// sort attachments to make sure the order is the same as in the UI (#1488423)
+$files = get_input_value('_attachments', RCUBE_INPUT_POST);
+if ($files) {
+  $files = explode(',', $files);
+  $files = array_flip($files);
+  foreach ($files as $idx => $val) {
+    $files[$idx] = $COMPOSE['attachments'][$idx];
+    unset($COMPOSE['attachments'][$idx]);
+  }
+
+  $COMPOSE['attachments'] = array_merge(array_filter($files), $COMPOSE['attachments']);
+}
+
 // set line length for body wrapping
 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
 

--
Gitblit v1.9.1