? .svn
? 164448.patch
? contributed/.svn
? contributed/theme_patches/.svn
Index: taxonomy_image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_image/taxonomy_image.module,v
retrieving revision 1.12.4.1
diff -u -p -r1.12.4.1 taxonomy_image.module
--- taxonomy_image.module	18 Jan 2007 05:00:24 -0000	1.12.4.1
+++ taxonomy_image.module	7 Feb 2008 21:51:11 -0000
@@ -14,23 +14,92 @@
  * if an image is found.  The format of the link can be modified with the
  * tags parameter.
  *
+ * See also taxonomy_image_get_object() and taxonomy_image_get_url().
+ *
  * @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.
  */
 function taxonomy_image_display($tid, $tags = NULL) {
+  $image = taxonomy_image_get_object($tid);
+  // Do we have an image?
+  if ($image->path) {
+    if (!$image->width || !$image->height) {
+      // get the image size
+      list($image->width, $image->height) = getimagesize(file_directory_path() .'/'. $image->path);
+      // handle image resizing @todo not tested with recent changes
+      switch (variable_get('taxonomy_image_resize', 0)) {
+        case 3: // exact
+          if ($width = variable_get('taxonomy_image_width', 0))
+            $image->width = $width;
+          if ($height = variable_get('taxonomy_image_height', 0))
+            $image->height = $height;
+          break;
+        case 2: // not less than
+          if (($width = variable_get('taxonomy_image_width', 0)) &&
+              ($width > $image->width)) {
+            $width_scale = $image->width / $width;
+          }
+          if (($height = variable_get('taxonomy_image_height', 0)) &&
+              ($height > $image->height)) {
+            $height_scale = $image->height / $height;
+          }
+          if ($height_scale || $width_scale) {
+            if ($width_scale && $height_scale)
+              $scale = min($width_scale, $height_scale);
+            else
+              $scale = $width_scale ? $width_scale : $height_scale;
+            $image->height = $image->height / $scale;
+            $image->width = $image->width / $scale;
+          }
+          break;
+        case 1: // not greater than
+          if (($width = variable_get('taxonomy_image_width', 0)) &&
+              ($width < $image->width)) {
+            $width_scale = $image->width / $width;
+          }
+          if (($height = variable_get('taxonomy_image_height', 0)) &&
+              ($height < $image->height)) {
+            $height_scale = $image->height / $height;
+          }
+          if ($height_scale || $width_scale) {
+            $scale = max($width_scale, $height_scale);
+            $image->height = $image->height / $scale;
+            $image->width = $image->width / $scale;
+          }
+          break;
+      }
+    }
+    return "<img src='$image->url' width='$image->width' height='$image->height' alt='$image->name' $tags />";
+  }
+  return '';
+}
+
+/**
+ * Get the url for an image, for use in css, html or other client-side code
+ * @param   int    $tid  the term id
+ * @return  string  the url
+ */
+function taxonomy_image_get_url($tid) {
+  $image = taxonomy_image_get_object($tid);
+  return $image->url;
+}
+
+/**
+ * Call this function to get an image object with more useful data for custom formatting
+ * @param   int     $tid    the term tid
+ * @return  image   object
+ */
+function taxonomy_image_get_object($tid) {
   global $user;
   static $image = array();
 
-  if (user_access('access taxonomy images') &&
-      !$user->taxonomy_image_disable_images) {
+  if (user_access('access taxonomy images') && !$user->taxonomy_image_disable_images) {
     // do lookup, return full display path
     if (!$image[$tid]->url) {
-      if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) {
-        $image[$tid]->url = file_create_url($image[$tid]->path);
-      }
-      else if (variable_get('taxonomy_image_recursive', 0)) {
+      $image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid));
+      if (!$image && variable_get('taxonomy_image_recursive', 0)) {
         // walk up the taxonomy hierarchy and look for an image
         $orig = $tid;
         while ($parent = db_fetch_object(db_query('SELECT t.tid FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', $tid))) {
@@ -42,64 +111,21 @@ function taxonomy_image_display($tid, $t
           } 
           else if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) {
             // we found a parent with a configured image, use it
-            $image[$tid]->url = file_create_url($image[$tid]->path);
+            $image[$tid]->path;
             $image[$orig] = $image[$tid];
             break;
           }
         }
       }
     }
