Index: taxonomy_image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/taxonomy_image.module,v retrieving revision 1.12.4.13.2.37 diff -u -r1.12.4.13.2.37 taxonomy_image.module --- taxonomy_image.module 17 Feb 2009 23:00:58 -0000 1.12.4.13.2.37 +++ taxonomy_image.module 10 Mar 2009 22:26:17 -0000 @@ -521,7 +521,7 @@ function taxonomy_image_form_alter(&$form, &$form_state, $form_id) { switch ($form_id) { - case 'taxonomy_form_term': + case 'taxonomy_form_term': // Don't show Term-Image fieldset if we're deleting this term. if ($form_state['confirm_delete']) { return; @@ -543,7 +543,7 @@ $form['taxonomy_image']['current_image']['image'] = array('#value' => $preview); - $form['taxonomy_image']['current_image']['current_image_delete'] = array( + $form['taxonomy_image']['current_image']['taxonomy_image_current_image_delete'] = array( '#type' => 'checkbox', '#title' => t('Delete'), '#prefix' => '
', @@ -551,14 +551,13 @@ ); } - $form['taxonomy_image']['new_image']['upload'] = array( + $form['taxonomy_image']['new_image']['taxonomy_image_upload'] = array( '#type' => 'file', '#title' => t('Upload image'), '#size' => 60, '#description' => t("The image file you wish to associate this term."), ); - - $form['taxonomy_image']['external'] = array( + $form['taxonomy_image']['taxonomy_image_external'] = array( '#type' => 'textfield', '#title' => t('Use external image'), '#default_value' => $curr_path, @@ -581,103 +580,99 @@ * Check an external link. */ function taxonomy_image_validate_external($form, &$form_state) { - if (empty($form_state['values']['external'])) { + if (empty($form_state['values']['taxonomy_image_external'])) { return; } - if (valid_url($form_state['values']['external']) - && substr($form_state['values']['external'], 0, 4) == 'http') { + if (valid_url($form_state['values']['taxonomy_image_external']) + && substr($form_state['values']['taxonomy_image_external'], 0, 4) == 'http') { return; } - form_set_error('external', t('The URL (@url) you entered does not appear to be valid.', array('@url' => $form['#value']))); + form_set_error('taxonomy_image_external', t('The URL (@url) you entered does not appear to be valid.', array('@url' => $form['#value']))); } /** * Implementation of hook_taxonomy(). */ function taxonomy_image_taxonomy($op, $type, $form_values = NULL) { - $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures')); - file_check_directory($directory, FILE_CREATE_DIRECTORY); - // We're only interested in term changes. if ($type != 'term') { return; } - + $tid = $form_values['tid']; switch ($op) { case 'insert': case 'update': -// drupal_set_message(show_array($form_values)); - // Determine which option they chose. - if ($form_values['current_image_delete']) { + + $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures')); + file_check_directory($directory, FILE_CREATE_DIRECTORY); + + // Did they mark it to delete? + if ($form_values['taxonomy_image_current_image_delete'] == TRUE) { taxonomy_image_delete($tid); } - if ($form_values['upload']) { // Upload an image. - $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures')); - file_check_directory($directory, FILE_CREATE_DIRECTORY); - - // Delete the cached version. - cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); - - // Did they mark it to delete? - if (!empty($form_values['current_image_delete'])) { - taxonomy_image_delete($tid); - } - $is_current_image = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid)); - // FILE_EXISTS_REPLACE allows multiple terms to use the same image without having multiple files. + // External file used? + if (!empty($form_values['taxonomy_image_external'])) { + $source = $form_values['taxonomy_image_external']; + $filename = basename($source); + $where = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'. $filename; + + if (copy($source, $where)) { + drupal_set_message(t('Copy from @source to @where successful.', array('@source' => $source, '@where' => $where))); + taxonomy_image_add($tid, $filename); + } + else { + drupal_set_message(t('Copy from @source to @where failed.', array('@source' => $source, '@where' => $where)), 'error'); + } + } + // Use upload field + else { $validators = array(); - if ($file = file_save_upload('path', $validators, $directory, FILE_EXISTS_REPLACE)) { - if ($is_current_image) { - // Delete old image before saving the new one. - taxonomy_image_delete($tid); - } + // FILE_EXISTS_REPLACE allows multiple terms to use the same image without having multiple files. + $file = file_save_upload('taxonomy_image_upload', $validators, $directory, FILE_EXISTS_REPLACE); + + if(is_object($file)) { // Make sure Cron doesn't delete this file. file_set_status($file, FILE_STATUS_PERMANENT); $filepath = variable_get('taxonomy_image_path', 'category_pictures') . '/' . $file->filename; - if (db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $filepath)) { - $message = t('Image uploaded as @name.', array('@name' => $filepath)); - } - else { - $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array('!tid' => $tid, '@path' => $filepath))); - } + + if (taxonomy_image_add($tid, $file->filename)) + drupal_set_message(t('Image uploaded as @name.', array('@name' => $filepath))); + else + drupal_set_message(t('Database insert failed. [tid = !tid, path = @path.', array('!tid' => $tid, '@path' => $filepath)), 'error'); } - else if (!file_check_directory($directory)) { + elseif (!file_check_directory($directory)) { // we know what's wrong, so generate a more useful error message - $message = theme('error', t('The category picture directory "%dir" does not exist, or is not writable.', array('%dir' => file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures')))); + drupal_set_message(t('The category picture directory "%dir" does not exist, or is not writable.', array('%dir' => $directory)), 'error'); } else { - $message = theme('error', t('Image upload failed.')); + drupal_set_message(t('Image upload failed.'), 'error'); } - return drupal_set_message($message); } - - if ($form_values['external']) { // Provided an external path. - // Delete the cached version. - cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); - - // Delete old image before saving the new one. - if (db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid))) { - taxonomy_image_delete($tid); - } - $source = $form_values['external']; - $base = basename($source); - $where = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/'. $base; - if (copy($source, $where)) { - drupal_set_message(t('Copy from @source to @where successful.', array('@source' => $source, '@where' => $where))); - } - else { - drupal_set_message(t('Copy from @source to @where failed.', array('@source' => $source, '@where' => @where))); - } - if (!db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $base)) { - $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array('!tid' => $tid, '@path' => $base))); - } - return; - } - + break; case 'delete': taxonomy_image_delete($tid); - return; + break; + } +} +/** + * 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. + taxonomy_image_delete($tid); + } + + if(db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $filename)) { + cache_clear_all("taxonomy_image:$tid", 'cache_tax_image'); + return TRUE; + } + else { + return FALSE; } } @@ -702,7 +697,7 @@ $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))); + drupal_set_message(t('!name image removed.', array('!name' => $old_path))); } else { drupal_set_message(t('Image delete failed. File: !file, Db: !db.',