--- imagefield/imagefield.module	2006-11-15 18:56:55.000000000 +0100
+++ myimagefield/imagefield.module	2007-02-19 12:58:29.000000000 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: imagefield.module,v 1.9.2.10 2006/11/15 17:56:55 dopry Exp $
+// $Id: imagefield.module,v 1.10 2006/09/01 20:19:56 dopry Exp $
 
 /**
  * @file
@@ -115,11 +115,15 @@ function imagefield_field_settings($op, 
 function imagefield_file_insert($node, &$file, $field) {
   $fieldname = $field['field_name'];
   if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
+    list($width, $height) = explode('x', str_replace('X', 'x', variable_get('thumbnail_resolution','120x120')));
+    if(!image_scale($file->filepath, $file->thumbnailpath, $width, $height)) {
+      form_set_error(NULL,t('There was a problem creating the thumbnail'));
+    }
     $file = (array)$file;
     $file['fid'] = db_next_id('{files}_fid');
-    db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)
-             VALUES (%d, %d, '%s','%s','%s',%d)",
-            $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
+    db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
+             VALUES (%d, %d,'%s','%s','%s',%d)",
+	     $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
     return (array)$file;
   }
   else {
@@ -139,9 +143,7 @@ function imagefield_file_insert($node, &
 function imagefield_file_update($node, &$file, $field) {
   $file = (array)$file; 
   if ($file['flags']['delete'] == TRUE) {
-    if(_imagefield_file_delete($file, $field['field_name'])) {
-      return array();
-    }
+    return _imagefield_file_delete($file, $field['field_name']);
   }
   if ($file['fid'] == 'upload') {  
     return imagefield_file_insert($node, $file, $field);
@@ -156,7 +158,7 @@ function imagefield_file_update($node, &
 /**
  * Implementation of hook_field().
  */
