Index: taxonomy_image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/taxonomy_image.module,v retrieving revision 1.12.4.13.2.43 diff -u -r1.12.4.13.2.43 taxonomy_image.module --- taxonomy_image.module 19 Mar 2009 13:44:46 -0000 1.12.4.13.2.43 +++ taxonomy_image.module 5 Apr 2009 20:16:52 -0000 @@ -658,7 +658,8 @@ } break; case 'delete': - taxonomy_image_delete($tid); + if (_taxonomy_image_exists($tid)) + taxonomy_image_delete($tid); break; } } @@ -667,10 +668,10 @@ * Helper function for adding an image to a term */ function taxonomy_image_add($tid, $filename) { - - $count = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid)); - if ($count == 1) { - // Delete old image before saving the new one. + // TODO: maybe we need to store also the FILE-ID from {files} table, better deletion and shared counts + + // Delete old image before saving the new one. + if (_taxonomy_image_exists($tid)) { taxonomy_image_delete($tid); } @@ -683,41 +684,59 @@ } } +/** + * Deletes the Taxonomy Image associated to the given Term-ID + */ function taxonomy_image_delete($tid) { - $old_path = db_result(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid)); - $how_many = db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $old_path)); - - // Delete the cached version. - cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); + if ($path = _taxonomy_image_exists($tid)) { + $count = taxonomy_image_get_shared_count($path); - $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'; - $full_path = $taxonomy_image_path . $old_path; - if ($how_many == 1) { - // This is the only term using this file, so it is safe to delete it. - $file_del_ok = db_query("DELETE FROM {files} WHERE filepath='%s'", $full_path); + $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'; + $full_path = $taxonomy_image_path . $old_path; + $file_del = FALSE; + + if ($count == 1) { + // TODO: we should rely on drupal {files} and FID? + // This is the only term using this file, so it is safe to delete it + // The physical file will be deleted only if also removed from {files} table + $file_del = db_query("DELETE FROM {files} WHERE filepath='%s'", $full_path) && file_delete($full_path); + } + else { + // This file is shared, don't remove file, but un-assoch TID from this image + $file_del = TRUE; + } + if ($file_del) { + $del_tid = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid); + if ($del_tid) { + drupal_set_message(t('Term-image %name removed.', array('%name' => $path))); + // Delete the cached version. + cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); + } + else { + drupal_set_message(t('Error deleting term-image %name.', array('%name' => $path)), 'error'); + } + } } else { - // Pretend we deleted it. - $file_del_ok = TRUE; - drupal_set_message(t('Not deleted from the files table because it is use on !count other terms.', array('!count' => $how_many - 1))); + drupal_set_message(t("Error deleting Term-image, seems that this image doesn't exists."), 'error'); } +} - $db_del_ok = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid); - if ($file_del_ok && $db_del_ok) { - drupal_set_message(t('!name image removed.', array('!name' => $old_path))); - } - else { - drupal_set_message(t('Image delete failed. File: !file, Db: !db.', - array('!file' => ($file_del_ok ? 'yes' : 'no'), '!db' => ($db_del_ok ? 'yes' : 'no')))); - } - return; +/** + * Retrieve count of TID associated with given $path + */ +function taxonomy_image_get_shared_count($path) { + return db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $path)); } +/** + * Check if the given TID have an image associated + * + * @param $tid: Term-ID to check + * @return the image filename or FALSE + */ function _taxonomy_image_exists($tid) { - if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid))) { - return TRUE; - } - return FALSE; + return db_result(db_query("SELECT path FROM {term_image} WHERE tid=%d", $tid)); } /**