### Eclipse Workspace Patch 1.0
#P drupal5
Index: sites/all/modules/imagefield/imagefield.css
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.css,v
retrieving revision 1.2
diff -u -r1.2 imagefield.css
--- sites/all/modules/imagefield/imagefield.css	1 Dec 2006 05:15:57 -0000	1.2
+++ sites/all/modules/imagefield/imagefield.css	25 Jan 2007 21:35:20 -0000
@@ -4,12 +4,18 @@
   margin-bottom: 10px;
 }
 
+div.imagefield-edit-image-row div.form-item {
+  margin: 0;
+}
+
 div.imagefield-edit-preview img {
   border: 1px solid #e5e5e5;
 }
 
-div.imagefield-edit-preview  {
+div.imagefield-edit-left  {
   float: left;
+  width: 150px;
+  padding: 4px 10px;
 }
 
 div.imagefield-edit-image-replace { 
@@ -20,13 +26,13 @@
   width: 100%;
 }
 
+div.imagefield-edit-image-flags {
+  text-align: right;
+}
+
 div.imagefield-edit-image-detail {
   margin-left: 180px;
   margin-right: 20px;
 }
 
-div.imagefield-edit-image-flags div.form-item {
-	float: right;
-}
-
 
Index: sites/all/modules/imagefield/imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.30.2.2
diff -u -r1.30.2.2 imagefield.module
--- sites/all/modules/imagefield/imagefield.module	25 Jan 2007 01:55:12 -0000	1.30.2.2
+++ sites/all/modules/imagefield/imagefield.module	25 Jan 2007 21:35:20 -0000
@@ -174,7 +174,10 @@
     // called before content.module defaults.
     case 'update':
       foreach ($node_field as $delta => $item) {
-        $node_field[$delta] = imagefield_file_update($node, $item, $field); 
+        $node_field[$delta] = imagefield_file_update($node, $item, $field);
+        if (empty($node_field[$delta])) {
+          unset($node_field[$delta]);
+        }
       }
       break;
 
@@ -351,17 +354,40 @@
  * Implementation of hook_widget().
  */
 function imagefield_widget($op, &$node, $field, &$node_field) {
-  $fieldname = $field['field_name'];
-  $content_type = $field['type_name'];
   switch ($op) {
     case 'prepare form values':
-      // clean up the session if we weren't posted.
-      if (!count($_POST)) {
-        imagefield_clear_session();
-      }
+      _imagefield_widget_prepare_form_values($node, $field, $node_field);
+      break;
 
-      // Attach new files 
-      if ($file = file_check_upload($fieldname . '_upload')) {
+    case 'form':
+      return _imagefield_widget_form($node, $field, $node_field);
+      break;
+
+    case 'validate':
+      _imagefield_widget_validate_form_values($node, $field, $node_field);
+      break;
+
+    case 'process form values':
+      _imagefield_widget_process_form_values($node, $field, $node_field);
+      break;
+  }
+}
+
+function _imagefield_widget_prepare_form_values($node, $field, &$node_field) {
+  $fieldname = $field['field_name'];
+  
+  // clean up the session if we weren't posted.
+  if (!count($_POST)) {
+    imagefield_clear_session();
+  }
+
+  // Attach new files
+  if (count($_FILES['files']['name'])) {
+    foreach ($_FILES['files']['name'] as $key => $upload) {
+      $match_found = preg_match('/' . preg_quote($fieldname, '/') . '_([0-9]+)_upload/', $key, $matches);
+      $delta = $matches[1];
+      
+      if ($match_found && $file = file_check_upload($fieldname . '_' . $delta . '_upload')) {
         $file = (array)$file;
         if (strpos($file['filemime'],'image') !== FALSE) { 
           $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
@@ -377,41 +403,19 @@
           
           $file['fid'] = 'upload';
           $file['preview'] = $filepath;
-          
-          // If a single field, mark any other images for deletion and delete files in session
-          if (!$field['multiple']) {
-            if (is_array($node_field)) {
-              foreach ($node_field as $delta => $session_file) {
-                $node_field[$delta]['flags']['delete'] = TRUE;
-              }
-            }
-            imagefield_clear_field_session($fieldname);
-          }
-          // Add the file to the session
-          $file_id = count($node_field) + count($_SESSION['imagefield'][$fieldname]);
-          $_SESSION['imagefield'][$fieldname][$file_id] = $file;
-        }
-      }
-      
-      // Load files from preview state. before committing actions.
-      if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) {
-        foreach($_SESSION['imagefield'][$fieldname] as $delta => $file) {
-          $node_field[] = $file;
-        }
-      }
-      break;
 
-    case 'form':
-      $form = _imagefield_widget_form($node, $field, $node_field);
-      return $form;
-
-    case 'validate':
-      if ($field['required']) {
-        if (!count($node_field)) {
-          form_set_error($fieldname, $field['widget']['label'] .' is required.');
+          // Add the file to the session
+          $_SESSION['imagefield'][$fieldname][$delta] = $file;
         }
       }
-      return;
+    }
+  }
+  
+  // Load all newly uploaded images from the SESSION into $node_field
+  if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) {
+    foreach($_SESSION['imagefield'][$fieldname] as $delta => $file) {
+      $node_field[$delta] = $file;
+    }
   }
 }
 