-function imagefield_field($op, $node, $field, &$node_field, $teaser, $page) {
+function imagefield_field($op, $node, $field, &$node_field, $a1, $a2) {
   $fieldname = $field['field_name'];
   switch ($op) {
     // called after content.module loads default data.
@@ -167,6 +169,7 @@ function imagefield_field($op, $node, $f
         foreach ($node_field as $delta => $file) {
           $values[$delta]  = array_merge((array)$node_field[$delta], _imagefield_file_load($file['fid']));
           $node_field[$delta] = $values[$delta];
+	  $values[$delta]['thumbnailpath'] = file_directory_path() . '/' . _thumbnailname($values[$delta]['filename']);
         }
         $output = array($fieldname => $values);
       };
@@ -177,8 +180,10 @@ function imagefield_field($op, $node, $f
       foreach ($node_field as $delta => $item) {
         // do not show images flagged for deletion in preview.
         if (!$item['flags']['delete'] == 1) {
-          $node_field[$delta]['view'] = content_format($field, $item, ($page ? 'default' : 'thumb')); 
+          $node_field[$delta]['view'] = theme('imagefield_image', $item); 
+	  $node_field[$delta]['thumbnailpath'] = file_directory_path() . '/' . _thumbnailname($node_field[$delta]['filename']);
           $images[] =  $node_field[$delta]['view'];
+	  $images[] =  $node_field[$delta]['thumbnailpath'];
         }
       }
       $view = theme('imagefield_multiple', $images);
@@ -237,11 +242,20 @@ function imagefield_widget_settings($op,
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+      $form['thumbnail_resolution'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum resolution for thumbnails'), 
+        '#default_value' => $widget['thumbnail_resolution'] ? $widget['thumbnail_resolution'] : '120x120',
+        '#size' => 15, 
+        '#maxlength' => 10, 
+        '#description' => 
+        t('The maximum thumbnails size resolution expressed as WIDTHxHEIGHT (e.g. 100x50). Default: 120x120')
+      );
       return $form;
     case 'validate':
       break;
     case 'save':
-      return array('max_resolution');
+      return array('max_resolution', 'thumbnail_resolution');
   }
 }
 
@@ -277,6 +291,7 @@ function imagefield_clear_field_session(
       foreach ($files as $delta => $file) {
         if (is_file($file['filepath'])) {
           file_delete($file['filepath']);
+	  file_delete($file['thumbnailpath']);
         }
       }
     }
@@ -291,7 +306,7 @@ function _imagefield_file_delete($file, 
   else {
     unset($_SESSION['imagefield'][$fieldname][$file['sessionid']]);
   }
-  return file_delete($file['filepath']);
+  return (file_delete($file['filepath']) && file_delete($file['thumbnailpath']));
 }
 
 /**
@@ -305,69 +320,50 @@ function imagefield_widget($op, $node, $
       if (!count($_POST)) {
         imagefield_clear_session();
       }
-
       // Attach new files 
       if ($file = file_check_upload($fieldname . '_upload')) {
         $file = (array)$file;
-        if (strpos($file['filemime'],'image') !== FALSE) { 
+        if (strpos($file['filemime'], 'image') !== FALSE) { 
           $file = _imagefield_scale_image($file, $field['widget']['max_resolution']); 
         }
         else {
-          return FALSE;
+          $return = FALSE;
         }
         $file['fid'] = 'upload';
         if (!$field['multiple']) {
           // Remove old temporary file from session.
           imagefield_clear_field_session($fieldname);
         }
-        //drupal_set_message('<pre>'. print_r($file, true) .'</pre>');
         $file['sessionid'] = count($_SESSION['imagefield'][$fieldname]);
         $_SESSION['imagefield'][$fieldname][$file['sessionid']] = $file;
       }
-      
-      // Load files from preview state. before committing actions.
-      /* Don't do this until we hear whether cck is going to support this...
-         although it is really needed if anyother module wants to modify imagefields.
-
-      if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) {
-        foreach($_SESSION['imagefield'][$fieldname] as $delta => $file) {
-          $node_field[] = $file;
-        }
-      }
-
-      */
-      //drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');
       break;
-
+      
     case 'form':
-      //drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');
-      /* This is hack since content.module isn't returning data added to node_field in hook_widget op=prepare form values.
-         its really annoying with new imagefields that don't already have data in them.  
-      */
       if (is_array($_SESSION['imagefield'][$fieldname]) && count($_SESSION['imagefield'][$fieldname])) {
         foreach($_SESSION['imagefield'][$fieldname] as $delta => $file) {
           $node_field[] = $file;
         }
       } 
-
       $form = _imagefield_widget_form($node, $field, $node_field);
-      return $form;
-
+      $return = $form;
+      break;
     case 'validate':
       if ($field['required']) {
         if (!count($node_field)) {
           form_set_error($fieldname, $field['widget']['label'] .' is required.');
-
         }
       }
-      return;
+      $return = "";
+      break;
   }
+  return $return;
 }
 
 function _imagefield_widget_form($node, $field, &$node_field) {
   $fieldname = $field['field_name'];
   theme_add_style(drupal_get_path('module', 'imagefield') .'/imagefield.css');
- 
+  
   $form = array(); 
   $form[$fieldname] = array(
     '#type' => 'fieldset',
@@ -376,28 +372,29 @@ function _imagefield_widget_form($node, 
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
     '#tree' => TRUE,
-  );
-      
-  // 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(
-    '#type'  => 'file',
-    '#description' => $field['widget']['description'],
-    '#tree' => FALSE,
-    '#weight' => 9,
-  );
-
-  $form[$fieldname]['Update'] = array(
-    '#type' => 'button',
-    '#value' => t('Update'),
-    '#name' => 'cck_imagefield_'.$fieldname.'_op',
-    '#attributes' => array('id' => $fieldname.'-attach-button'),
-    '#tree' => FALSE,
-    '#weight' => 10,
-  );
-
+    );
+  // If node has no image at all
+  if (!is_array($node_field) || !count($node_field) || ($node_field[0][fid]==0 && !count($_SESSION['imagefield'][$fieldname]))) {      
+    // Separate from tree becase of that silly things won't be 
+    // displayed if they are a child of '#type' = form issue
+    $description = $field['widget']['description'];
+    $form[$fieldname][$fieldname .'_upload'] = array(
+      '#type'  => 'file',
+      '#description' => $description ? $description : t('Attach an image'),
+      '#tree' => FALSE,
+      '#weight' => 9,
+      );
+    $form[$fieldname]['Update'] = array(
+      '#type' => 'button',
+      '#value' => t('Update'),
+      '#name' => 'cck_imagefield_'.$fieldname.'_op',
+      '#attributes' => array('id' => $fieldname.'-attach-button'),
+      '#tree' => FALSE,
+      '#weight' => 10,
+      );
+  }
   // Store the file data object to be carried on.
-  if (is_array($node_field) && count($node_field)) {
+  else{
     foreach($node_field as $delta => $file) {
       if ($file['filepath']) {
         $form[$fieldname][$delta] = array (
@@ -408,10 +405,11 @@ function _imagefield_widget_form($node, 
           '#type' => 'checkbox',
           '#title' => t('Delete'),
           '#default_value' => 0,
+	  '#description' => t('If checked, the image will be deleted when you save the node'),
           '#attributes' => array('id' => $fieldname .'-'. $delta.'-delete-checkbox'),
         );
       
-        $form[$fieldname][$delta]['preview'] = array(
+	$form[$fieldname][$delta]['preview'] = array(
           '#type' => 'markup',
           '#value' => theme('imagefield_image_preview', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE),
         );
@@ -420,30 +418,23 @@ function _imagefield_widget_form($node, 
           '#type' => 'markup',
           '#value' => '<p>'. t('filename:') . $file['filename']. '</p>',
         );
-        $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' => 'textfield',
           '#title' =>  t('title'),
           '#default_value' =>  $file['title'],
-          '#description' => t('Text to be displayed on mouse overs.'),
+          '#description' => t('Text to be displayed as the title of the image'),
           '#maxlength' => 255,
           '#size' => 10,
         );
         // must set #tree => true, so data structure will be carried
         // with node into hook_field.... 
         $form[$fieldname][$delta]['filename'] = array('#type' => 'value',  '#value' => $file['filename']);
+	$form[$fieldname][$delta]['thumbnailname'] = array('#type' => 'value',  '#value' => _thumbnailname($file['filename']));
         $form[$fieldname][$delta]['filepath'] = array('#type' => 'value',  '#value' => $file['filepath']);
+	$form[$fieldname][$delta]['thumbnailpath'] = array('#type' => 'value',  '#value' => file_directory_path() . '/'._thumbnailname($file['filename']));
         $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'] && $delta == 0) {
           // Mark [0] for deletion if there is more than 1 image.
@@ -454,68 +445,31 @@ function _imagefield_widget_form($node, 
               '#value' => t('If a new image is chosen, the current image will be replaced upon submitting the form.'),
             );
           }
-        } 
+        }
       }
     }
   }
   return $form;
 }
 
