Index: imagefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.9.2.10
diff -u -r1.9.2.10 imagefield.module
--- imagefield.module	15 Nov 2006 17:56:55 -0000	1.9.2.10
+++ imagefield.module	25 Nov 2006 05:33:03 -0000
@@ -24,31 +24,32 @@
 
 function imagefield_menu($maycache) {
   $items = array();
-  if ($maycache) {
-
-  } 
-  else {
+  if (!$maycache) {
     // Add handlers for previewing new uploads.
     if ($_SESSION['imagefield']) {
-      foreach ($_SESSION['imagefield'] as $fieldname => $files) {
-        foreach($files as $delta => $file) {
-          $filename = file_create_filename($file['filename'], file_create_path());
-          // strip file_directory_path() from private downloads path. @see file_create_url
-          if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) ==  FILE_DOWNLOADS_PRIVATE) {
-            if (strpos($filename, file_directory_path()) !== FALSE) {
-              $filename = trim(substr($filename, strlen(file_directory_path())), '\\/');
+      foreach ($_SESSION['imagefield'] as $fieldname => $content_type) {
+        foreach ($content_type as $content_type => $files) {
+          $field = content_fields($fieldname, $content_type);
+          if (is_array($files)) {
+            foreach($files as $delta => $file) {
+              $filename = file_create_filename($file['filename'], file_create_path($field['widget']['image_path']));
+              // strip file_directory_path() from private downloads path. @see file_create_url
+              if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) ==  FILE_DOWNLOADS_PRIVATE) {
+                if (strpos($filename, file_directory_path()) !== FALSE) {
+                  $filename = trim(substr($filename, strlen(file_directory_path())), '\\/');
+                }
+                $filename = 'system/files/' . $filename;
+              }
+              $items[] = array(
+                'path' => $filename,
+                'callback' => '_imagefield_preview',
+                'access' => TRUE,
+                'type' => MENU_CALLBACK,
+              );
+              $file['preview'] = $filename;
+              $_SESSION['imagefield'][$fieldname][$content_type][$delta] = $file;
             }
-            $filename = 'system/files/' . $filename;
           }
-          $items[] = array(
-            'path' => $filename, 'title' => t('file download'),
-            'callback' => '_imagefield_preview',
-            'access' => TRUE,
-            'type' => MENU_CALLBACK
-          );
-          $file['preview'] = $filename;
-          $_SESSION['imagefield'][$fieldname][$delta] = $file;        
-          //drupal_set_message($filename);
         }
       }
     }
@@ -61,11 +62,11 @@
  *  @todo  multiple support
  */
 function _imagefield_preview() {
-  foreach ($_SESSION['imagefield'] as $fieldname => $files) {
+  foreach ($_SESSION['imagefield'] as $fieldname => $content_type) {
+    foreach ($content_type as $content_type => $files)
     foreach ($files as $delta => $file) {
       if ($file['preview'] == $_GET['q']) {
-        file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']),
-                                           'Content-Length: '. $file['filesize']));
+        file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']), 'Content-Length: '. $file['filesize']));
         exit();
       }
     }
@@ -114,7 +115,9 @@
  */
 function imagefield_file_insert($node, &$file, $field) {
   $fieldname = $field['field_name'];
-  if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
+  $filepath = file_create_path($field['widget']['image_path']) . '/' . $file['filename'];
+
+  if ($file = file_save_upload((object)$file, $filepath)) {
     $file = (array)$file;
     $file['fid'] = db_next_id('{files}_fid');
     db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)
@@ -139,7 +142,7 @@
 function imagefield_file_update($node, &$file, $field) {
   $file = (array)$file; 
   if ($file['flags']['delete'] == TRUE) {
-    if(_imagefield_file_delete($file, $field['field_name'])) {
+    if(_imagefield_file_delete($file, $field['field_name'], $field['type_name'])) {
       return array();
     }
   }
@@ -175,11 +178,8 @@
     case 'view':
       $images = array();
       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')); 
-          $images[] =  $node_field[$delta]['view'];
-        }
+        $node_field[$delta]['view'] = content_format($field, $item, 'default'); 
+        $images[] =  $node_field[$delta]['view'];
       }
       $view = theme('imagefield_multiple', $images);
       
@@ -202,7 +202,7 @@
 
     case 'delete':
       foreach ($node_field as $delta => $item) {
-        _imagefield_file_delete($item, $field['field_name']); 
+        _imagefield_file_delete($item, $field['field_name'], $field['type_name']); 
       }
       break;
   }
@@ -237,14 +237,38 @@
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+      $form['image_path'] = array(
+        '#type' => 'textfield', 
+        '#title' => t('Image path'), 
+        '#default_value' => $widget['image_path'] ? $widget['image_path'] : '', 
+        '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => theme('placeholder', variable_get('file_directory_path', 'files')))), 
+        '#after_build' => array('imagefield_check_directory'),
+      );
       return $form;
     case 'validate':
       break;
     case 'save':
-      return array('max_resolution');
+      return array('max_resolution', 'image_path');
   }
 }
 
+/**
+ * Create the image directory relative to the 'files' dir - if user specified one
+ * Won't allow form submit unless the directory exists & is writable
+ * 
+ * @param $form_element
+ *   The form element containing the name of the directory to check.
+ */ 
+function imagefield_check_directory($form_element) {
+  if(!empty($form_element['#value'])) {
+    // create each directory necessary if it doesn't exist
+    foreach(explode('/', $form_element['#value']) as $dir) {
+      $dirs[] = $dir;
+      file_check_directory(file_create_path(implode($dirs,'/')), FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
+    }
+  }
+  return $form_element;
+}
 
 function _imagefield_scale_image($file, $resolution = 0) {
   $info = image_get_info($file['filepath']);
@@ -265,31 +289,35 @@
 function imagefield_clear_session() {
   if (is_array($_SESSION['imagefield']) && count($_SESSION['imagefield'])) {
     foreach (array_keys($_SESSION['imagefield']) as $fieldname) {
-      imagefield_clear_field_session($fieldname);
+      if (is_array($fieldname) && count($fieldname)) {
+        foreach (array_keys($fieldname) as $content_type) {
+          imagefield_clear_field_session($fieldname, $content_type);
+        }
+      }
     }
     unset($_SESSION['imagefield']);
   }
 }
 
-function imagefield_clear_field_session($fieldname) {
-  if (count($_SESSION['imagefield'][$fieldname])) {
-    foreach ($_SESSION['imagefield'][$fieldname] as $files) {
+function imagefield_clear_field_session($fieldname, $content_type) {
+  if (count($_SESSION['imagefield'][$fieldname][$content_type])) {
+    foreach ($_SESSION['imagefield'][$fieldname][$content_type] as $files) {
       foreach ($files as $delta => $file) {
         if (is_file($file['filepath'])) {
           file_delete($file['filepath']);
         }
       }
     }
-    unset($_SESSION['imagefield'][$fieldname]);
+    unset($_SESSION['imagefield'][$fieldname][$content_type]);
   }
 }
 
-function _imagefield_file_delete($file, $fieldname) {
+function _imagefield_file_delete($file, $fieldname, $content_type) {
   if (is_numeric($file['fid'])) {
     db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']);
   }
   else {
-    unset($_SESSION['imagefield'][$fieldname][$file['sessionid']]);
+    unset($_SESSION['imagefield'][$fieldname][$content_type][$file['sessionid']]);
   }
   return file_delete($file['filepath']);
 }
@@ -297,59 +325,50 @@
 /**
  * Implementation of hook_widget().
  */
-function imagefield_widget($op, $node, $field, &$node_field) {
+function imagefield_widget($op, &$node, $field, &$node_field) {
   $fieldname = $field['field_name'];
+  $content_type = $field['type_name'];
   switch ($op) {
     case 'prepare form values':
+      static $imagefield_upload_processed = FALSE;
+      
       // clean up the session if we weren't posted.
       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) { 
-          $file = _imagefield_scale_image($file, $field['widget']['max_resolution']); 
-        }
-        else {
-          return FALSE;
-        }
-        $file['fid'] = 'upload';
-        if (!$field['multiple']) {
-          // Remove old temporary file from session.
-          imagefield_clear_field_session($fieldname);
+      if ($imagefield_upload_processed == FALSE) {
+        // Attach new files 
+        if ($file = file_check_upload($fieldname . '_upload')) {
+          $file = (array)$file;
+          if (strpos($file['filemime'],'image') !== FALSE) { 
+            $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
+            
+            $file['fid'] = 'upload';
+            
+            if (!$field['multiple']) {
+              // Remove old temporary file from session.
+              imagefield_clear_field_session($fieldname, $content_type);
+              $node_field = array();
+            }
+            
+            $file_id = count($node_field) + count($_SESSION['imagefield'][$fieldname][$content_type]);
+            $_SESSION['imagefield'][$fieldname][$content_type][$file_id] = $file;
+          }
         }
-        //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;
+        
+        // add all new images saved in the session to the node_field
+        if (is_array($_SESSION['imagefield'][$fieldname][$content_type])) {
+          foreach ($_SESSION['imagefield'][$fieldname][$content_type] as $delta => $file) {
+            $node_field[] = $file;
+          }
         }
+        
+        $imagefield_upload_processed = TRUE;
       }
-
-      */
-      //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;
 
@@ -357,7 +376,6 @@
       if ($field['required']) {
         if (!count($node_field)) {
           form_set_error($fieldname, $field['widget']['label'] .' is required.');
-
         }
       }
       return;
@@ -387,9 +405,9 @@
     '#weight' => 9,
   );
 
-  $form[$fieldname]['Update'] = array(
+  $form[$fieldname]['upload'] = array(
     '#type' => 'button',
-    '#value' => t('Update'),
+    '#value' => t('Upload'),
     '#name' => 'cck_imagefield_'.$fieldname.'_op',
     '#attributes' => array('id' => $fieldname.'-attach-button'),
     '#tree' => FALSE,
@@ -410,10 +428,12 @@
           '#default_value' => 0,
           '#attributes' => array('id' => $fieldname .'-'. $delta.'-delete-checkbox'),
         );
-      
+        
+        $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_preview', $file, $file['alt'], $file['title'], array('width' => '150'), FALSE),
+          '#value' => theme('imagefield_image_preview', file_create_url($filename), $file['alt'], $file['title']),
         );
         
         $form[$fieldname][$delta]['description'] = array(
@@ -422,7 +442,7 @@
         );
         $form[$fieldname][$delta]['alt'] = array(
           '#type' => 'textfield',
-          '#title' =>  t('alternate text'),
+          '#title' =>  t('Alternate Text'),
           '#default_value' => $file['alt'],
           '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
           '#maxlength' => 255,
@@ -430,7 +450,7 @@
         );
         $form[$fieldname][$delta]['title'] = array(
           '#type' => 'textfield',
-          '#title' =>  t('title'),
+          '#title' =>  t('Title'),
           '#default_value' =>  $file['title'],
           '#description' => t('Text to be displayed on mouse overs.'),
           '#maxlength' => 255,
@@ -446,15 +466,11 @@
       
         // Special handling for single value fields
         if (!$field['multiple'] && $delta == 0) {
-          // Mark [0] for deletion if there is more than 1 image.
-          if (count($_SESSION['imagefield'][$fieldname]) > 1) {
-            $form[$fieldname][$delta]['flags']['delete']['#value'] = 1;
-            $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.'),
-            );
-          }
-        } 
+          $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.'),
+          );
+        }
       }
     }
   }
@@ -499,11 +515,10 @@
       return theme('imagecache', $formatter, $file['filepath']);
     }
   }
-  return theme('imagefield_image', $file, $item['alt'], $item['title']);
+  return theme('imagefield_image', file_create_url($file['filepath']), $item['alt'], $item['title']);
 
 }
 
-
 function _imagefield_file_load($fid = NULL) {
   // Don't bother if we weren't passed and fid.
   if (isset($fid)) { 
@@ -518,55 +533,51 @@
   return array();
 }
 
-function theme_imagefield_image_preview($file, $alt ='', $title='', $attributes = NULL, $getsize= TRUE) {
-  return '<span class="imagefield-image-preview">'. theme('imagefield_image', $file, $alt, $title, $attributes , $getsize) .'</span>';
+function theme_imagefield_image_preview($url, $alt ='', $title='', $attributes = array('width' => 150), $getsize = FALSE) {
+  return '<span class="imagefield-image-preview">'. theme('imagefield_image', $url, $alt, $title, $attributes, $getsize) .'</span>';
 }
 
-function theme_imagefield_view_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
-  return theme('imagefield_image', $file, $alt, $title, $attributes , $getsize);
+function theme_imagefield_view_image($url, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
+  return theme('imagefield_image', $url, $alt, $title, $attributes, $getsize);
 }
 
 
 
 function theme_imagefield_edit_image_row($element) {
   $output = '<div class="imagefield-edit-preview">'. form_render($element['preview']) .'</div>';
+  $output .= '<div class="imagefield-edit-fields">';
   $output .= '<div class="imagefield-edit-image-description">'. form_render($element['description']) .'</div>';
   //$output .= '<div class="imagefield-edit-image-fid">'. $element['fid']['#value'] .'</div>';
+  $output .= '<div class="imagefield-edit-image-alt">'.form_render($element['alt']).'</div>';
+  $output .= '<div class="imagefield-edit-image-title">'.form_render($element['title']).'</div>';
   if (isset($element['replace'])) {
     $output .= '<div class="imagefield-edit-image-replace">'. form_render($element['replace']) .'</div>';
-  } else {
-    $output .= '<div class="imagefield-edit-image-flags">'. form_render($element['flags']) .'</div>';
   }
+  $output .= '<div class="imagefield-edit-image-flags">'. form_render($element['flags']) .'</div>';
+  $output .= '</div>';
   $output .= '<div style="clear:both">&nbsp;</div>';
-  if (!isset($element['replace'])) {
-    $output .= '<div class="imagefield-edit-image-alt">'.form_render($element['alt']).'</div>';
-    $output .= '<div class="imagefield-edit-image-title">'.form_render($element['title']).'</div>';
-  }
   $output = '<div class="imagefield-edit-image-row">'. $output .'</div>';
   return $output;  
 }
 
-function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
+function theme_imagefield_image($url, $alt = '', $title = '', $attributes = array(), $getsize = TRUE) {
+  global $base_url;
+  
+  $pattern = '!^' . preg_quote($base_url) . '?' . preg_quote(base_path()) . '?!';
+  $path = preg_replace($pattern, '', $url);
+  
+  if ($getsize && is_file($path) && list($width, $height) = getimagesize($path)) {
+    $size_attributes = array();
+    $size_attributes['width'] = $width;
+    $size_attributes['height'] = $height;
+    $attributes = array_merge($size_attributes, $attributes);
+  }
   
-  $file = (array)$file;
-  if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
+  if (is_array($attributes) && count($attributes)) {
     $attributes = drupal_attributes($attributes);
-    if ($file['fid'] == 'upload') {
-      $path = file_create_filename($file['filename'], file_create_path());
-    }
-    else {
-      $path = $file['filepath'];
-    }
-    $url = file_create_url($path);
-    if ($alt == '') {
-      $alt = $file['alt'];
-    }
-    if ($title == '') {
-      $title = $file['title'];
-    }
-    return '<img src="'. check_url($url) .'" alt="'.
-        check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
   }
+  
+  return '<img src="' . check_url($url) . '" alt="' . check_plain($alt) . '" title="' . check_plain($title) . '" ' . $attributes . ' />';
 }
 
 /**
Index: imagefield.css
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.css,v
retrieving revision 1.1
diff -u -r1.1 imagefield.css
--- imagefield.css	7 Jul 2006 05:41:01 -0000	1.1
+++ imagefield.css	25 Nov 2006 05:33:02 -0000
@@ -12,9 +12,10 @@
   width: 165px;
 }
 
-div.imagefield-edit-image-replace { 
-  font-weight: bold;
+div.imagefield-edit-image-replace {
   color: red;
 }
 
-
+div.imagefield-edit-fields {
+  margin-left: 170px;
+}
\ No newline at end of file
