--- notify.module	2008-02-04 07:46:25.000000000 +0100
+++ notify.module.new	2008-02-04 07:46:17.000000000 +0100
@@ -23,6 +23,7 @@ function notify_help($section) {
  */
 function notify_admin_settings() {
   $period = array(
+    60          => format_interval(60),
     900         => format_interval(900),
     1800        => format_interval(1800),
     3600        => format_interval(3600),
@@ -55,7 +56,21 @@ function notify_admin_settings() {
     '#default_value' => variable_get('notify_attempts', 5),
     '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20),
   );
-
+  
+  //vocabulary
+  $result = db_query('SELECT v.vid, v.name FROM {vocabulary} v');
+  $vocabulary = array();
+  while ( $voc = db_fetch_object($result) ) {
+    $vocabulary['vid_'.$voc->vid] = $voc->name;
+    $default[$voc->vid] = 0;
+  }
+  $form['notify_vocabulary'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Which vocabularies can the user choose from'),
+    '#default_value' => variable_get('notify_vocabulary',array()),
+    '#options' => array_map('t',$vocabulary),
+  );   
+  
   return system_settings_form($form);
 }
 
@@ -151,7 +166,7 @@ function notify_user_settings_form($uid 
     return;
   }
 
-  $result = db_query('SELECT u.uid, u.name, u.mail, n.status, n.node, n.teasers, n.comment FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE u.uid = %d AND u.status = 1', $account->uid);
+  $result = db_query('SELECT u.uid, u.name, u.mail, n.status, n.node, n.teasers, n.comment, n.taxonomy FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE u.uid = %d AND u.status = 1', $account->uid);
   $notify = db_fetch_object($result);
   $form = array();
   if ($notify->mail) {
@@ -162,6 +177,39 @@ function notify_user_settings_form($uid 
       '#options' =>  array(t('Disabled'), t('Enabled')),
       '#description' => t('Do you wish to receive periodic e-mails when new content is posted?'),
     );
+	
+  	//interests in specific pages
+    $allowed_vocs_withprefix = variable_get('notify_vocabulary',array());
+    foreach ( $allowed_vocs_withprefix as $key => $value ) {
+      $allowed_vocs[$key] = str_replace('vid_','',$value);
+    }
+    if ( count($allowed_vocs) > 0 ) {
+    	$result = db_query('SELECT v.vid, v.name FROM {vocabulary} v WHERE v.vid IN (%s)',implode(',',$allowed_vocs));
+    	$vocabulary = array();
+    	while ( $voc = db_fetch_object($result) ) {
+    		$vocabulary[$voc->vid] = $voc->name;
+    	}
+  	
+    	if ( count($vocabulary) > 0 ) {
+        $default = unserialize($notify->taxonomy);
+    		$form['taxonomy'] = array('#type' => 'fieldset', '#title' => t('Interests'), '#tree' => true);
+    		foreach ( $vocabulary as $vid => $name ) {
+          //options
+          $options = array();
+          $result = db_query('SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d ORDER BY t.weight', $vid);
+          while ( $opt = db_fetch_object($result) ) {
+            $options[$opt->tid] = $opt->name;
+          }
+          //form
+          $form['taxonomy'][$vid] = array('#type' => 'checkboxes',
+            '#title' => t($name),
+            '#default_value' => $default[$vid],
+            '#options' => array_map('t',$options),
+            '#description' => t('Check your interests in '.$name.'.'),
+          );
+    		}
+    	}
+    }
 
     $form['notify_page_detailed'] = array('#type' => 'fieldset', '#title' => t('Detailed settings'));
     $form['notify_page_detailed']['node'] = array('#type' => 'radios',
@@ -193,8 +241,9 @@ function notify_user_settings_form($uid 
 }
 
 function notify_user_settings_form_submit($form_id, $form_values) {
+  //print_r($form_values);
   db_query('DELETE FROM {notify} WHERE uid = %d', $form_values['uid']);
-  db_query('INSERT INTO {notify} (uid, status, node, teasers, comment) VALUES (%d, %d, %d, %d, %d)', $form_values['uid'], $form_values['status'], $form_values['node'], $form_values['teasers'], $form_values['comment']);
+  db_query('INSERT INTO {notify} (uid, status, node, teasers, comment, taxonomy) VALUES (%d, %d, %d, %d, %d, \'%s\')', $form_values['uid'], $form_values['status'], $form_values['node'], $form_values['teasers'], $form_values['comment'], serialize($form_values['taxonomy']));
   drupal_set_message(t('Notify settings saved.'));
 }
 
@@ -316,6 +365,49 @@ function _notify_content($node, $notify)
 }
 
 /**
+ * Check if user has checked the taxonomy term for the specific node
+ */
+function _notify_checktaxonomy($vocabularies,$nid,$interests) {
+  $interested = false;
+  
+  // Get tids linked to the node
+  $tresult = db_query('SELECT t.tid FROM {term_node} t WHERE t.nid = %d', $nid);
+  $terms = array();
+  while ( $term = db_fetch_object($tresult) ) {
+    $terms[] = $term->tid;
+  }
+  
+  // check interests in node only if node has terms specified
+  if (count($terms)) {
+	  // If no interests checked by user => always include nodes
+	  // Else check only the categories where one or more terms are checked
+	  $sum = 0;
+	  foreach ( $vocabularies as $vid ) {
+	    if ( array_key_exists($vid,$interests) ) {
+	      $sum += array_sum($interests[$vid]);
+	    }
+	  }
+	  
+	  if ( $sum > 0 ) {
+	    //check per category
+	    foreach ( $vocabularies as $vid ) {
+	      if ( array_key_exists($vid,$interests) && array_sum($interests[$vid]) > 0  && count(array_intersect($interests[$vid],$terms)) ) {
+	        $interested = true;
+	      }
+	    }
+	  }
+	  else {
+	    $interested = true;
+	  }
+  }
+  else {
+	    $interested = true;
+  }
+  
+  return $interested;
+}
+
+/**
  * Helper function to send the notification email.
  * 
  * TODO: Needs some cleanup and themability.
@@ -333,10 +425,24 @@ function _notify_send() {
   // Fetch users with notify enabled
   $uresult = db_query('SELECT u.uid, u.name, u.mail, n.status, n.node, n.teasers, n.comment FROM {notify} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND u.status = 1 AND n.attempts <= %d', variable_get('notify_attempts', 5));
 
+  // Check which vocabularies admin has selected to notify (if empty all nodes will be send)
+  $vocabularies_withprefix = variable_get('notify_vocabulary',array());
+  $vocabularies = array();
+  foreach ( $vocabularies_withprefix as $key => $value ) {
+    $vocabularies[$key] = str_replace('vid_','',$value); 
+  }
+  
   while ($user = db_fetch_object($uresult)) {
     // Switch current user to this account to use node_access functions, etc.
     _notify_switch_user($user->uid);
 
+    // Get all interests of the user
+    $iresult = db_query('SELECT n.taxonomy FROM {notify} n WHERE n.uid = %d', $user->uid);
+    $interests = array();
+    while ( $interest = db_fetch_object($iresult)) {
+      $interests = unserialize($interest->taxonomy);
+    }
+    
     // Fetch all new nodes and 'load' it to get proper body, etc.
     $nresult = db_query(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE (n.status = 1 OR n.moderate = 1) AND n.created > %d AND n.created <= %d ORDER BY n.created'), $period, time());
     $nodes = array();
@@ -357,13 +463,17 @@ function _notify_send() {
     // Write new node content to e-mail if user has permissions and nodes are
     // ready to be sent.
     if ($user->node && user_access('access content') && count($nodes)) {
-
       $node_count = 0;
       foreach ($nodes as $node) {
         // Skip to next if this user is NOT allowed to view this node.
         if (!node_access('view', $node)) {
           continue;
         }
+        
+        // When vocabularies specified by admin, check if user is interested in some terms of it
+        if ( array_sum($vocabularies) > 0 && !_notify_checktaxonomy($vocabularies,$node->nid,$interests)) {
+          continue;
+        }
 
         // TODO: Add functionality to hook into moderation modules?
         if ($node->status == 1) {
