Index: modules/upload.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/upload.module,v
retrieving revision 1.64
diff -u -r1.64 upload.module
--- modules/upload.module	30 Nov 2005 11:03:58 -0000	1.64
+++ modules/upload.module	2 Dec 2005 01:32:51 -0000
@@ -153,32 +153,9 @@
   }
 }
 
-/**
- * Implementation of hook_nodeapi().
- */
-function upload_nodeapi(&$node, $op, $arg) {
-  switch ($op) {
-    case 'validate':
-      $node->files = upload_load($node);
-      // Double check existing files:
-      if (is_array($node->list)) {
-        foreach ($node->list as $key => $value) {
-          if ($file = file_check_upload($key)) {
-            $node->files[$file->source] = $file;
-            $node->files[$key]->list = $node->list[$key];
-            $node->files[$key]->remove = $node->remove[$key];
-            $node->files[$key]->description = $node->description[$key];
-            if ($file->source) {
-              $filesize += $file->filesize;
-            }
-          }
-        }
-      }
-      else {
-        foreach ($node->files as $key => $file) {
-          $node->list[$key] = $file->list;
-        }
-      }
+
+function _upload_load_upload(&$node) {
+      // Get a new Upload
       if (($file = file_check_upload('upload')) && user_access('upload files')) {
         global $user;
 
@@ -226,15 +203,52 @@
           $node->files[$key] = $file;
         }
       }
-      for ($x = 0; $x < count($_SESSION['file_uploads']); $x++) {
-        $key = 'upload_' . $x;
-        if ($file = file_check_upload($key)) {
-          $node->files[$key] = $file;
+      
+      if( is_array($node->list) || is_array($node->files)) {
+        // Double check existing files:
+        if (is_array($node->list)) {
+          foreach ($node->list as $key => $value) {
+            if ($file = file_check_upload($key)) {
+              $node->files[$file->source] = $file;
+              $node->files[$key]->list = $node->list[$key];
+              $node->files[$key]->remove = $node->remove[$key];
+              $node->files[$key]->description = $node->description[$key];
+
+              if ($file->source) {
+                $filesize += $file->filesize;
+              }
+            }
+          }
+        }
+        else {
+          foreach ($node->files as $key => $file) {
+            $node->list[$key] = $file->list;
+          }
+        }
+  
+        for ($x = 0; $x < count($_SESSION['file_uploads']); $x++) {
+          $key = 'upload_' . $x;
+          if ($file = file_check_upload($key)) {
+            $node->files[$key] = $file;
+          }
         }
       }
-      break;
+}
+/**
+ * Implementation of hook_nodeapi().
+ */
+function upload_nodeapi(&$node, $op, $arg) {
+  switch ($op) {
 
     case 'form':
+      // To reset uploads array in session upon entering first time.
+      if(sizeof($_POST) == 0) {
+        $_SESSION['file_uploads'] = NULL;
+      }
+      $node->files = upload_load($node);
+      if($_POST['fileop'] == t('Attach') || $arg == t('Attach') || !$_POST['edit']['op']) {
+        _upload_load_upload($node);
+      }
       if (variable_get("upload_$node->type", 1) == 1 && user_access('upload files')) {
         $output = upload_form($node);
         $output['#attributes'] = array('enctype' => 'multipart/form-data');
@@ -248,6 +262,10 @@
       break;
 
     case 'view':
+      
+      $node->files = upload_load($node);
+      _upload_load_upload($node);
+      
       if ($node->files && user_access('view uploaded files')) {
         $header = array(t('Attachment'), t('Size'));
         $rows = array();
@@ -424,6 +442,7 @@
   if (is_array($node->files) && count($node->files)) {
     $form['current']['#theme'] = 'upload_form_current';
     $form['current']['description']['#tree'] = TRUE;
+    $form['current']['vid'] = array('#type' => 'hidden', '#value' => $node->vid);
     foreach ($node->files as $key => $file) {
       $options[$key] = '';
       if ($file->remove) {
@@ -442,7 +461,6 @@
   }
 
   if (user_access('upload files')) {
-
     $form['new']['upload'] = array('#type' => 'file', '#title' => t('Attach new file'), '#size' => 40);
     $form['new']['fileop'] = array('#type' => 'button', '#value' => t('Attach'), '#name'=> 'fileop', '#attributes' => array('id' => 'fileop'));
     // The class triggers the js upload behaviour.
@@ -516,7 +534,11 @@
 function upload_js() {
   // We only do the upload.module part of the node validation process.
   $node = array2object($_POST['edit']);
-  upload_nodeapi($node, 'validate', NULL);
+
+  
+  $node->files = upload_load($node);
+  _upload_load_upload($node);
+
   $form = _upload_form($node);
   $form = _form_builder('upload_js', $form);
   $output = theme('status_messages') . form_render($form);
@@ -525,3 +547,4 @@
   print drupal_call_js('window.parent.iframeHandler', $output);
   exit;
 }
+
