Index: image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v
retrieving revision 1.161
diff -u -r1.161 image.module
--- image.module	11 Aug 2005 15:55:42 -0000	1.161
+++ image.module	14 Aug 2005 14:56:52 -0000
@@ -207,29 +207,56 @@
  *
  * Offers 2 blocks: latest image and random image
  */
-function image_block($op, $delta = 0) {
+function image_block($op, $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
       $block[0]['info'] = t('Latest image');
       $block[1]['info'] = t('Random image');
 
       return $block;
+
     case 'view':
       if (user_access('access content')) {
         switch($delta) {
           case 0:
-            $images = image_get_latest();
-            $block['subject'] = t('Latest image');
-            $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
+            $tid = variable_get('image_block_latest_tid', 0);
+            if ($images = image_get_latest(1, $tid)) {
+              $block['subject'] = t('Latest image');
+              $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
+            }
             break;
           case 1:
-            $images = image_get_random();
-            $block['subject'] = t('Random image');
-            $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
+            $tid = variable_get('image_block_random_tid', 0);
+            if ($images = image_get_random(1, $tid)) {
+              $block['subject'] = t('Random image');
+              $block['content'] = l(image_display($images[0], 'thumbnail'), 'node/'.$images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
+            }
             break;
         }
       }
       return $block;
+
+    case 'configure':
+      switch ($delta) {
+        case 0:
+          $form = taxonomy_form(_image_get_vid(), variable_get('image_block_latest_tid', 0), t('Display only images from selected image gallery.'), 'image_block_latest_tid');
+          break;
+        case 1:
+          $form = taxonomy_form(_image_get_vid(), variable_get('image_block_random_tid', 0), t('Display only images from selected image gallery.'), 'image_block_random_tid');
+          break;
+      }
+      return $form;
+
+    case 'save':
+      switch ($delta) {
+        case 0:
+          variable_set('image_block_latest_tid', $edit['image_block_latest_tid'][0]);
+          break;
+        case 1:
+          variable_set('image_block_random_tid', $edit['image_block_random_tid'][0]);
+          break;
+      }
+      break;
   }
 }
 
@@ -561,7 +588,12 @@
  * Fetch a random N image(s) - optionally from a given term.
  */
 function image_get_random($count = 1, $tid = 0) {
-  $result = db_query_range(db_rewrite_sql("SELECT nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY RAND()"), 0, $count);
+  if ($tid != 0) {
+    $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY RAND()"), $tid, 0, $count);
+  }
+  else {
+    $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY RAND()"), 0, $count);
+  }
   $output = array();
   while ($nid = db_fetch_object($result)) {
     $output[] = node_load(array('nid' => $nid->nid));
@@ -570,10 +602,15 @@
 }
 
 /**
- * Fetch the latest N image(s).
+ * Fetch the latest N image(s) - optionally from a given term.
  */
 function image_get_latest($count = 1, $tid = 0) {
-  $result = db_query_range(db_rewrite_sql("SELECT nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY changed DESC"), 0, $count);
+  if ($tid != 0) {
+    $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY n.changed DESC"), $tid, 0, $count);
+  }
+  else {
+    $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY changed DESC"), 0, $count);
+  }
   $output = array();
   while ($nid = db_fetch_object($result)) {
     $output[] = node_load(array('nid' => $nid->nid));
