diff --git a/taxonomy_image.module b/taxonomy_image.module index 105ec8b..cc30002 100644 --- a/taxonomy_image.module +++ b/taxonomy_image.module @@ -1,5 +1,4 @@ $current->width, 'height' => $current->height, + 'class' => 'taxonomy-image-term-' . $current->tid .' taxonomy-image-vid-' . $current->vid ); // $tag was originally an HTML attribute string. It should now be a standard attributes array. // If the caller provided the same key, this will force me to use those. @@ -252,9 +252,9 @@ function taxonomy_image_get_object($tid, $recursive = NULL) { } // If there was no image, but there is a default, fake it. + $term = taxonomy_get_term($tid); if (!isset($image[$tid]->path) && ($default = variable_get('taxonomy_image_default', NULL))) { $image[$tid]->path = $default; - $term = taxonomy_get_term($tid); $image[$tid]->name = $term->name; $image[$tid]->description = $term->description; // $image[$tid]->url = file_create_url($mypath . $image[$tid]->path); @@ -263,8 +263,11 @@ function taxonomy_image_get_object($tid, $recursive = NULL) { // Get more properties if we had an image. if (!empty($image[$tid]->path)) { $image[$tid]->tid = $tid; -// $img = getimagesize($image[$tid]->path); - $img = getimagesize($image[$tid]->url); + $image[$tid]->vid = $term->vid; + $p = $fullpath . $image[$tid]->path; + if (is_readable($p) ) { + $img = getimagesize($p); + } // Make sure it worked. if (!$img) { return NULL; @@ -658,7 +661,9 @@ function taxonomy_image_taxonomy($op, $type, $form_values = NULL) { } break; case 'delete': - taxonomy_image_delete($tid); + if (_taxonomy_image_exists($tid)) { + taxonomy_image_delete($tid); + } break; } } @@ -667,10 +672,10 @@ function taxonomy_image_taxonomy($op, $type, $form_values = NULL) { * 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 +688,87 @@ function taxonomy_image_add($tid, $filename) { } } +/** + * 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. + */ 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)); + $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)); - // Delete the cached version. - cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); + // 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; + } + + 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'); + } + return; + } $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); + $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 { - // 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 image %path.', array('%path' => $full_path)), 'error'); + // Don't even try to delete from disk. + return; } - $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))); + $phys_del = file_delete($full_path); + if ($phys_del) { + if ($verbose) { + drupal_set_message(t('%path deleted from disk.', array('%path' => $full_path)), 'status'); + } } 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')))); + drupal_set_message(t('Error deleting image file %path from disk.', array('%path' => $full_path)), 'warning'); } - return; } +/** + * Check if the given TID has 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)); } /** @@ -752,3 +803,13 @@ function taxonomy_image_views_handlers() { ), ); } + +/** + * Implementation of hook_ctools_plugin_directory(). + */ +function taxonomy_image_ctools_plugin_directory($module, $plugin) { + if ($module == 'ctools' && !empty($plugin)) { + return "plugins/$plugin"; + } +} +