--- zeitgeist.module.orig	Wed Jan 20 16:53:26 2010
+++ zeitgeist.module	Wed Feb 03 11:06:44 2010
@@ -89,7 +89,9 @@
 define('ZGVARTOPSPAN',     'zeitgeist_top_span');     // ZG span for the top queries block
 define('ZGVARTOPCACHE',    'zeitgeist_top_cache');    // Block minimum caching duration in minutes
 define('ZGVARHISTORY',     'zeitgeist_history');      // How long does the module keep its historical data.
+define('ZGVARDOMAIN',      'zeitgeist_domain');       // Should Zeitgeist filter search history by domain?
 define('ZGVARNOFOLLOW',    'zeitgeist_nofollow');     // Emit a rel=nofollow attribute in feature blocks
+define('ZGVARSEARCHPATH',  'zeitgeist_searchpath');   // User may specify search path if it's different than core search
 define('ZGVARPAGEHEIGHT',  'zeitgeist_page_height');  // Number of rows on the zeitgeist pager
 define('ZGVARRECORDEMPTY', 'zeitgeist_record_empty'); // Record empty searches
 define('ZGVARTYPE',        'zeitgeist_type');         // Display search type in "recent searches" blocks
@@ -102,7 +104,9 @@
 define('ZGDEFTOPCOUNT',    5);                        // Default for ZGVARTOPCOUNT
 define('ZGDEFTOPSPAN',     ZGSPANMONTH);              // Default for ZGVARTOPSPAN
 define('ZGDEFHISTORY',     0);                        // Default means forever
+define('ZGDEFDOMAIN',      0);                        // By default, don't filter by domain
 define('ZGDEFNOFOLLOW',    1);                        // Default is TRUE
+define('ZGDEFSEARCHPATH',  'search/node');            // Default is the core search path
 define('ZGDEFPAGEHEIGHT',  20);                       // Number of rows on the zeitgeist pager
 define('ZGDEFRECORDEMPTY', 1);                        // Defaut is TRUE: record empty queries
 define('ZGDEFTYPE',        ZGDISPLAYTYPEADMIN);       // Default is admin only
@@ -133,7 +137,9 @@
     case 'admin/help#zeitgeist':
       $ret = t('<p>zeitgeist records recent searches when they are performed.</p>
 <p>It also supplies a block listing the "n" latest searches, and a setting to define the value of "n".</p>
-<p>If Views 2.x is installed, Zeitgeist can be used as a Views data source.');
+<p>If the Domain module is installed, Zeitgeist can optionally filter blocks by domain.  e.g., the "Top 5" block will
+   show the top 5 searches that were performed in the current domain.</p>
+<p>If Views 2.x is installed, Zeitgeist can be used as a Views data source.</p>');
       break;
 
     default:
@@ -196,6 +202,18 @@
     '#size'               => 4, // this is good until 2037, should be enough for all 6.x needs.
     '#maxlength'          => 4,
     );
+    
+  if (module_exists('domain')) {
+    $options = array('1' => t('Enabled'), '0' => t('Disabled'));
+    $form[ZGVARDOMAIN]     = array
+      (
+      '#type'               => 'radios',
+      '#title'              => t('Filter blocks by domain'),
+      '#description'        => t('If enabled, then Zeitgeist blocks will show search data only for searches conducted in the current domain.'),
+      '#default_value'      => variable_get(ZGVARDOMAIN, ZGDEFDOMAIN),
+      '#options'            => $options,
+      );
+  }
 
   $form['zeitgeist-display'] = array
     (
@@ -334,6 +352,18 @@
     '#description'        => t('An attribute for the "a" (X)HTML alement, !attr is non-standard, but is recognized by several search engines, notably Google, MSN and Yahoo!',
       array('!attr' => '<code>rel="nofollow"</code>')),
     );