@@ -423,112 +427,139 @@
   $form[$fieldname] = array(
     '#type' => 'fieldset',
     '#title' => t($field['widget']['label']),
+    '#description' => $field['widget']['description'],
     '#weight' => $field['widget']['weight'],
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
     '#tree' => TRUE,
   );
+
+  // Create a row for each existing image
+  if (is_array($node_field) && count($node_field)) {
+    foreach($node_field as $delta => $file) {
+      if (!$_POST[$fieldname][$delta]['flags']['delete']) {
+        $form[$fieldname][$delta] = _imagefield_edit_image_row_form($field, $delta, $file);
+        $form[$fieldname][$delta]['#theme'] = 'imagefield_edit_image_row';
+      }
+      else {
+        $form[$fieldname][$delta]['flags']['delete'] = array(
+          '#type' => 'hidden',
+          '#value' => 1,
+        );
+      }
       
+      $form[$fieldname][$delta]['filename'] = array('#type' => 'value',  '#value' => $file['filename']);
+      $form[$fieldname][$delta]['filepath'] = array('#type' => 'value',  '#value' => $file['filepath']);
+      $form[$fieldname][$delta]['filemime'] = array('#type' => 'value',  '#value' => $file['filemime']);
+      $form[$fieldname][$delta]['filesize'] = array('#type' => 'value',  '#value' => $file['filesize']);
+      $form[$fieldname][$delta]['fid'] = array('#type' => 'value',  '#value' => $file['fid']);
+    }
+  }
+  
+  // Create the empty field form
+  if ($field['multiple'] || count($node_field) == 0 || $_POST[$fieldname][$delta]['flags']['delete']) {
+    $delta = count($node_field);
+    $form[$fieldname][$delta] = _imagefield_edit_image_row_form($field, $delta);
+    $form[$fieldname][$delta]['#theme'] = 'imagefield_edit_image_row';
+  }
+  
+  $form[$fieldname]['update'] = array(
+    '#type' => 'button',
+    '#value' => ($field['multiple'] && count($node_field) > 0) ? t('Update Images') : t('Update Image'),
+  );
+  
+  return $form;
+}
+
+function _imagefield_edit_image_row_form($field, $delta, $file = array()) {
+  $form = array();
+  if ($file['filepath']) {
+    $form['flags']['delete'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Delete'),
+      '#default_value' => 0,
+    );
+
+    $filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];
+    
+    $form['preview'] = array(
+      '#type' => 'markup',
+      '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE),
+    );
+  }
+  
   // Seperate from tree becase of that silly things won't be 
   // displayed if they are a child of '#type' = form issue