-/**
- * Implementation of hook_field_formatter_info().
- */
-function imagefield_field_formatter_info() {
-  $formatters = array(
-    'default' => array(
-      'label' => 'Default',
-      'field types' => array('image'),
-    ),
-  );
-  if (module_exist('imagecache')) {
-    $rules = _imagecache_get_presets();
-    foreach ($rules as $ruleid => $rulename) {
-       $formatters[$rulename] = array(
-         'label' => $rulename,
-         'field types' => array('image'),
-         );
-    }
-  }
-  return $formatters;
-}
-
-/**
- * Implementation of hook_field_formatter().
- *
- */
-function imagefield_field_formatter($field, $item, $formatter) {
-  if (!isset($item['fid'])) {
-    return '';
-  }
-  
-  $file = _imagefield_file_load($item['fid']);
-  if (module_exist('imagecache')) {
-    $rules = _imagecache_get_presets();
-    if (in_array($formatter, (array) $rules)) {
-      return theme('imagecache', $formatter, $file['filepath']);
-    }
-  }
-  return theme('imagefield_image', $file, $item['alt'], $item['title']);
-
+function imagefield_field_view_item($field, $node_field_item) {
+    $file = _imagefield_file_load($node_field_item['fid']);
+    return theme('imagefield_view_image', $file, $node_field_item['alt'], $node_field_item['title']); 
 }
 
 
 function _imagefield_file_load($fid = NULL) {
-  // Don't bother if we weren't passed and fid.
-  if (isset($fid)) { 
-    // Test to catch fid, eventuall plan to have node_load syntax
-    // once file_attributes table is complete
-    if (is_numeric($fid)) {
-      $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);
-      $file = db_fetch_array($result);
-      return ($file) ? $file : array();
-    }
+  // Exit if we were not passed an fid to load;
+  if (!isset($fid)) { 
+    return false;
+  }
+  // Test to catch fid, eventuall plan to have node_load syntax
+  // once file_attributes table is complete
+  if (is_numeric($fid)) {
+    $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);
+    return db_fetch_array($result);
   }
-  return array();
+  return FALSE;
 }
 
 function theme_imagefield_image_preview($file, $alt ='', $title='', $attributes = NULL, $getsize= TRUE) {
@@ -547,7 +501,6 @@ function theme_imagefield_edit_image_row
 }
 
 function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
-  
   $file = (array)$file;
   if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
     $attributes = drupal_attributes($attributes);
@@ -579,3 +532,7 @@ function theme_imagefield_image($file, $
 function theme_imagefield_multiple($images) {
   return implode("\n", $images);
 }
+
+function _thumbnailname($filename){
+  return preg_replace('/(.+)\.([a-zA-Z]+)/','${1}_thumbnail.${2}',$filename);
+}
\ No newline at end of file