+  $form['advanced'][ZGVARSEARCHPATH] = array
+    (
+    '#type'               => 'textfield',
+    '#title'              => t('Base search path'),
+    '#default_value'      => variable_get(ZGVARSEARCHPATH, ZGDEFSEARCHPATH),
+    '#description'        => t('If you\'re using core search, leave this at the default value.  Otherwise, enter your site\'s base ' .
+                               'search path (no trailing slash), e.g. search/apachesolr_search.  This is used in the Zeitgeist ' .
+                               'blocks to create links for the top/latest search terms.'),
+    '#size'               => 40,
+    );
+
+    
   $form['advanced'][ZGVERSION] = array
     (
     '#value'         => '<p>'
@@ -453,13 +483,14 @@
           $subject = variable_get('zeitgeist_latest_title', t('Latest %count searches'));
           $subject = t($subject, array('%count' => $count));
           $now = time();
-          $cache = cache_get(ZGCIDLATEST);
+          $cache_id = _zeitgeist_cache_id(ZGCIDLATEST);
+          $cache = cache_get($cache_id);
           if (empty($cache) || ($now >= $cache->expire)) {
             $block = array(
               'subject' => $subject,
               'content' => theme('zeitgeist_block_latest', $count),
             );
-            cache_set(ZGCIDLATEST, $block, 'cache', $expires = $now + 60 * variable_get(ZGVARLATESTCACHE, 0));
+            cache_set($cache_id, $block, 'cache', $expires = $now + 60 * variable_get(ZGVARLATESTCACHE, 0));
           }
           else {
             $block = $cache->data;
@@ -470,13 +501,14 @@
           $subject = variable_get('zeitgeist_top_title', t('Top %count searches'));
           $subject = t($subject, array('%count' => $count));
           $now = time();
-          $cache = cache_get(ZGCIDTOP);
+          $cache_id = _zeitgeist_cache_id(ZGCIDTOP);
+          $cache = cache_get($cache_id);
           if (empty($cache) || ($now >= $cache->expire)) {
             $block = array(
               'subject' => $subject,
               'content' => theme('zeitgeist_block_top', $count),
             );
-            cache_set(ZGCIDTOP, $block, 'cache', $expires = $now + 60 * variable_get(ZGVARTOPCACHE, 0));
+            cache_set($cache_id, $block, 'cache', $expires = $now + 60 * variable_get(ZGVARTOPCACHE, 0));
           }
           else {
             $block = $cache->data;
@@ -489,6 +521,22 @@
 }
 
 /**
+ * Returns a cache_id to be used in calls to cache_get() and cache_set().
+ * If Domain filtering is enabled, then the cache_id must be unique to the domain, so
+ * this function appends the current domain id to $base_id 
+ * @param string $base_id The cache_id to be used if domain filtering is not enabled
+ * @return string
+ */
+function _zeitgeist_cache_id($base_id) {
+  if (module_exists('domain') && variable_get(ZGVARDOMAIN, ZGDEFDOMAIN)) {
+    global $_domain;
+    return $base_id . "_" . $_domain['domain_id'];
+  } else {
+    return $base_id;
+  }
+}
+
+/**
  * @param string $search
  * @param string $category
  * @param int $ts
@@ -502,12 +550,20 @@
   if (!$do_store || !_zeitgeist_validate($keys, 'save')) {
     return;
   }
+  
+  if (module_exists('domain')) {
+    //store the domain_id even if "Filter by Domain" is not enabled
+    global $_domain;
+    $domain_id = $_domain['domain_id'];
+  } else {
+    $domain_id = 'NULL';
+  }
 
   if (!isset($ts)) {
     $ts = time();
   }
-  $sq = 'INSERT INTO ' . ZGTABLE . ' (search, category, ts) '
-      . "VALUES ('%s', '%s', %d) ";
+  $sq = 'INSERT INTO ' . ZGTABLE . ' (search, category, ts, domain_id) '
+      . "VALUES ('%s', '%s', %d, $domain_id) "; //don't parameterize $domain_id because we don't want NULL cast to 0
   // No access control on logging, so no db_rewrite_sql().
   db_query($sq, $keys, $kind, $ts);
 }
@@ -668,9 +724,17 @@
       break;
   }
 
+  $filter_by_domain = (module_exists('domain') && variable_get(ZGVARDOMAIN, ZGDEFDOMAIN));
+  
   $sq = 'SELECT zg.search, zg.category, count(zg.ts) cnt '
       . 'FROM ' . ZGTABLE . ' zg '
-      . 'WHERE (zg.ts >= %d) AND( zg.ts < %d) ' ;
+      . 'WHERE (zg.ts >= %d) AND (zg.ts < %d) ' ;
+  
+  if ($filter_by_domain) {
+    global $_domain;
+    $domain_id = $_domain['domain_id'];
+    $sq .= "AND (zg.domain_id = $domain_id) ";
+  }
   if (isset($category)) {
     $sq .= "AND (zg.category = '%s') "
          . 'GROUP BY 1, 2 '
@@ -769,16 +833,18 @@
   unset($ar_date);
    * -- end of fragment
    */
-
+  
   $ar_stats = _zeitgeist_stats(ZGSPANMONTH, $ts, 'node', $count);
+  
   $ar_scores = array();
   $attr = variable_get(ZGVARNOFOLLOW, ZGDEFNOFOLLOW)
     ? array('rel' => 'nofollow')
     : NULL ;
   $attr = array_merge($attr, array('absolute' => TRUE));
+  $search_path = trim(variable_get(ZGVARSEARCHPATH, ZGDEFSEARCHPATH), " /");
   foreach ($ar_stats->scores as $score) {
     $ar_scores[] = l($score['search'] . " (${score['count']})",
-      "search/node/$score[search]", $attr);
+      "$search_path/$score[search]", $attr);
   }
   unset($ar_stats);
 
@@ -891,8 +957,9 @@
   $q = pager_query($sq, $height, 0, $sq_count);
   $ar = array();
   $cnt = 0;
+  $search_path = trim(variable_get(ZGVARSEARCHPATH, ZGDEFSEARCHPATH), " /");
   while ($o = db_fetch_object($q)) {
-    $ar[] = "$o->cnt - " . l($o->search, "search/node/$o->search");
+    $ar[] = "$o->cnt - " . l($o->search, "$search_path/$o->search");
     $cnt += $o->cnt;
   }
   $ret .= theme('item_list', $ar);
@@ -988,6 +1055,13 @@
  * @param array $form_values
  */
 function _zeitgeist_admin_settings_submit($form, &$form_state) {
+  if (module_exists('domain') && variable_get(ZGVARDOMAIN, ZGDEFDOMAIN)) {
+    $available_domains = domain_domains();
+    foreach ($available_domains as $domain) {
+      cache_clear_all(ZGCIDTOP . '_' . $domain['domain_id'], 'cache');
+      cache_clear_all(ZGCIDLATEST . '_' . $domain['domain_id'], 'cache');
+    }
+  }
   cache_clear_all(ZGCIDLATEST, 'cache');
   cache_clear_all(ZGCIDTOP, 'cache');
 }
@@ -1034,7 +1108,6 @@
   $kind = 'node';
   $form_id = $form_state['values']['form_id'];
   $keys = trim($form_state['values'][$form_id]);
-
   // dsm(t("Search box (@kind): @keys", array('@kind' => $kind, '@keys' => $keys)));
   _zeitgeist_store_search($keys, $kind);
 }
