Index: attachment.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/attachment/attachment.install,v
retrieving revision 1.3
diff -u -r1.3 attachment.install
--- attachment.install	17 Jul 2007 19:50:04 -0000	1.3
+++ attachment.install	4 Nov 2007 11:56:27 -0000
@@ -10,6 +10,23 @@
 }
 
 /**
+ * Add weight column to database table
+ */
+function attachment_update_2() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+        $ret[] = update_sql("ALTER TABLE {attachment} ADD COLUMN weight int(10) NOT NULL default 0;");
+      break;
+    case 'pgsql':
+        $ret[] = update_sql("alter table {attachment} add column weight integer not null;");
+      break;
+  }
+  return $ret;
+}
+
+/**
  * Install the initial schema.
  */
 function attachment_install() {
@@ -23,6 +40,7 @@
             aid int(10) unsigned NOT NULL,
             nid int(10) unsigned NOT NULL,
             fid int(10) unsigned NOT NULL,
+	    weight int(10) NOT NULL default 0,
             filename varchar(255) NOT NULL,
             title varchar(255) NOT NULL,
             description varchar(255) NOT NULL,
@@ -41,6 +59,7 @@
           aid serial primary key,
           nid integer not null,
           fid integer not null,
+          weight integer not null,
           filename varchar(255) not null,
           title varchar(255) not null,
           description varchar(255) not null,
Index: attachment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/attachment/attachment.module,v
retrieving revision 1.20
diff -u -r1.20 attachment.module
--- attachment.module	2 Aug 2007 17:38:57 -0000	1.20
+++ attachment.module	4 Nov 2007 11:58:01 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: attachment.module,v 1.20 2007/08/02 17:38:57 cscsteve Exp $
+// $Id:$
 
 define(ATTACHMENT_EXTENSION_WHITELIST, variable_get('attachment_text_rename_whitelist', 'jpg jpeg gif png tiff txt html doc xls pdf ppt pps odt mp3 ogg wav wmv mpg'));
 
@@ -58,6 +58,7 @@
           $form['attachments'][$key]['hidden'] = array('#type'=>'checkbox', '#return_value'=>1, '#default_value'=>$attachment['hidden']);
           $form['attachments'][$key]['title'] = array('#type'=>'textfield', '#size'=>30, '#maxlength'=>255, '#default_value'=>$attachment['title']);
           $form['attachments'][$key]['description'] = array('#type'=>'textfield', '#size'=>100, '#maxlength'=>255, '#default_value'=>$attachment['description']);
+          $form['attachments'][$key]['weight'] = array('#type'=>'weight','#default_value'=>$attachment['weight']);
           $form['attachments'][$key]['display'] = array('#type'=>'markup', '#value'=> '<strong>Filename:</strong>'. $attachment['filename'] .'<br /><strong>URL:</strong><span class="attachment-url">'. module_invoke('filemanager', 'url', $attachment['fid'], FALSE, TRUE) .'<br /><strong>Size:</strong>' . format_size($attachment['size']) . '</span>');
         }
 
@@ -126,11 +127,11 @@
         if (!$attachment['deleted']) {
           module_invoke('filemanager', 'promote_working', $attachment['fid']);
           if ($attachment['aid']) {
-            db_query("UPDATE {attachment} SET title='%s', description = '%s', size=%d, hidden='%s' WHERE aid=%d", $attachment['title'], $attachment['description'], $attachment['size'], $attachment['hidden'], $attachment['aid']);
+            db_query("UPDATE {attachment} SET title='%s', description = '%s', size=%d, hidden='%s', weight='%d' WHERE aid=%d", $attachment['title'], $attachment['description'], $attachment['size'], $attachment['hidden'], $attachment['weight'], $attachment['aid']);
           }
           else {
              $aid = db_next_id('{attachment}_aid');
-             db_query("INSERT INTO {attachment} (aid,title,description,nid,fid,filename,size,hidden) VALUES (%d,'%s','%s',%d,%d,'%s',%d,'%s')", $aid, $attachment['title'],$attachment['description'],$node->nid, $attachment['fid'], $attachment['filename'], $attachment['size'], $attachment['hidden']);
+             db_query("INSERT INTO {attachment} (aid,title,description,nid,fid,filename,size,hidden,weight) VALUES (%d,'%s','%s',%d,%d,'%s',%d,'%s',%d)", $aid, $attachment['title'],$attachment['description'],$node->nid, $attachment['fid'], $attachment['filename'], $attachment['size'], $attachment['hidden'], $attachment['weight']);
           }
         }
         else {
@@ -269,34 +270,37 @@
  * Theme the attachment form
  */
 function theme_attachment_form($form) {
+  _attachments_enable_dragdrop();
   $output = '';
   $header = array(
     t('Delete'),
     t('Hidden'),
+    t('Weight'),
     t('Title'),
     t('Description'),
   );
 
   $rows = array();
+
+$output .= "<ul class='attachment-container'>";
   foreach (element_children($form) as $key) {
     if ($key{0} != 'c') {
-      $row = array();
-      $row[0]['data'] = drupal_render($form[$key]['display']);
-      $row[0]['colspan'] = '4';
-      $rows[] = $row;
       $row = array(
         drupal_render($form[$key]['deleted']),
         drupal_render($form[$key]['hidden']),
+        drupal_render($form[$key]['weight']),
         drupal_render($form[$key]['title']),
         drupal_render($form[$key]['description']),
       );
-      $rows[] = $row;
+      $output .= '<li class="attachment-item" id="' . $key . '">' 
+              . '<div class="attachment-display">' 
+        . drupal_render($form[$key]['display']) . "</div>"
+              . theme('table', $header, array($row))
+              . '</li>';
     }
   }
 
-  if (count($rows) > 0) {
-    $output .= theme('table', $header, $rows);
-  }
+  $output .= "</ul><div style='clear:both'></div>";
 
   $output .= drupal_render($form);
 
@@ -381,7 +385,7 @@
  * Load the attachments for the given node
  */
 function attachment_load($node) {
-  $result = db_query("SELECT aid, title, description, fid, filename, size, hidden FROM {attachment} WHERE nid = %d", $node->nid);
+  $result = db_query("SELECT aid, title, description, fid, filename, size, hidden,weight FROM {attachment} WHERE nid = %d ORDER BY weight ASC,title", $node->nid);
   while ($attachment = db_fetch_array($result)) {
     $attachment['deleted'] = FALSE;
     $attachment['working'] = FALSE;
@@ -451,3 +455,36 @@
  * @} end of addtogroup themeable
  */
 
+/**
+ * includes javascript to enable drag and drop of attachment list to
+ * rearrange the order of the attachments. requires jquery interface 
+ * module which if absent will fall back on letting user reorder list
+ * via the weights.
+ */
+function _attachments_enable_dragdrop() {
+  if(module_exists('jquery_interface')) {
+      jquery_interface_add();
+      drupal_add_js( <<<EOT
+$(document).ready(
+  function () {
+  $('ul.attachment-container').Sortable (
+    {
+      accept: 'attachment-item',
+      axis: 'vertically',
+      opacity: 0.8,
+      onStop: function() {
+        //go through list of items and weight them
+        //by order.
+        var wt = -10;
+        $(this).parent().children('li').each( function(){
+          var id = $(this).attr('id');
+          $('select#edit-attachments-' + id + '-weight').val(wt);
+          wt++;
+        });
+      }
+    });
+  });
+EOT
+, 'inline');
+  }
+}
