Index: taxonomy_image.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/Attic/taxonomy_image.admin.inc,v retrieving revision 1.1.2.10 diff -u -r1.1.2.10 taxonomy_image.admin.inc --- taxonomy_image.admin.inc 17 Apr 2009 15:54:42 -0000 1.1.2.10 +++ taxonomy_image.admin.inc 18 Apr 2009 09:25:04 -0000 @@ -74,13 +74,6 @@ '#maxlength' => 255, ); - $form['general']['taxonomy_image_verbose_delete'] = array( - '#type' => 'checkbox', - '#title' => t('Show all delete messages'), - '#default_value' => variable_get('taxonomy_image_verbose_delete', FALSE), - '#description' => t('When a term or image is deleted, this option displays that the image is deleted from this module, the files table, and the disk.'), - ); - $form['resizing'] = array( '#type' => 'fieldset', '#title' => t('Resizing Options'), Index: taxonomy_image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/taxonomy_image.module,v retrieving revision 1.12.4.13.2.46 diff -u -r1.12.4.13.2.46 taxonomy_image.module --- taxonomy_image.module 17 Apr 2009 16:25:35 -0000 1.12.4.13.2.46 +++ taxonomy_image.module 18 Apr 2009 09:34:52 -0000 @@ -622,7 +622,9 @@ // Did they mark it to delete? if ($form_values['taxonomy_image_current_image_delete'] == TRUE) { - taxonomy_image_delete($tid); + if (taxonomy_image_delete($tid) !== TRUE) { + drupal_set_message(t('Error deleting pre-associated term-image.'), 'error'); + } } // External file used? @@ -660,8 +662,8 @@ } break; case 'delete': - if (_taxonomy_image_exists($tid)) { - taxonomy_image_delete($tid); + if (taxonomy_image_delete($tid) !== TRUE) { + drupal_set_message(t('Error deleting term-image.'), 'error'); } break; } @@ -674,8 +676,8 @@ // @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); + if (taxonomy_image_delete($tid) !== TRUE) { + drupal_set_message(t('Error deleting pre-associated term-image.'), 'error'); } if (db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $filename)) { @@ -689,84 +691,73 @@ /** * Deletes the Taxonomy Image associated with the given termid. - * @param $tid - the id of the term to delete. - * return none - the association is broken, the path is removed from the files table, - * and the image is deleted from the disk, if appropriate. + * The association is broken, the path is removed from the files table, + * and the image is deleted from the disk, if appropriate. + * + * @param $tid - the TID of the term to delete. + * @return TRUE on success, FALSE on failure + * @TODO: provide more informative return-codes? */ function taxonomy_image_delete($tid) { - $verbose = variable_get('taxonomy_image_verbose_delete', FALSE); - // Is there an image to delete? - $path = _taxonomy_image_exists($tid); - if (!$path) { - // No image, go back now. - return; - } - - // See how many terms are using this image. - $count = db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $path)); - - // Get the term's name. - $term = taxonomy_get_term($tid); - $term_name = check_plain(taxonomy_image_tt("taxonomy:term:$term->tid:name", $term->name)); - - // Break the term to image association. - $del_tid = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid); - if ($del_tid) { - if ($verbose) { - drupal_set_message(t('Term image association removed for !name.', array('!name' => $term_name)), 'status'); - } - // Delete the cached version. - cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); - } - else { - drupal_set_message(t('Error deleting %path for !name.', array('%path' => $path, '!name' => $term_name)), 'error'); - // Don't delete anything else either. - return; - } + $return = FALSE; + $filename = NULL; - if ($count > 1) { - // This file is shared, don't remove file. - if ($verbose) { - drupal_set_message(t('%path is used !count other places.', array('%path' => $full_path, '!count' => $count - 1)), 'status'); + if ($filename = taxonomy_image_exists($tid)) { + $count = taxonomy_image_get_shared_count($filename); + $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'; + $full_path = $taxonomy_image_path . $filename; + $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); } - return; - } - - $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'; - $full_path = $taxonomy_image_path . $path; - - // 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); - if ($file_del) { - if ($verbose) { - drupal_set_message(t('%path deleted from database.', array('%path' => $full_path)), 'status'); + else { + // This file is shared, so don't remove it: only un-assoch TID from this image + $file_del = TRUE; } - } - else { - drupal_set_message(t('Error deleting image %path.', array('%path' => $full_path)), 'error'); - // Don't even try to delete from disk. - return; - } - - $phys_del = file_delete($full_path); - if ($phys_del) { - if ($verbose) { - drupal_set_message(t('%path deleted from disk.', array('%path' => $full_path)), 'status'); + if ($file_del) { + $del_tid = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid); + if ($del_tid) { + // Delete the cached version. + cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); + // Term-Image successfully deleted. + $return = TRUE; + } + else { + // Error deleting from {term_image} table. + $return = FALSE; + } + } + else { + // Error deleting from {files} or removing phisycal file. + $return = FALSE; } } else { - drupal_set_message(t('Error deleting image file %path from disk.', array('%path' => $full_path)), 'warning'); + // This term haven't any img associated, so no deletion needed (so it's all OK) + $return = TRUE; } + + return $return; +} + +/** + * Retrieve count of TID associated with given $filename + */ +function taxonomy_image_get_shared_count($filename) { + return db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $filename)); } /** - * Check if the given TID has an image associated. + * 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) { +function taxonomy_image_exists($tid) { return db_result(db_query("SELECT path FROM {term_image} WHERE tid=%d", $tid)); }