--- subscriptions.module.1	Mon Dec 20 08:16:02 2004
+++ subscriptions.module	Sun Dec 19 17:30:51 2004
@@ -86,6 +86,12 @@
     $account = $user;
   }
   if (is_null($subscriptions[$account->uid])) {
+    // query string for comment subscriptions
+    $queryc = "SELECT td.tid, td.name, c.subject, c.cid, c.nid, n.nid, n.title, s.stype, s.sid FROM ";
+    $queryc .= "(({subscriptions} s LEFT JOIN {comments} c ON c.cid = s.sid LEFT JOIN {node} n  ON n.nid = c.nid) ";
+    $queryc .= "LEFT JOIN {term_node} tn ON tn.nid = n.nid) ";
+    $queryc .= "LEFT JOIN {term_data} td ON td.tid = tn.tid ";
+    $queryc .= "WHERE n.status = 1 AND s.uid = $account->uid AND s.stype = \"comm\"";
     // query string for node subscriptions
     $queryn = 'SELECT td.tid, td.name, n.nid, n.title, s.stype, s.sid FROM ';
     $queryn .= '(({subscriptions} s LEFT JOIN {node} n ON n.nid = s.sid) ';
@@ -100,18 +106,24 @@
     $queryt .= 'SELECT td.tid, td.name FROM ';
     $queryt .= '{subscriptions} s INNER JOIN {term_data} td ON td.tid = s.sid ';
     $queryt .= 'WHERE s.uid = %d AND s.stype = \'taxa\'';
+    $resultc = db_query($queryc);
     $resultn = db_query($queryn, $account->uid);
     $resultb = db_query($queryb, $account->uid);
     $resultt = db_query($queryt, $account->uid);
 
-    $subscriptions[$account->uid]['node'] = $subscriptions[$account->uid]['taxa'] = $subscriptions[$account->uid]['blog'] = array();
+
+    $subscriptions[$account->uid]['node'] = $subscriptions[$account->uid]['comm'] = $subscriptions[$account->uid]['taxa'] = $subscriptions[$account->uid]['blog'] = array();
+  
+    while ($csub = db_fetch_object($resultc)) {
+      $subscriptions[$account->uid]['comm'][$csub->cid] = $csub;
+    }
     while ($nsub = db_fetch_object($resultn)) {
       $subscriptions[$account->uid]['node'][$nsub->nid] = $nsub;
     }
     while ($bsub = db_fetch_object($resultb)) {
       $subscriptions[$account->uid]['blog'][$bsub->uid] = $bsub;
     }
-    while ($tsub = db_fetch_object($resultn)) {
+    while ($tsub = db_fetch_object($resultt)) {
       $subscriptions[$account->uid]['taxa'][$tsub->tid] = $tsub;
     }
   }
@@ -121,7 +133,12 @@
 // returns a summary of all subscriptions
 function subscriptions_get_summary() {
   static $subssumm;
-
+  // query string for comment subscriptions
+  $queryc = 'SELECT s.sid, c.cid, c.subject, c.nid, COUNT(*) as ncount FROM ';
+  $queryc = '{subscriptions} s INNER JOIN {comments} c ON s.sid = c.cid ';
+  $queryc = 'WHERE s.stype = "comm" ';
+  $queryc = 'GROUP BY s.sid ';
+  $queryc = 'ORDER BY s.sid ';
   // query string for node subscriptions
   $queryn = 'SELECT n.nid, n.title, s.sid, COUNT(*) as ncount FROM ';
   $queryn .= '{subscriptions} s INNER JOIN {node} n ON n.nid = s.sid ';
@@ -141,11 +158,16 @@
   $queryt .= 'GROUP BY s.sid ';
   $queryt .= 'ORDER BY s.sid ';
 
+  $resultc = db_query($queryc);
   $resultn = db_query($queryn);
   $resultb = db_query($queryb);
   $resultt = db_query($queryt);
 
-  $subssumm['node'] = $subssumm['taxa'] = $subssumm['blog'] = array();
+  $subssumm['comm'] = $subssumm['node'] = $subssumm['taxa'] = $subssumm['blog'] = array();
+  while ($csub = db_fetch_object($resultc)) {
+    $subscriptions['comm'][$csub->cid][$csub->nid] = $csub;
+  }
+  print_r ($subscriptions);
   while ($nsub = db_fetch_object($resultn)) {
     $subssumm['node'][$nsub->nid] = $nsub;
   }
@@ -170,10 +192,18 @@
 }
 
 // get e-mail vars
-function subscriptions_mailvars($sid, $ssid, $uid, $stype, $strsent) {
+function subscriptions_mailvars($sid, $ssid, $pid, $uid, $stype, $strsent) {
   global $base_url;
   global $locale;
   $initial_locale = $locale;
+  if ($stype == 'comm') {
+    $result = db_query("SELECT subject FROM {comments} WHERE cid = $pid");
+    $subj = db_result($result);
+    $result = db_query("SELECT u.uid, u.name, u.mail FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE s.sid = $pid AND s.stype = \"comm\"");
+    $strtype = "thread";
+    $nid = $sid;
+    $cid = "comment-$ssid";
+  }
   if (function_exists('locale')) {
     $languages = locale_supported_languages();
     $languages = $languages['name'];
@@ -262,12 +292,41 @@
 function subscriptions_comment($op, $comment) {
   global $user;
   $strsent = '';  // tracks whether or not a particular user has already been notified by another subsciption
+  if (is_array($comment)) {
+    $status = $comment['status'];
+  }
+  else {
+    $status = $comment->status;
+  }
   if ($comment->status == 0 && ($op == 'insert' || $op == 'update')) {
     $nid = $comment['nid'];
     $cid = $comment['cid'];
-    subscriptions_mailvars($nid, $cid, $user->uid, 'node', $strsent);
-    subscriptions_autosubscribe($user->uid, $nid);
+    $pid = $comment['pid'];
+    if( $comment[subscriptions_subscribe] ) {
+      subscriptions_add($cid, $user->uid, 'comm');
+    }
+    if( $comment['pid'] ) {
+      $type = 'comm';
+    } else {
+      $type = 'node';
+      $result = db_query("SELECT c.nid FROM {comments} c WHERE c.cid = $cid");
+      $nid = db_result($result);
+    }
+    subscriptions_mailvars ($nid, $cid, $pid, $user->uid, $type, $strsent);
   }
+  if($op == 'form post') {
+    $edit = $comment;
+    // subscribe checkbox
+    if( user_access('maintain subscriptions') ) {
+      if( !isset($edit[subscriptions_subscribe]) ) {
+        $checkbox_state = -1;
+      } else {
+        $checkbox_state = $edit[subscriptions_subscribe];
+      }
+      $val = subscriptions_get_checkbox_state ($checkbox_state, $comment);
+      return form_item(t("Subscribe"), form_checkbox(t("Receive notification of replies to this comment."), "subscriptions_subscribe", 1, $val));
+    }
+  }    
 }
 
 /**
@@ -277,16 +336,25 @@
   global $user;
   $strsent = '!';
   switch ($op) {
+    case 'update':
+    if(!$node->subscriptions_subscribe) {
+      subscriptions_del($node->nid, $user->uid, 'node');
+    }
+    if($node->subscriptions_subscribe) {
+      subscriptions_add($node->nid, $user->uid, 'node');
+    }
+    break;
+
     case 'insert':
       if ($node->status) {
         if (isset($node->taxonomy)) {
           foreach ($node->taxonomy as $tid){
-            $strsent = $strsent . subscriptions_mailvars($tid, $node->nid, $user->uid, 'taxa', $strsent);
+            $strsent = $strsent . subscriptions_mailvars ($tid, $node->nid, $pid, $user->uid, "taxa", $strsent);
           }
         }
-        $strsent = $strsent . subscriptions_mailvars($node->nid, 0, $user->uid, 'node', $strsent);
+        $strsent = $strsent . subscriptions_mailvars ($node->nid, 0, $pid, $user->uid, "node", $strsent);
         if ($node->type == 'blog') {
-          $strsent = $strsent . subscriptions_mailvars($node->uid, $node->nid, $user->uid, 'blog', $strsent);
+          $strsent = $strsent . subscriptions_mailvars ($node->uid, $node->nid, $pid, $user->uid, "blog", $strsent);
         }
         subscriptions_autosubscribe($user->uid, $node->nid);
       }
@@ -294,18 +362,36 @@
         if ($node->subscriptions_subscribe) {
           subscriptions_add($node->nid, $user->uid, 'node');
         }
-        user_save($user, array('subscriptions_subscribe' => $node->subscriptions_subscribe));
+        //user_save($user, array('subscriptions_subscribe' => $node->subscriptions_subscribe));
       }
       break;
     case 'form post':
-      if (!$user->subscriptions_auto) {
-        $allsubs = subscriptions_get_user();
-        $val = isset($node->subscriptions_subscribe) ? $node->subscriptions_subscribe : $allsubs['node'][$node->nid] ? 1 : $user->subscriptions_subscribe;
+      if (user_access('maintain subscriptions') && $user->uid) {
+        if (!isset($node->subscriptions_subscribe)) {
+          $checkbox_state = -1;
+        } else {
+          $checkox_state = $node->subscriptions_subscribe;
+        }
+        $val = subscriptions_get_checkbox_state($checkbox_state, $node);
         return form_item(t('Subscribe'), form_checkbox(t('Receive notification of replies to this %name.', array('%name' => node_invoke($node, 'node_name'))), 'subscriptions_subscribe', 1, $val));
       }
-      break;
   }
 }
+function subscriptions_get_checkbox_state ($checkbox_state, $node) {
+  global $user;
+  $val = 0;
+  $allsubs = subscriptions_get_user();
+  if( $user->subscriptions_auto && ($checkbox_state == -1 ) ) {
+    $val = 1;
+  }
+  if( $checkbox_state == 1) {
+    $val = 1;
+  }
+  if(array_key_exists($node->nid, $allsubs['node'])) {
+    $val = 1;
+  }
+  return $val;
+}
 
 /**
  * Implementation of hook_menu().
@@ -347,6 +433,15 @@
       $links[] = l(t('subscribe post'), 'subscriptions/add/node/'. $node->nid, array('title' => t('Receive an e-mail whenever a comment is posted to this %n.', array('%n' => $name))));
     }
   }
+  if (user_access('maintain subscriptions') && $type == 'comment' ) {
+    $subscriptions = subscriptions_get_user();
+    if( isset($subscriptions['comm'][$node->cid])) {
+      $links[] = l(t('unsubscribe'), "subscriptions/del/comm/$node->nid/$node->cid", array('title' => t('Stop receiving an email whenever someone makes a new post to this comment.'))); 
+    }
+    else {
+      $links[] = l(t('subscribe'), "subscriptions/add/comm/$node->nid/$node->cid", array('title' => t('Receive an email whenever someone makes a new post to this comment.')));
+    }
+  }
   return $links ? $links : array();
 }
 
@@ -376,6 +471,10 @@
   return $link;
 }
 
+function subscriptions_del($sid, $uid, $stype) {
+  db_query("DELETE FROM {subscriptions} WHERE sid = $sid AND uid = $uid AND stype = '$stype'");
+}
+
 function subscriptions_add($sid, $uid, $stype) {
   db_query('INSERT INTO {subscriptions} ( sid , uid, stype )  VALUES (%d , %d, \'%s\')', $sid, $uid, $stype);
 }
@@ -385,21 +484,34 @@
   global $user;
   $subscribed = false;
   $uid = $user->uid;
-  if (!arg(2)) {
-    $sid = arg(1);
-    $nid = $sid;
-    $op = arg(0);
-  }
-  else {
-    $op = arg(1);
-    $stype = arg(2);
-    $sid = arg(3);
-    $nid = arg(4);
-    if ($stype == 'node') {
+  if( arg(1) != 'onauto' && arg(1) != 'offauto' ) {
+    if (! arg(2)) {
+      $sid = arg(1);
       $nid = $sid;
     }
+    else {
+      $op = arg(1);
+      $stype = arg(2);
+      $sid = arg(3);
+      if ($stype == 'node') {
+        $nid = $sid;
+      }
+      if ($stype == 'comm') {
+        $sid = arg(4);
+        $cid = $sid;
+        $nid = arg(3);
+      }
+    }
+  } else {
+    $op = arg(1);
   }
 
+  if( $op == 'onauto' ) {
+    user_save($user, array('subscriptions_auto' => '1'));
+  }
+  if( $op == 'offauto' ) {
+    user_save($user, array('subscriptions_auto' => '0'));
+  }
   switch ($op) {
     // inserts a new subscription into the subscriptions_nodes table
     case 'add':
@@ -409,9 +521,14 @@
         if (arg(2) == 'blog') {
           $message .= l(t('Return to the blog'), 'blog/'. $sid) .' | ';
         }
-        else {
+        else if (arg(2) == 'node') {
           $message .= l(t('Return to the thread'), 'node/'. $nid) .' | ';
         }
+        else if( arg(2) == 'comm') {
+          $message .= l(t('Return to the thread'), "node/$nid/#comment-$cid") . " | ";
+        } else {
+          $message .= l(t("Return to the thread"), "node/$nid") . " | ";
+        }
       }
       $message .= l(t('Return to the subscriptions'), 'subscriptions') .'<br />';
       print theme('page', $message, t('Subscription Activated'));
@@ -419,10 +536,14 @@
 
     // removes a subscription from the subscriptions_nodes table
     case 'del':
-      db_query('DELETE FROM {subscriptions} WHERE sid = %d AND uid = %d AND stype = \'%s\'', $sid, $uid, $stype);
+      db_query("DELETE FROM {subscriptions} WHERE sid = $sid AND uid = $uid AND stype = '$stype'");
       $message = t('<p>Your subscription was deactivated.</p>');
       if (!(arg(2) == 'taxa')) {
-        $message .= l(t('Return to the thread'), 'node/'. $nid) .' | ';
+        if( $stype == "comm" ) {
+          $message .= l(t("Return to the thread"), "node/$nid/#comment-$cid") . " | ";
+        } else {
+          $message .= l(t('Return to the thread'), 'node/'. $nid) .' | ';
+        }
       }
       $message .= l(t('Return to the subscriptions'), 'subscriptions') .'<br />';
       print theme('page', $message, t('Subscription Deactivated'));
@@ -442,12 +563,7 @@
       }
       // traverse the taxonomy tree
       $taxa = subscriptions_get_taxa_count();
-      // omit undesired vocabularies from listing
       $vocabularies = taxonomy_get_vocabularies();
-      $omits = variable_get('subscriptions_omitted_taxa', array());
-      foreach ($omits as $omit) {
-        unset($vocabularies[$omit]);
-      }
       foreach ($vocabularies as $vocab) {
         $tree = taxonomy_get_tree($vocab->vid);
         foreach ($tree as $term) {
@@ -456,7 +572,7 @@
       }
       // concatentate the arrays
       $headers = array(t('type'), t('title'), t('subscribers'));
-      $subrows = array_merge($subrowsn, $subrowsb, $subrowst);
+      $subrows = array_merge($subrowsn, $subrowsb, $subrowst, $subrowsc);
       // assemble output
       if (!$subrows) {
         $message .= t('<p>No threads or categories are currently subscribed.</p>');
@@ -464,18 +580,39 @@
       else {
         $message .= theme('table', $headers, $subrows, array('id' => 'subscriptions'));
       }
-      print theme('page', $message, t('Subscriptions Summary'));
+      $message .= l(t('Return to the subscriptions'), 'subscriptions') .'<br />';
+      print theme('page', $message, t('Subscription Summary'));
       break;
 
     // determines the user's subscription status and displays the right option to change it
     default:
       // get all subscriptions and write to table rows
+      if(  $op == 'onauto' || $op == 'offauto' ) {
+        if( $op == 'onauto' ) {
+          $message .= _subscriptions_autosubscribe_form('off');
+        }
+        if( $op == 'offauto' ) {
+          $message .= _subscriptions_autosubscribe_form('on');
+        }
+      } else {
+        if( $user->subscriptions_auto ) {
+          $message .= _subscriptions_autosubscribe_form('off');
+        }
+        if( !($user->subscriptions_auto) ) {
+          $message .= _subscriptions_autosubscribe_form('on');
+        }
+      }
       $subscriptions = subscriptions_get_user();
       // build node rows
       foreach ($subscriptions['node'] as $nsub) {
         $subrowsn[] = array(t('thread'), l($nsub->title, 'node/'. $nsub->nid), '['. l(t('unsubscribe'), 'subscriptions/del/node/'. $nsub->nid, array('title' => t('Unsubscribe from this thread.'))) .']');
       }
-      // build blog rows
+      // build comment rows
+      foreach ($subscriptions['comm'] as $csub) {  
+        $subrowsc[] = array(t('comment'), l($csub->subject, 'node/' . $csub->nid . '#comment-' . $csub->cid), '[' . l(t('unsubscribe'), 'subscriptions/del/comm/' . $csub->nid . '/' . $csub->cid, array('title' => t('Unsubscribe from this comment.'))) . ']');
+      }
+
+    // build blog rows
       foreach ($subscriptions['blog'] as $bsub) {
         $subrowsb[] = array(t('blog'), l($bsub->name, 'blog/'. $bsub->uid), '['. l(t('unsubscribe'), 'subscriptions/del/blog/'. $bsub->uid, array('title' => t('Unsubscribe from this user\'s blog.'))) .']');
       }
@@ -495,8 +632,8 @@
         }
       }
       // concatentate the arrays
-      $headers = array(t('type'), t('title'), t('operations'));
-      $subrows = array_merge($subrowsn, $subrowsb, $subrowst);
+      $headers = array(t('type'), t('title'), t('operation'));
+      $subrows = array_merge($subrowsn, $subrowsb, $subrowst, $subrowsc);
       if (!$subrows) {
         $message .= t('<p>You are not currently subscribed to any active threads or categories.</p>');
       }
@@ -505,6 +642,21 @@
       }
       print theme('page', $message);
       break;
+  }
+}
+
+function _subscriptions_autosubscribe_form ($status) {
+  if ($status == 'on') {
+    $message = '<br />Autosubscribe is currently <b>OFF.</b>';
+    $form = form_submit('Turn Autosubscribe On', 'autosubscribeon');
+    $message .= form( $form, 'post', 'subscriptions/onauto' ) . "Clicking this button will check the 'Subscribe' checkbox for you when you submit content.  You will be required to uncheck the checkbox manually if you do not wish to subscribe.<br /><br /><br />";         
+     return $message;
+  }
+  else {
+    $message = '<br />Autosubscribe is currently <b>ON.</b>';
+    $form = form_submit('Turn Autosubscribe Off', 'autosubscribeoff');
+    $message .= form( $form, 'post', 'subscriptions/offauto' ) . "Click this button to leave the 'Subscribe' checkbox unchecked when you submit content.  You will be required to check the checkbox manually if you wish to subscribe.<br /><br /><br />";
+    return $message;
   }
 }
 
