Index: image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v
retrieving revision 1.276
diff -u -p -r1.276 image.module
--- image.module	17 Aug 2008 08:35:36 -0000	1.276
+++ image.module	21 Aug 2008 12:53:36 -0000
@@ -170,66 +170,6 @@ function image_operations_rebuild($nids)
 }
 
 /**
- * TODO: document me...
- */
-function image_node_form_submit($form, &$form_state) {
-  // We need to be aware that a user may try to edit multiple image nodes at
-  // once. By using the $nid variable each node's files can be stored separately
-  // in the session.
-  $nid = $form_state['values']['nid'] ? $form_state['values']['nid'] : 'new_node';
-  // When you enter the edit view the first time we need to clear our files in
-  // session for this node. This is so if you upload a file, then decide you
-  // don't want it and reload the form (without posting), the files will be
-  // discarded.
-  if (count($_POST) == 0) {
-    unset($_SESSION['image_new_files'][$nid]);
-  }
-  
-  // Validators for file_save_upload().
-  $validators = array(
-    'file_validate_is_image' => array(),
-  );
-
-  if ($file = file_save_upload('image', $validators)) {
-    // Resize the original.
-    $image_info = image_get_info($file->filepath);
-    $aspect_ratio = $image_info['height'] / $image_info['width'];
-    $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
-    if (!empty($original_size['width']) && !empty($original_size['height'])) {
-      $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
-      if ($result) {
-        clearstatcache();
-        $file->filesize = filesize($file->filepath);
-        drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height'])));
-      }
-    }
-
-    // Check the file size limit.
-    if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) {
-      form_set_error('image', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize))));
-      file_delete($file->filepath);
-      return;
-    }
-
-    // We're good to go.
-    $form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath;
-    $form_state['values']['rebuild_images'] = FALSE;
-    $form_state['values']['new_file'] = TRUE;
-
-    // Call hook to allow other modules to modify the original image.
-    module_invoke_all('image_alter', $form_state['values'], $file->filepath, IMAGE_ORIGINAL);
-    $form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE);
-
-    // Store the new file into the session.
-    $_SESSION['image_new_files'][$nid] = $form_state['values']['images'];
-  }
-  // Reload new files uploaded in a previous preview.
-  else if (isset($_SESSION['image_new_files'][$nid])) {
-    $form_state['values']['images'] = $_SESSION['image_new_files'][$nid];
-  }
-}
-
-/**
  * Implementation of hook_file_download().
  *
  * Note that in Drupal 5, the upload.module's hook_file_download() checks its
@@ -343,12 +283,12 @@ function image_form_add_thumbnail($form,
 /**
  * Implementation of hook_form
  */
-function image_form(&$node, &$param) {
+function image_form(&$node, $form_state) {
   _image_check_settings();
 
   $type = node_get_types('type', $node);
 
-  $form['#submit'][] = 'image_node_form_submit';
+  $form['#validate'][] = 'image_form_validate';
 
   $form['title'] = array(
     '#type' => 'textfield',
@@ -399,21 +339,44 @@ function image_form(&$node, &$param) {
   return $form;
 }
 
-function image_validate($node) {
-  $nid = ($node->nid) ? $node->nid : 'new_node';
-  if (!isset($node->images[IMAGE_ORIGINAL]) && !isset($_SESSION['image_new_files'][$nid])) {
-    form_set_error('image', t('You must upload an image.'));
-  }
-}
+function image_form_validate($form, &$form_state) {
+  $validators = array('file_validate_is_image' => array());
+  // New image uploads need to be saved in images/temp to be viewable
+  // in the node preview.
+  $dest = file_create_path(variable_get('image_default_path', 'images') .'/temp');
+
+  if ($file = file_save_upload('image', $validators, $dest)) {
+    // Resize the original.
+    $image_info = image_get_info($file->filepath);
+    $aspect_ratio = $image_info['height'] / $image_info['width'];
+    $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
+    if (!empty($original_size['width']) && !empty($original_size['height'])) {
+      $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
+      if ($result) {
+        clearstatcache();
+        $file->filesize = filesize($file->filepath);
+        drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height'])));
+      }
+    }
+
+    // Check the file size limit.
+    if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) {
+      form_set_error('image', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize))));
+      file_delete($file->filepath);
+      return;
+    }
 
-function image_submit($node) {
-dsm("image_submit...");
-  $nid = ($node->nid) ? $node->nid : 'new_node';
-  if (isset($_SESSION['image_new_files'][$nid])) {
-    $node->new_file = TRUE;
-    $node->rebuild_images = FALSE;
-    $node->images = $_SESSION['image_new_files'][$nid];
-    unset($_SESSION['image_new_files'][$nid]);
+    // We're good to go.
+    $form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath;
+    $form_state['values']['rebuild_images'] = FALSE;
+    $form_state['values']['new_file'] = TRUE;
+
+    // Call hook to allow other modules to modify the original image.
+    module_invoke_all('image_alter', $form_state['values'], $form_state['values']['images'][IMAGE_ORIGINAL], IMAGE_ORIGINAL);
+    $form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE);
+  }
+  elseif (empty($form_state['values']['images'])) {
+    form_set_error('image', t('You need to upload an image.'));
   }
 }
 
