Index: image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v retrieving revision 1.293 diff -u -r1.293 image.module --- image.module 19 Jan 2009 10:43:30 -0000 1.293 +++ image.module 21 Jan 2009 23:41:50 -0000 @@ -523,6 +523,15 @@ * Implementation of hook_insert */ function image_insert($node) { + // If no new file posted, skip all this update but if a translation, fill in the image table + if (empty($node->new_file)) { + // Just in the case we have a translation source, fill in the table. Both nodes will use the same image. + if (!empty($node->translation_source)) { + db_query("INSERT INTO {image}(nid, fid, image_size) SELECT %d, fid, image_size FROM {image} WHERE nid = %d", $node->nid, $node->translation_source->nid); + } + return; + } + // Derivative images that aren't needed are set to the original file. Make // note of the current path before calling _image_insert() because if it's // in the temp directory it'll be moved. We'll need it later to determine @@ -563,10 +572,9 @@ // Remove all the existing images. $result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid); while ($file = db_fetch_object($result)) { - file_delete(file_create_path($file->filepath)); - db_query("DELETE FROM {files} WHERE fid = %d", $file->fid); + db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid); + _image_file_remove($file); } - db_query("DELETE FROM {image} WHERE nid = %d", $node->nid); // Save the original first so that it if it's moved the derivatives are // placed in the correct directory. @@ -579,12 +587,11 @@ // Delete all but the original image. $result = db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND f.fid <> %d", $node->nid, $original_file->fid); while ($file = db_fetch_object($result)) { + db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid); // Beware of derivative images that have the same path as the original. if ($file->filepath != $original_file->filepath) { - file_delete(file_create_path($file->filepath)); + _image_file_remove($file); } - db_query("DELETE FROM {files} WHERE fid = %d", $file->fid); - db_query("DELETE FROM {image} WHERE fid = %d", $file->fid); } $node->images = _image_build_derivatives($node, FALSE); @@ -614,10 +621,9 @@ function image_delete($node) { $result = db_query('SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d', $node->nid); while ($file = db_fetch_object($result)) { - file_delete(file_create_path($file->filepath)); - db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); - } - db_query("DELETE FROM {image} WHERE nid = %d", $node->nid); + db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid); + _image_file_remove($file); + } } /** @@ -988,6 +994,16 @@ } /** + * Remove image file if no further references in the image table to it + */ +function _image_file_remove($file) { + if (!db_result(db_query("SELECT COUNT(*) FROM {image} WHERE fid = %d", $file->fid))) { + file_delete(file_create_path($file->filepath)); + db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); + } +} + +/** * Function to other modules to use to create image nodes. * * @param $filepath