From a51de32578dce27b4b6a28aa793ed525ca5484f0 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <gordon@heydon.com.au>
Date: Fri, 21 Oct 2011 15:15:11 +1100
Subject: [PATCH] Make change to all similar nodes to be exclusive as well as
 allow anything.

---
 .../similarterms_handler_argument_node_nid.inc     |   37 +++++++++++++++-----
 .../views/similarterms_handler_sort_similar.inc    |   20 ++++++----
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/similarterms/views/similarterms_handler_argument_node_nid.inc b/similarterms/views/similarterms_handler_argument_node_nid.inc
index 76971a3..6e04d2b 100644
--- a/similarterms/views/similarterms_handler_argument_node_nid.inc
+++ b/similarterms/views/similarterms_handler_argument_node_nid.inc
@@ -30,6 +30,7 @@ class similarterms_handler_argument_node_nid extends views_handler_argument_nume
 
     $options['vocabularies'] = array('default' => array());
     $options['include_args'] = array('default' => FALSE);
+    $options['join_type'] = 'inner';
     
     return $options;
   }
@@ -52,6 +53,15 @@ class similarterms_handler_argument_node_nid extends views_handler_argument_nume
       '#default_value' => empty($this->options['vocabularies']) ? array() : $this->options['vocabularies'],
     );
     
+    $form['join_type'] = array(
+      '#type' => 'radios',
+      '#title' => t('Join type'),
+      '#default_value' => $this->options['join_type'],
+      '#options' => array(
+        'INNER' => t('Inner'),
+        'LEFT' => t('Left'),
+      ),
+    );
     $form['include_args'] = array(
       '#type' => 'checkbox',
       '#title' => t('Include argument node(s) in results'),
@@ -142,15 +152,25 @@ class similarterms_handler_argument_node_nid extends views_handler_argument_nume
     $tids = $this->tids;
   
     $v = $this->query->add_table('term_node');
-              
-    if (count($tids) == 1) {
-      $this->query->add_where(0, "term_node.tid = %d", $tids);
+    
+    if ($this->options['join_type'] == 'INNER') {      
+      if (count($tids) == 1) {
+        $this->query->add_where(0, "term_node.tid = %d", $tids);
+      }
+      elseif (count($tids) > 1) {
+        $placeholders = implode(', ', array_fill(0, count($tids), '%d'));
+        $this->query->add_where(0, "term_node.tid IN ($placeholders)", $tids);
+      }
     }
-    elseif (count($tids) > 1) {
-      $placeholders = implode(', ', array_fill(0, count($tids), '%d'));
-      $this->query->add_where(0, "term_node.tid IN ($placeholders)", $tids);
+    else {
+      if (count($tids) == 1) {
+        $this->query->add_field(NULL, "(SELECT COUNT(tn.tid) FROM {term_node} tn WHERE tn.nid = node.nid AND tn.tid = " . reset($tids) . ")", 'term_count');
+      }
+      else {
+        $this->query->add_field(NULL, "(SELECT COUNT(tn.tid) FROM {term_node} tn WHERE tn.nid = node.nid AND tn.tid IN (" . implode(',', $tids) . "))", 'term_count');
+      }
     }
-          
+
     // exclude the current node(s)
     if (empty($this->options['include_args'])) {
       if (count($this->value) > 1) {
@@ -160,8 +180,7 @@ class similarterms_handler_argument_node_nid extends views_handler_argument_nume
       else {
         $this->query->add_where(0, 'node.nid != %d', $this->value[0]);
       }
-    }
-    
+    }    
   }
   
 }
\ No newline at end of file
diff --git a/similarterms/views/similarterms_handler_sort_similar.inc b/similarterms/views/similarterms_handler_sort_similar.inc
index 0c3e8a5..a59a926 100644
--- a/similarterms/views/similarterms_handler_sort_similar.inc
+++ b/similarterms/views/similarterms_handler_sort_similar.inc
@@ -12,16 +12,20 @@ class similarterms_handler_sort_similar extends views_handler_sort {
   }
 
   function query() {
-      
-    // add function to count nid occurrences based on grouping 
-    $this->query->add_field(NULL, 'COUNT(node.nid)', 'node_count', array('aggregate' => TRUE));
-        
-    // sort 'em
-    $this->query->add_orderby(NULL, NULL, $this->options['order'], 'node_count');
     
-    // group 'em
-    $this->query->add_groupby('nid');
+    if (isset($this->query->fields['term_count'])) {
+      $this->query->add_orderby(NULL, NULL, $this->options['order'], 'term_count');
+    }
+    else {
+      // add function to count nid occurrences based on grouping 
+      $this->query->add_field(NULL, 'COUNT(node.nid)', 'node_count', array('aggregate' => TRUE));
         
+      // sort 'em
+      $this->query->add_orderby(NULL, NULL, $this->options['order'], 'node_count');
+    
+      // group 'em
+      $this->query->add_groupby('nid');
+    }  
   }
 
 }
-- 
1.7.5.4