-  $form[$fieldname][$fieldname .'_upload'] = array(
+  $form['file'][$field['field_name'] . '_' . $delta .'_upload'] = array(
     '#type'  => 'file',
-    '#description' => $field['widget']['description'],
+    '#title' => t('File'),
+    '#description' => (!$field['multiple'] || $file['filepath']) ? t('If a new image is chosen, the current image will be replaced upon submitting the form.') : t('Click &quot;Browse...&quot; to select an image from you computer.'),
     '#tree' => FALSE,
-    '#weight' => 9,
-  );
-
-  $form[$fieldname]['upload'] = array(
-    '#type' => 'button',
-    '#value' => t('Upload'),
-    '#name' => 'cck_imagefield_'.$fieldname.'_op',
-    '#attributes' => array('id' => $fieldname.'-attach-button'),
-    '#tree' => FALSE,
-    '#weight' => 10,
   );
+  
+  if (isset($filename)) {
+    $form['file'][$field['field_name'] . '_' . $delta .'_upload']['#description'] = file_create_url($filename) . '<br />' . $form['file'][$field['field_name'] . '_' . $delta .'_upload']['#description'];
+  }
+  
+  if ($field['widget']['custom_alt']) {
+    $form['alt'] = array(
+      '#type' => 'textfield',
+      '#title' =>  t('Alternate text'),
+      '#default_value' => $file['alt'],
+      '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
+      '#maxlength' => 255,
+      '#size' => 10,
+    );
+  }
+  else {
+    $form['alt'] = array(
+      '#type' => 'hidden',
+      '#value' => $file['filename'],
+    );
+  }
+  
+  if ($field['widget']['custom_title']) {
+    $form['title'] = array(
+      '#type' => 'textfield',
+      '#title' =>  t('Title'),
+      '#default_value' =>  $file['title'],
+      '#description' => t('Text to be displayed on mouse overs.'),
+      '#maxlength' => 255,
+      '#size' => 10,
+    );
+  }
+  else {
+    $form['title'] = array(
+      '#type' => 'hidden',
+      '#value' => $file['filename'],
+    );
+  }
+    
+  return $form;
+}
 
