Index: imagefield.module
===================================================================
--- imagefield.module	(revision 2196)
+++ imagefield.module	(working copy)
@@ -102,9 +102,88 @@
         'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
       );
       return $columns;
+    case 'tables':
+      // Override default Views field handlers provided by CCK
+      $tables = content_views_field_tables($field);
+      $tables['node_data_'. $field['field_name']]['fields'][$field['field_name'] .'_fid']['handler'] = array(
+        'imagefield_views_field_handler_group' => t('Group multiple values'),
+        'imagefield_views_field_handler_ungroup' => t('Do not group multiple values'),
+      );
+      return $tables;
+      break;      
   }
 }
 
+/**
+ * Custom views table field handler for hook_views_tables().
+ *
+ * Used in place of the default Views field handlers provided by CCK to display
+ * the default image when no other images have been uploaded to a particular
+ * node.
+ */
+function imagefield_views_field_handler_group($field_info, $field_data, $value, $data) {
+  $field = $field_info['content_field'];
+  $items = array();
+
+  if ($field['multiple']) {
+    foreach ($field_info['content_db_info']['columns'] as $column => $attributes) {
+      $query_columns[] = "node_data_$field[field_name].$attributes[column] AS $column";
+    }
+    $query = "SELECT ". implode(', ', $query_columns) .
+             " FROM {node} node".
+             " LEFT JOIN {". $field_info['content_db_info']['table'] ."} node_data_$field[field_name] ON node.vid = node_data_$field[field_name].vid".
+             " WHERE node.nid = ". $data->nid .
+             " ORDER BY node_data_$field[field_name].delta";
+    $result = db_query(db_rewrite_sql($query, 'node'));
+    while ($item = db_fetch_array($result)) {
+      $items[] = content_format($field, $item, $field_data['options'], $data);
+    }
+
+    // if there are no items to display
+    if ($items[0] == '') {
+      $node = node_load(array('nid' => $data->nid));
+      $content_types = content_types();
+      $real_field = &$content_types[$node->type]['fields'][$field['field_name']];
+      if ($real_field['widget']['use_default_image']) { // and default image is enabled
+        $items = array(content_format($field, $real_field['widget']['default_image'] + array('nid' => $data->nid), $field_data['options'], $data)); // return the default image
+      }
+    }
+
+    return theme('content_view_multiple_field', $items, $field, $data);
+  }
+  else {
+    return content_views_field_handler_ungroup($field_info, $field_data, $value, $data);
+  }
+}
+
+/**
+ * Custom views table field handler for hook_views_tables().
+ *
+ * Used in place of the default Views field handlers provided by CCK to display
+ * the default image when no other images have been uploaded to a particular
+ * node.
+ */
+function imagefield_views_field_handler_ungroup($field_info, $field_data, $value, $data) {
+  $field = $field_info['content_field'];
+  $item = array();
+  foreach ($field_info['content_db_info']['columns'] as $column => $attributes) {
+    $view_column_name = $field_data['tablename'] .'_'. $attributes['column'];
+    $item[$column] = $data->$view_column_name;
+  }
+
+  // if there are no items to display
+  if (empty($item['fid'])) {
+    $node = node_load(array('nid' => $data->nid));
+    $content_types = content_types();
+    $real_field = &$content_types[$node->type]['fields'][$field['field_name']];
+    if ($real_field['widget']['use_default_image']) { // and default image is enabled
+      $item = $real_field['widget']['default_image'] + array('nid' => $data->nid); // return the default image
+    }
+  }
+
+  return content_format($field, $item, $field_data['options'], $data);
+}
+
 function imagefield_default_item() {
   return array(
     'fid' => 0,
@@ -184,17 +263,7 @@
       $context = $teaser ? 'teaser' : 'full';
       $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
       if (!count($items) && $field['widget']['use_default_image']) {
-        // build a fake items array
-        $items = array(array(
-          'fid' => 0,
-          'title' => $field['widget']['default_image']['filename'],
-          'alt' => $field['widget']['default_image']['filename'],
-          'nid' => $node->nid,
-          'filename' => $field['widget']['default_image']['filename'],
-          'filepath' => $field['widget']['default_image']['filepath'],
-          'filemime' => $field['widget']['default_image']['filemime'],
-          'filesize' => $field['widget']['default_image']['filesize'], 
-        ));
+        $items = array($field['widget']['default_image']); // return default image
       }
       foreach ($items as $delta => $item) {
         $items[$delta]['view'] = content_format($field, $item, $formatter, $node);
@@ -221,6 +290,10 @@
             }
           }
         }
+      } else if ($field['widget']['use_default_image']) {
+        $items = array($field['widget']['default_image']); // return default image
+      }
+      if (count($items)) {
         $items = array_values($items); // compact deltas
         return array($fieldname => $items);
       }
@@ -380,7 +453,11 @@
 
       // We save the upload here because we can't know the correct
       // file path until we save the file.
-      if ($file = file_check_upload('default_image_upload')) {
+      if ($file = file_check_upload('default_image_upload')) {        
+        if (isset($widget['default_image'])) {
+          _imagefield_file_delete($widget['default_image'], $widget['field_name']); // delete previous default image
+        }
+        
         if (function_exists('token_replace')) {
           global $user;
           $widget_image_path = token_replace($widget['image_path'],'user', $user);
@@ -390,9 +467,13 @@
         }
  
         $filepath = file_create_path($widget_image_path);
-        if ($file = file_save_upload('default_image_upload', $filepath)) {
-          form_set_value(array('#parents' => array('default_image')), (array) $file);
-        }
+        $node = (object) array('nid' => NULL);
+        $file = (array) $file;
+        $file = imagefield_file_insert($node, $file, $widget);
+
+        form_set_value(array('#parents' => array('default_image')), $file);
+      } else if (!$widget['use_default_image'] && isset($widget['default_image'])) {
+        _imagefield_file_delete($widget['default_image'], $widget['field_name']); // delete previous default image
       }
       break;
 