-    if ($image[$tid]->url) {
-      if (!$image[$tid]->width || !$image[$tid]->height) {
-        list($image[$tid]->width, $image[$tid]->height) = getimagesize($image[$tid]->path);
-        // handle image resizing
-        switch (variable_get('taxonomy_image_resize', 0)) {
-          case 3: // exact
-            if ($width = variable_get('taxonomy_image_width', 0))
-              $image[$tid]->width = $width;
-            if ($height = variable_get('taxonomy_image_height', 0))
-              $image[$tid]->height = $height;
-            break;
-          case 2: // not less than
-            if (($width = variable_get('taxonomy_image_width', 0)) &&
-                ($width > $image[$tid]->width)) {
-              $width_scale = $image[$tid]->width / $width;
-            }
-            if (($height = variable_get('taxonomy_image_height', 0)) &&
-                ($height > $image[$tid]->height)) {
-              $height_scale = $image[$tid]->height / $height;
-            }
-            if ($height_scale || $width_scale) {
-              if ($width_scale && $height_scale)
-                $scale = min($width_scale, $height_scale);
-              else
-                $scale = $width_scale ? $width_scale : $height_scale;
-              $image[$tid]->height = $image[$tid]->height / $scale;
-              $image[$tid]->width = $image[$tid]->width / $scale;
-            }
-            break;
-          case 1: // not greater than
-            if (($width = variable_get('taxonomy_image_width', 0)) &&
-                ($width < $image[$tid]->width)) {
-              $width_scale = $image[$tid]->width / $width;
-            }
-            if (($height = variable_get('taxonomy_image_height', 0)) &&
-                ($height < $image[$tid]->height)) {
-              $height_scale = $image[$tid]->height / $height;
-            }
-            if ($height_scale || $width_scale) {
-              $scale = max($width_scale, $height_scale);
-              $image[$tid]->height = $image[$tid]->height / $scale;
-              $image[$tid]->width = $image[$tid]->width / $scale;
-            }
-            break;
-        }
-      }
-      $current = $image[$tid];
-      return "<img src='$current->url' width='$current->width' height='$current->height' alt='$current->name' $tags />";
+    
+    if (!empty($image[$tid]->path)) {
+      // set the url
+      $image[$tid]->url = file_create_url($image[$tid]->path);
     }
+    
+    return $image[$tid];
   }
-  return '';
 }
 
 // standard Drupal functions
@@ -110,7 +136,7 @@ function taxonomy_image_perm() {
 function taxonomy_image_help($section = '') {
   switch ($section) {
     case 'admin/content/taxonomy/image':
-      return t('The taxonomy_image module allows site administrators to associate images with category terms.  Once defined, this association allows Drupal themes to display images with site content.  For example, this module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food.  To upload a new image for a specific term, click "upload image" next to the term.  To modify or delete and existing image, click "edit image".  To learn more about how to create vocabularies and terms, review the <a href="%taxonomy-help">taxonomy help page</a>.', array('%taxonomy-help' => url('admin/help/taxonomy')));
+      return t('The taxonomy_image module allows site administrators to associate images with category terms.  Once defined, this association allows Drupal themes to display images with site content.  For example, this module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food.  To upload a new image for a specific term, click "upload image" next to the term.  To modify or delete and existing image, click "edit image".  To learn more about how to create vocabularies and terms, review the <a href="!taxonomy-help">taxonomy help page</a>.', array('!taxonomy-help' => url('admin/help/taxonomy')));
     case 'admin/help#taxonomy_image':
       return t('
       <h3>Introduction</h3>
@@ -398,27 +424,27 @@ function taxonomy_image_form($edit = arr
 }
 
 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']))) {
+  // Where should our images be?
+  $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures'));
+  
+  // Does this dir exist? If not, make it
+  file_check_directory($directory, true);
+  if ($file = file_save_upload('path', $directory)) {
+    if ($old_image = db_fetch_object(db_query('SELECT tid FROM {term_image} WHERE tid = %d', $tid))) {
       // delete old image before saving the new one
       taxonomy_image_delete($old_image->tid);
     }
 
-    foreach ($fields as $field) {
-      $values[] = (string)db_escape_string($edit[$field]);
-    }
+    // Set the file path relative to the files directory
+    $file_path = str_replace(file_directory_path() .'/', '', $file->filepath);
 
-    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\')', $tid, $file_path);
+    cache_clear_all(); // @todo do we need to clear the whole cache?  Can't we just clear taxonomy_image bits of the cache?
 
     $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
+  else if (!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' => variable_get('file_directory_path', 'files'). '/' . variable_get('taxonomy_image_path', 'category_pictures'))));
   }
   else {