-  // Store the file data object to be carried on.
-  if (is_array($node_field) && count($node_field)) {
-    foreach($node_field as $delta => $file) {
-      if ($file['filepath'] && !$file['flags']['delete']) {
-        $form[$fieldname][$delta] = array (
-          '#theme' => 'imagefield_edit_image_row',
-        );
 
-        $form[$fieldname][$delta]['flags']['delete'] = array(
-          '#type' => 'checkbox',
-          '#title' => t('Delete'),
-          '#default_value' => 0,
-        );
-        
-        $filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];
-        
-        $form[$fieldname][$delta]['preview'] = array(
-          '#type' => 'markup',
-          '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE),
-        );
-        
-      //drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');
-        $form[$fieldname][$delta]['description'] = array(
-          '#type' => 'markup',
-          '#value' => '<strong>' . t('Filename: ') . '</strong>' . $file['filename'],
-        );
+function _imagefield_widget_validate_form_values($node, $field, &$node_field) {
+  if ($field['required']) {
+    if (!count($node_field)) {
+      form_set_error($field['field_name'], $field['widget']['label'] .' is required.');
+    }
+  }
+}
 
-        $form[$fieldname][$delta]['alt'] = array(
-          '#type' => 'hidden',
-          '#value' => $file['filename'],
-        );
-        // overwrite with an input field if custom_alt is flagged;
-        if ($field['widget']['custom_alt']) {
-          $form[$fieldname][$delta]['alt'] = array(
-            '#type' => 'textfield',
-            '#title' =>  t('Alternate text'),
-            '#default_value' => $file['alt'],
-            '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
-            '#maxlength' => 255,
-            '#size' => 10,
-          );
-        }
-        
-        $form[$fieldname][$delta]['title'] = array(
-          '#type' => 'hidden',
-          '#value' => $file['filename'],
-        );
-        // overwrite with an input field if custom_title is flagged;
-        if ($field['widget']['custom_title']) {
-          $form[$fieldname][$delta]['title'] = array(
-            '#type' => 'textfield',
-            '#title' =>  t('Title'),
-            '#default_value' =>  $file['title'],
-            '#description' => t('Text to be displayed on mouse overs.'),
-            '#maxlength' => 255,
-            '#size' => 10,
-          );
-        }
-        
-        $form[$fieldname][$delta]['filename'] = array('#type' => 'value',  '#value' => $file['filename']);
-        $form[$fieldname][$delta]['filepath'] = array('#type' => 'value',  '#value' => $file['filepath']);
-        $form[$fieldname][$delta]['filemime'] = array('#type' => 'value',  '#value' => $file['filemime']);
-        $form[$fieldname][$delta]['filesize'] = array('#type' => 'value',  '#value' => $file['filesize']);
-        $form[$fieldname][$delta]['fid'] = array('#type' => 'value',  '#value' => $file['fid']);
-      
-        // Special handling for single value fields
-        if (!$field['multiple']) {
-          $form[$fieldname][$delta]['replace'] = array(
-            '#type' => 'markup',
-            '#value' => t('If a new image is chosen, the current image will be replaced upon submitting the form.'),
-          );
-        }
-      }
-      elseif ($file['filepath'] && $file['flags']['delete']) {
-        $form[$fieldname][$delta]['flags']['delete'] = array(
-          '#type' => 'hidden', // A value type will not persist here, must be hidden
-          '#value' => $file['flags']['delete'],
-        );
+function _imagefield_widget_process_form_values($node, $field, &$node_field) {
+  foreach ($node_field as $delta => $file) {
+    // Do not save empty fields
+    if (is_numeric($delta)) {
+      if (empty($file['filepath'])) {
+        unset($node_field[$delta]);
       }
     }
+    // Remove non-image fields (i.e. the update button)
+    else {
+      unset($node_field[$delta]);
+    }
   }
-  return $form;
 }
 
 /**
@@ -593,19 +624,18 @@
 
 
 function theme_imagefield_edit_image_row($element) {
-  $output = '<div class="imagefield-edit-preview">'. drupal_render($element['preview']) .'</div>';
-  $output .= '<div class="imagefield-edit-image-detail">';
-  $output .= '<div class="imagefield-edit-image-flags">'. drupal_render($element['flags']) .'</div>';
-  $output .= '<div class="imagefield-edit-image-description">'. drupal_render($element['description']);
+  $output = '';
+  $output .= '<div class="imagefield-edit-left">';
+  $output .= '<div class="imagefield-edit-preview">' . drupal_render($element['preview']) . '</div>';
+  $output .= '<div class="imagefield-edit-image-flags">' . drupal_render($element['flags']) . '</div>';
   $output .= '</div>';
+  $output .= '<div class="imagefield-edit-image-detail">';
+  $output .= '<div class="imagefield-edit-image-description">'. drupal_render($element['description']) . '</div>';
+  $output .= drupal_render($element['file']);
   $output .= drupal_render($element['alt']);
   $output .= drupal_render($element['title']);
-  $output .= '</div>';
-  //$output .= '<div class="imagefield-edit-image-fid">'. $element['fid']['#value'] .'</div>';
+  $output .= '</div>';  
   $output = '<div class="imagefield-edit-image-row clear-block">'. $output .'</div>';
-  if (isset($element['replace'])) {
-    $output .= '<div class="imagefield-edit-image-replace">'. drupal_render($element['replace']) .'</div>';
-  }
   return $output;  
 }
 
