Index: taxonomy_image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/taxonomy_image.module,v
retrieving revision 1.13
diff -u -F^f -r1.13 taxonomy_image.module
--- taxonomy_image.module	18 Jan 2007 04:43:13 -0000	1.13
+++ taxonomy_image.module	16 May 2007 00:45:54 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: taxonomy_image.module,v 1.13 2007/01/18 04:43:13 jeremy Exp $ taxonomy_image.module
+// $Id: taxonomy_image.module,v 1.12.4.1 2007/01/18 05:00:24 jeremy Exp $ taxonomy_image.module
 
 /*
 ** taxonomy_image.module:
@@ -14,7 +14,7 @@
  * if an image is found.  The format of the link can be modified with the
  * tags parameter.
  *
- * @param  int     $tid	 the term id.
+ * @param  int     $tid   the term id.
  * @param  string  $tags optional tags to add into the <img src=''> link
  * 
  * @return string  An html <img src> link.
@@ -142,19 +142,20 @@ function taxonomy_image_menu($may_cache)
   $items = array();
 
   if ($may_cache) {
-    $items[] = array('path' => 'admin/content/taxonomy/image', 'title' => t('Category Images'),
-      'callback' => 'taxonomy_image_admin',
-      'access' => user_access('administer taxonomy images'),
-      'type' => MENU_LOCAL_TASK);
-    // Admin Settings
     $items[]= array (
-      'path' => 'admin/settings/taxonomy_image',
-      'title' => t('Taxonomy Image'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => array('taxonomy_image_admin_settings'),
+      'path' => 'admin/content/taxonomy/taxonomy_image',
+      'title' => t('Images'),
+      'callback' => 'taxonomy_image_overview',
       'access' => user_access('administer site configuration'),
-      'description' => t('Global configuration of taxonomy image functionality.'),
-      'type' => MENU_NORMAL_ITEM,
+      'description' => t('An overview of taxonomy images'),
+      'type' => MENU_LOCAL_TASK,
+    );
+    $items[]= array (
+      'path' => 'admin/settings/taxonomy_image',
+      'title' => t('Administer Taxonomy images'),
+      'callback' => 'taxonomy_image_admin_settings',
+      'description' => t('An overview of taxonomy images'),
+      'type' => MENU_CALLBACK,
     );
   }
 
@@ -277,41 +278,6 @@ function taxonomy_image_file_download($f
   }
 }
 
-// taxonomy_image specific functions
-function taxonomy_image_admin() {
-  global $form_values;
-  $op = $_POST['op'];
-  $tid = $_POST['tid'];
-
-  // TODO: Use menus, not arg()
-
-  if (empty($op)) {
-    $op = arg(3);
-  }
-
-  switch ($op) {
-    case 'image':
-      if (arg(4) == 'add' || arg(4) == 'edit') { 
-        $output = drupal_get_form('taxonomy_image_form', (array)(taxonomy_image_get_term(arg(5))));
-        break;
-      }
-      $output = taxonomy_image_overview();
-      break;
-    case t('Save'):
-      $output = taxonomy_image_save($tid);
-      $output = taxonomy_image_overview();
-      break;
-    case t('Delete'):
-      $output = taxonomy_image_delete($tid);
-      $output = taxonomy_image_overview();
-      break;
-    default:
-      $output = taxonomy_image_overview();
-  }
-
-  print theme('page', $output);
-}
-
 function taxonomy_image_overview() {
   $output = '';
   if (variable_get('taxonomy_image_recursive', 0)) {
@@ -333,7 +299,7 @@ function taxonomy_image_overview() {
     $tree = taxonomy_get_tree($vocabulary->vid);
     if ($tree) {
       foreach ($tree as $term) {
-        $data =  str_repeat('--', $term->depth) .' '. $term->name .' ('. ( _taxonomy_image_exists($term->tid) ? l(t('edit image'), "admin/content/taxonomy/image/edit/$term->tid") : l(t('upload image'), "admin/content/taxonomy/image/add/$term->tid") ) .')<br />';
+        $data =  str_repeat('--', $term->depth) .' '. $term->name .' ('. ( _taxonomy_image_exists($term->tid) ? l(t('edit image'), "admin/taxonomy/edit/term/$term->tid") : l(t('upload image'), "admin/taxonomy/edit/term/$term->tid") ) .')<br />';
         /* use taxonomy_image_display() instead of _taxonomy_image_exists() in
         ** case image display recursion is enabled...
         */
@@ -352,79 +318,63 @@ function taxonomy_image_get_term($tid) {
   return db_fetch_object(db_query('SELECT d.name, d.description, d.tid, i.path FROM {term_data} d LEFT JOIN {term_image} i ON d.tid = i.tid WHERE d.tid = %d', $tid));
 }
 
