Index: related_nodes.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/related_nodes/related_nodes.module,v
retrieving revision 1.6
diff -u -p -r1.6 related_nodes.module
--- related_nodes.module	1 Jun 2007 04:38:01 -0000	1.6
+++ related_nodes.module	14 Jun 2007 16:09:10 -0000
@@ -5,7 +5,7 @@
  * @file
  * Displays a sidebar block of nodes related by taxonomy terms.
  */
-
+// TODO: reset cache when admin settings change (ex. number of nodes)
 /**
  * Implementation of hook_block();
  *
@@ -56,17 +56,17 @@ function related_nodes_block($op = 'list
     case('save'):
       variable_set('related_nodes_block_num', $edit['related_nodes_block_num']);
       variable_set('related_nodes_vocabularies', $edit['related_nodes_vocabularies']);
-      variable_set('related_nodes_type', explode(',',$edit['related_nodes_type']));
+      variable_set('related_nodes_type', $edit['related_nodes_type']);
       break;
 
     case('view'):
       if (arg(0) == 'node' && is_numeric(arg(1))) {
         $nid = arg(1);
-        $count = variable_get('related_nodes_count', 5);
+        $count = variable_get('related_nodes_block_num', 5);
         $vocabularies = variable_get('related_nodes_vocabularies', array());
         $node_type = variable_get('related_nodes_type', '');
 
-        if (count($numTags)) {
+        if ($count) {
           $nodes = related_nodes_get_nodes($nid, $count, $vocabularies, $node_type);
 
           if (count($nodes)) {
@@ -80,29 +80,37 @@ function related_nodes_block($op = 'list
   }
 }
 
-function related_nodes_get_nodes($nid, $numNodes = 5, $vocabularies = array(), $node_type = '', $reset = FALSE) {
+function related_nodes_get_nodes($nid, $numnodes = 5, $vocabularies = array(), $node_type = '', $reset = FALSE) {
   static $nodes;
+watchdog('related nodes', 'TODO: delete reset');
+$reset = TRUE;
+
   if (empty($nodes[$nid]) || $reset) {
     if (!$reset && (($cache = cache_get('related_nodes:nodelist:'. $nid)) && !empty($cache->data))) {
       $nodes[$nid] = unserialize($cache->data);
     }
     else {
+      $where = '';
+      $join = '';
       if (count($vocabularies)) {
-        $join = 'INNER JOIN {term_data} td ON tn.tid = tn.tid ';
-        $where = ' AND td.vid IN ('. implode(',', $vocabularies) .') ';
+        $join = 'INNER JOIN {term_data} td ON td.tid = tn.tid ';
+        $where .= ' AND td.vid IN ('. implode(',', $vocabularies) .') ';
       }
 
       if (!empty($node_type)) {
-        $where .= " AND n.type = '". $node_type ."'";
+        $where .= " AND n.type = '". $node_type ."' ";
       }
 
-      $sql = 'SELECT n.nid AS nid, n.title as title, COUNT(tn3.tid) c FROM {term_node} tn ';
-      $sql .=  $join;
-      $sql .= 'INNER JOIN {term_node} tn2 ON tn.tid = tn2.tid INNER JOIN {term_node} tn3 ON tn2.nid = tn3.nid INNER JOIN {node} n ON tn3.nid = n.nid ';
-      $sql .= 'WHERE n.status = 1 AND tn.nid = %d AND tn2.nid <> %d '. $where .' AND tn.nid <> tn3.nid ';
-      $sql .= ' GROUP BY n.nid ORDER BY c DESC';
-
-      $results = db_query($sql, $nid, $nid);
+      $sql = 'SELECT n.nid AS nid, n.title AS title, COUNT(tn.nid) AS c ';
+      $sql .= 'FROM {node} n ';
+      $sql .= 'INNER JOIN term_node tn ON tn.nid = n.nid ';
+      $sql .= $join;
+      $sql .= 'INNER JOIN term_node tn2 ON tn2.tid = td.tid ';
+      $sql .= 'WHERE n.status = 1 ';
+      $sql .= 'AND tn2.nid = %d AND tn.nid <> %d '. $where; 
+      $sql .= 'GROUP BY n.nid, n.title ';
+      $sql .= 'ORDER BY c DESC';
+      $results = db_query_range(db_rewrite_sql($sql), $nid, $nid, 0, $numnodes);
       $nodes = array();
 
       while ($node = db_fetch_object($results)) {
