Index: image/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/image.module	17 Aug 2008 08:35:36 -0000	1.276
+++ image/image.module	22 Aug 2008 09:40:19 -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,13 +283,17 @@ function image_form_add_thumbnail($form,
 /**
  * Implementation of hook_form
  */
-function image_form(&$node, &$param) {
+function image_form(&$node, $form_state) {
+
+  // User has started new iteration of Image node creation so clear old session data.
+  if (!property_exists($node, 'nid')) {
+    unset($_SESSION['image_new_files']);
+  }
+
   _image_check_settings();
 
   $type = node_get_types('type', $node);
 
-  $form['#submit'][] = 'image_node_form_submit';
-
   $form['title'] = array(
     '#type' => 'textfield',
     '#title' => check_plain($type->title_label),
@@ -396,24 +340,85 @@ function image_form(&$node, &$param) {
     $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
   }
 
+  $form['#validate'][] = '_image_upload_validate';
+
   return $form;
 }
 
-function image_validate($node) {
+function image_validate($node, &$form) {
   $nid = ($node->nid) ? $node->nid : 'new_node';
-  if (!isset($node->images[IMAGE_ORIGINAL]) && !isset($_SESSION['image_new_files'][$nid])) {
+  if (empty($node->images[IMAGE_ORIGINAL]) && !isset($_SESSION['image_new_files'][$nid])) {
     form_set_error('image', t('You must upload an image.'));
   }
 }
 
-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]);
+/**
+ * Upload validation as callback
+ *
+ * Validates upload even if user has left empty title field.
+ * form_set_error() calls here will actually work correctly with the upload field
+ * highlighted, which did not appear to work with image_node_form_submit().
+ * Populates upload-related items in $form_state.
+ *
+ * @param $form
+ *   An associative array containing the structure of the form.
+ * @param $form_state
+ *   A keyed array containing the current state of the form. This
+ *   includes the current persistent storage data for the form, and
+ *   any data passed along by earlier steps when displaying a
+ *   multi-step form. Additional information, like the sanitized $_POST
+ *   data, is also accumulated here.
+ */
+function _image_upload_validate($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';
+
+  // Validators for file_save_upload().
+  $validators = array(
+    'file_validate_is_image' => array(),
+  );
+
+  // Capture potential upload to Image temp directory
+  $temp_path = file_create_path(file_directory_path() .'/'. variable_get('image_default_path', 'images')).'/temp';
+
+  if ($file = file_save_upload('image', $validators, $temp_path)) {
+    // 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.- will leave unused row on {files} if file deleted?
+    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 from last image_load or upload.
+  else if (isset($_SESSION['image_new_files'][$nid])) {
+    $form_state['values']['images'] = $_SESSION['image_new_files'][$nid];
   }
 }
 
@@ -499,6 +504,14 @@ function image_load(&$node) {
     image_update($node);
     watchdog('image', 'Derivative images were regenerated for %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
   }
+
+  // If starting a new edit on an existing Image node, reload the session array with
+  // the node's current file details. 
+  preg_match('|node/(\d+)/edit|', $_GET['q'], $parseURL);
+  $URLnode = $parseURL[1];
+  if (count($_POST) == 0 && $URLnode == $node->nid) {
+    $_SESSION['image_new_files'][$node->nid] = $node->images;
+  }
 }
 
 /**
@@ -521,6 +534,14 @@ function image_insert($node) {
       _image_insert($node, $key, $node->images[$key]);
     }
   }
+
+  // Tidy up SESSION - moved here from deprecated image_submit().
+  if (isset($_SESSION['image_new_files']['new_node'])) {
+    unset($_SESSION['image_new_files']['new_node']);
+  }
+  if (isset($_SESSION['image_new_files'][$node->nid])) {
+    unset($_SESSION['image_new_files'][$node->nid]);
+  }
 }
 
 /**
@@ -538,7 +559,7 @@ function image_update(&$node) {
     $original_path = $node->images[IMAGE_ORIGINAL];
 
     if (!empty($node->new_file)) {
-      // The derivative images were built during image_prepare() or 
+      // The derivative images were built during _image_upload_validate() or 
       // image_create_node_from() so all we need to do is remove all the old,
       // existing images.
 
@@ -588,6 +609,14 @@ function image_update(&$node) {
       }
     }
   }
+
+  // Tidy up SESSION - moved here from deprecated image_submit()
+  if (isset($_SESSION['image_new_files']['new_node'])) {
+    unset($_SESSION['image_new_files']['new_node']);
+  }
+  if (isset($_SESSION['image_new_files'][$node->nid])) {
+    unset($_SESSION['image_new_files'][$node->nid]);
+  }
 }
 
 /**
