Index: contrib/image_attach/image_attach.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_attach/image_attach.module,v
retrieving revision 1.37
diff -u -p -r1.37 image_attach.module
--- contrib/image_attach/image_attach.module	12 Feb 2008 21:11:43 -0000	1.37
+++ contrib/image_attach/image_attach.module	4 Apr 2008 02:54:24 -0000
@@ -40,7 +40,6 @@ function image_attach_menu() {
   return $items;
 }
 
-
 function image_attach_admin_settings() {
   $form = array();
 
@@ -230,6 +229,7 @@ function image_attach_form_alter(&$form,
  * Implementation of hook_nodeapi().
  */
 function image_attach_nodeapi(&$node, $op, $teaser, $page) {
+  
   // Make sure that if an image is deleted it is detached from any nodes.
   if ($node->type == 'image') {
     switch ($op) {
@@ -242,35 +242,35 @@ function image_attach_nodeapi(&$node, $o
     return;
   }
   switch ($op) {
-    case 'prepare':
+    
+    case 'validate':
 	  $validators = array(
 	    'file_validate_is_image' => array(),
 	  );
-      if (file_save_upload('image', $validators)) {
-        $image->title = $_POST['image_title'];
-        $image->uid = $node->uid;
-        $image->name = $node->name;
-        $image->created = $node->created;
-        $image->type = 'image';
+	  
+      if($node->attached_image = file_save_upload('image', $validators)) {
+        
+        $node->attached_image->title = $_POST['image_title'];
+        $node->attached_image->uid = $node->uid;
+        $node->attached_image->name = $node->name;
+        $node->attached_image->created = $node->created;
+        $node->attached_image->type = 'image';
+        
         // Set the node's defaults... (copied this from node and comment.module)
         $node_options = variable_get('node_options_image', array('status', 'promote'));
-        $image->status = in_array('status', $node_options);
-        $image->promote = in_array('promote', $node_options);
+        $node->attached_image->status = in_array('status', $node_options);
+        $node->attached_image->promote = in_array('promote', $node_options);
         if (module_exists('comment')) {
-          $image->comment = variable_get('comment_image', COMMENT_NODE_READ_WRITE);
+          $node->attached_image->comment = variable_get('comment_image', COMMENT_NODE_READ_WRITE);
         }
-        image_prepare($image, 'image');
-        if ($image->images) {
-          if (empty($image->title)) {
-            $image->title = basename($image->images[IMAGE_ORIGINAL]);
-          }
-          node_validate($image);
-          if (!form_get_errors()) {
-            $image = node_submit($image);
-            node_save($image);
-            $node->iid = $image->nid;
-            $node->new_image = TRUE;
+        
+        // the following was mostly handled in image_prepare in Drupal 5
+        if ($node->attached_image->filepath) {
+          $node->attached_image->images[IMAGE_ORIGINAL] = $node->attached_image->filepath;
+          if (empty($node->attached_image->title)) {
+            $node->attached_image->title = basename($node->attached_image->filepath);
           }
+          node_validate($node->attached_image);
         }
       }
       elseif (!empty($_POST['iid'])) {
@@ -278,10 +278,17 @@ function image_attach_nodeapi(&$node, $o
       }
       break;
 
-    case 'insert':
     case 'update':
+    case 'insert':
+      if($node->attached_image) {
+        node_save($node->attached_image);
+        $node->iid = $node->attached_image->nid;
+        $node->new_image = TRUE;
+      }
       if (isset($node->iid)) {
-        db_query("DELETE FROM {image_attach} WHERE nid=%d", $node->nid);
+        if($op == 'update') {
+          db_query("DELETE FROM {image_attach} WHERE nid=%d", $node->nid);
+        }
         if ($node->iid > 0) {
           db_query("INSERT INTO {image_attach} (nid, iid) VALUES (%d, %d)", $node->nid, $node->iid);
         }