-function taxonomy_image_form($edit = array()) {
-  $form['#method'] = 'post';
-  $form['#action'] = 0;
-  $form['#attributes'] = array('enctype' => 'multipart/form-data');
-
-  // A path may be set when the image doesn't exist if using recursion
-  if (!is_null($edit['path']) && _taxonomy_image_exists($edit['tid'])) {
-    $form['current_image'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Current Image'),
-    );
-    $form['current_image']['image'] = array(
-      '#value' => taxonomy_image_display($edit['tid']) .'<br />'
-    );
-    $form['current_image']['delete'] = array(
-      '#type' => 'submit',
-      '#value' => t('Delete'),
-    );
-    drupal_set_title(t('Edit image'));
-  }
-  
-  $form['new_image'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Upload new image'),
-  );
-  $form['new_image']['path'] = array(
-    '#type' => 'file',
-    '#title' => t('Taxonomy image file'),
-    '#size' => 40,
-    '#description' => t("The image file you wish to associate with the '%term' term.", array('%term' => $edit['name'])),
-  );
-  $form['new_image']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save'),
-  );
-  if (!is_null($edit['tid'])) {
-    $form['new_image']['tid'] = array(
-      '#type' => 'hidden',
-      '#value' => $edit['tid'],
-    );
+function taxonomy_image_form_alter($form_id, &$form) {
+  switch ($form_id) {
+    case 'taxonomy_form_term': 
+      $form['#attributes'] = array('enctype' => 'multipart/form-data');
+      if ($preview = @taxonomy_image_display($form['tid']['#value'])) {
+        $form['current_image'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Current Image'),
+        );
+        $form['current_image']['image'] = array(
+        '#value' => $preview .'<br />'
+        );
+        $form['current_image']['current_image_delete'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Delete'),
+        );
+      }
+      $form['new_image']['path'] = array(
+        '#type' => 'file',
+        '#title' => t('Upload image'),
+        '#size' => 40,
+        '#description' => t("The image file you wish to associate this term."),
+      );
+      $form['submit']['#weight'] = 10;
+      $form['delete']['#weight'] = 10;
+      break;
   }
-
-  return $form;
 }
 
-function taxonomy_image_save($tid) {
-  $edit['tid'] = $tid;
-  $fields = array('tid', 'path');
-  if ($file = file_save_upload('path', file_create_path(variable_get('taxonomy_image_path', 'category_pictures')))) {
-    $edit['path'] = $file->filepath;
-
-    if ($old_image = db_fetch_object(db_query('SELECT tid FROM {term_image} WHERE tid = %d', $edit['tid']))) {
-      // delete old image before saving the new one
-      taxonomy_image_delete($old_image->tid);
+function taxonomy_image_taxonomy($op, $type, $form_values = NULL) {
+  if (($op == 'insert' || $op == 'update') && $type == 'term') {
+    if ($form_values['tid'] && $form_values['current_image_delete']) {
+      taxonomy_image_delete($form_values['tid']);
+      drupal_set_message(t('Image deleted'));
     }
+    if ($file = $file = file_check_upload('path')) {
+      file_save_upload($file, file_create_path(variable_get('taxonomy_image_path', 'category_pictures')));
 
-    foreach ($fields as $field) {
-      $values[] = (string)db_escape_string($edit[$field]);
-    }
+      if ($old_image = db_fetch_object(db_query('SELECT tid FROM {term_image} WHERE tid = %d', $form_values['tid']))) {
+        // delete old image before saving the new one
+        taxonomy_image_delete($old_image->tid);
+      }
 
-    db_query('INSERT INTO {term_image} (' .implode(', ', $fields). ') VALUES (\'' .implode('\', \'', $values). '\')');
-    cache_clear_all();
+      db_query("INSERT INTO {term_image} (tid, path) VALUES ('%s', '%s')", $form_values['tid'], $file->filepath);
+      cache_clear_all();
 
-    $message = t('Image uploaded.');
-  }
-  else if (!file_check_directory(file_create_path(variable_get('taxonomy_image_path', 'category_pictures')))) {
-    // we know waht'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' => variable_get('file_directory_path', 'files'). '/' . variable_get('taxonomy_image_path', 'category_pictures'))));
-  }
-  else {
-    $message = theme('error', t('Image upload failed.'));
+      $message = t('Image uploaded.');
+    }
+    else if (!file_check_directory(file_create_path(variable_get('taxonomy_image_path', 'category_pictures')))) {
+      // 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' => variable_get('file_directory_path', 'files'). '/' . variable_get('taxonomy_image_path', 'category_pictures'))));
+    }
+    else {
+      $message = theme('error', t('Image upload failed.'));
+    }
+    return drupal_set_message($message);
   }
-  return drupal_set_message($message);
 }
 
 function taxonomy_image_delete($tid) {
@@ -436,9 +386,7 @@ function taxonomy_image_delete($tid) {
 
 function _taxonomy_image_exists($tid) {
   if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid = %d', $tid))) {
-    return 1;
+    return TRUE;
   }
-  return 0;
-}
-
-?>
+  return FALSE;
+}
\ No newline at end of file
