Index: field_image.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flexinode/field_image.inc,v
retrieving revision 1.16
diff -u -p -r1.16 field_image.inc
--- field_image.inc	16 Apr 2005 12:54:16 -0000	1.16
+++ field_image.inc	23 Jul 2005 10:52:28 -0000
@@ -8,71 +8,107 @@ function flexinode_field_image_name($fie
 function flexinode_field_image_form($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
   $output = '';
-  if ($node->$fieldname) {
-    $output .= form_hidden($fieldname .'_old', serialize($node->$fieldname));
+  // check if the existing image node still exists
+  $image_old = db_fetch_object(db_query("SELECT nid from {node} WHERE nid = %d", $node->{$fieldname}));
+  if (is_numeric($node->$fieldname) && $image_old) {
+    $output .= form_hidden($fieldname .'_old', $node->$fieldname);
+    $desc = t('Image %number has been created: if you upload another one, the current will be replaced.', array('%number' => l($node->$fieldname, 'node/'.$node->$fieldname)));
+  } 
+  else {
+    $desc = '';
   }
-  $output .= form_file(t($field->label), $fieldname, 40, ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') .' '. t($field->description) .' '. t('The file is limited to %kbKB and a resolution of %wxh pixels (width x height).', array('%wxh' => $field->options[1], '%kb' => $field->options[2])), $field->required);
+  $output .= form_file(t($field->label), $fieldname, 40, $desc .' '. t($field->description) .' '. t('The file is limited to %kbKB and a resolution of %wxh pixels (width x height).', array('%wxh' => $field->options[1], '%kb' => $field->options[2])), $field->required);
+
   return $output;
 }
 
 function flexinode_field_image_db_select($field) {
   $fieldname = 'flexinode_'. $field->field_id;
-  return $fieldname .'.serialized_data AS '. $fieldname;
+  return $fieldname .'.numeric_data AS '. $fieldname;
 }
 
 function flexinode_field_image_db_sort_column($field) {
-  return 'flexinode_'. $field->field_id .'.textual_data';
+  return 'flexinode_'. $field->field_id .'.numeric_data';
 }
 
 function flexinode_field_image_insert($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  $node->$fieldname = file_save_upload($node->$fieldname, $node->$fieldname->filename);
-  $serialized = is_object($node->$fieldname) ? serialize($node->$fieldname) : '';
-  db_query("INSERT INTO {flexinode_data} (nid, field_id, textual_data, serialized_data) VALUES (%d, %d, '%s', '%s')", $node->nid, $field->field_id, $node->$fieldname->filename, $serialized);
+
+  // check if the existing image node still exists
+  $image_old = db_fetch_object(db_query("SELECT nid from {node} WHERE nid = %d", $node->{$fieldname . '_old'}));
+
+  if ($node->$fieldname) {
+    $file = file_save_upload($node->$fieldname, _image_filename($node->$fieldname->filename, NULL, TRUE));
+
+    // create a new image node
+    $image->type     = 'image';
+    $image->title    = $node->title . ' ' . $field->label;
+    $image->uid      = $node->uid;
+    $image->file     = $image->images['_original'] = $file->filepath;
+
+    if ($image_old->nid) {
+      $image->nid = $image_old->nid;
+    }
+    _image_build_derivatives($image, true);
+    $image_nid = node_save($image);
+  }
+  else {
+    $image_nid = $node->{$fieldname . '_old'};
+  }
+
+  db_query("INSERT INTO {flexinode_data} (nid, field_id, numeric_data) VALUES (%d, %d, %d)", $node->nid, $field->field_id, $image_nid);
   return $node;
 }
 
 function flexinode_field_image_delete($field, $node, $unconditional = 0) {
   $fieldname = 'flexinode_'. $field->field_id;
-  $result = db_fetch_object(db_query('SELECT serialized_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id));
-  $file = unserialize($result->serialized_data);
-  if ($unconditional || $node->$fieldname != $file) {
-    file_delete($file->filepath);
-  }
+  // should we remove the image node?
+  $image['nid'] = $node->$fieldname;
+  $image['confirm'] = 1;
+  node_delete($image);
 }
 
 function flexinode_field_image_validate($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-
+  
   $file = flexinode_validate_picture($field, $node);
 
   if (is_string($file)) {
-    return array('value' => $node->$fieldname, 'error' => $file);
+    return array('value' => $node->{$fieldname}, 'error' => $file);
   }
   else if (is_object($file)) {
     return array('value' => $file);
   }
   else if (empty($node->$fieldname)) {
-    return array('value' => unserialize($node->{$fieldname .'_old'}));
+    // return array('value' => unserialize($node->{$fieldname .'_old'}));
   }
 }
 
 function flexinode_field_image_format($field, $node, $brief = 0) {
   $fieldname = 'flexinode_'. $field->field_id;
-  $file = is_object($node->$fieldname) ? $node->$fieldname : unserialize($node->$fieldname);
-  if ($file) {
+  if (is_numeric($node->$fieldname)) {
+    $image = node_load(array('nid' => $node->$fieldname));
+  }
+  if ($node && $image->nid) {
     if ($brief) {
-      return '<a href="'. file_create_url($file->filepath) .'">'. check_plain($file->filename) .'</a> ('. format_size($file->filesize) .')';
+      return '<a href="node/' . $image->nid . '">' . $image->title . '</a>';
     }
     else {
-      return '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($node->title) .'" />';
+      // Check if we aren't in teaser mode. Must find a better way
+      $path = explode('/',$_GET['q']);
+      if ($node->nid == $path[count($path)-1]) {
+	return '<a href="node/' . $image->nid . '"><img src="image/view/' . $image->nid . '/thumbnail" alt="'. check_plain($image->title) .'" /></a>';
+      } 
+      else {
+	return '<img src="image/view/' . $image->nid . '/thumbnail' .'" alt="'. check_plain($image->title) .'" />';
+      }
     }
   }
 }
 
 function flexinode_field_image_load($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  return unserialize($node->$fieldname);
+  return $node->$fieldname;
 }
 
 function flexinode_field_image_config($field, $edit) {
@@ -144,35 +180,4 @@ function flexinode_validate_picture($fie
 
   return 0;
 }
-
-function flexinode_make_thumbnail($path, $name) {
-  $_imagick_convert = variable_get('flexinode_convert_path', '/usr/local/bin/convert');
-  $filter = '-scale '. variable_get('flexinode_thumb_size', '100') .' -filter QUADRATIC';
-  list($twidth, $theight) = explode('x', variable_get('flexinode_thumb_size', '100'));
-  $size = getimagesize($path);
-  $path_only = str_replace($name, '', $path);
-  $thumb = $path_only .'t_'. flexinode_image_escape_shell($name);
-
-  if ($size[0] < $twidth || $size[1] < $theight) {
-    copy($path, $thumb);
-  }
-  else {
-    $path = flexinode_image_escape_shell($path);
-    exec("$_imagick_convert $filter $path $thumb");
-  }
-
-  return  $path_only .'t_'. $name;
-}
-
-function flexinode_image_escape_shell($filename) {
-
-  if (strstr($_ENV['OS'], 'Windows')) {
-  // I can't make escapeshellarg work on windows, it adds a '
-  // this should be safe enough
-    return '"'.addslashes($filename).'"';
-  }
-  else {
-    return escapeshellarg($filename);
-  }
-}
 ?>
