Index: subscriptions.module
===================================================================
--- subscriptions.module	(revision 38)
+++ subscriptions.module	(working copy)
@@ -1,14 +1,5 @@
 <?php
-// $Id: subscriptions.module,v 1.38.2.3.2.30 2007/01/21 15:00:52 arthuregg Exp $
-
-/*
- * @ TODO allow admin to edit user subscriptions (http://drupal.org/node/80323)
- * @ TODO make sure content restrictions are being restricted (http://drupal.org/node/13502)
- * @ TODO switch cron send to load actual nodes instead of serialized content
- * @ TODO fix comment sending to only use cron
- */
-
-
+// $Id: subscriptions.module,v 1.38.2.3.2.17 2006/11/05 15:05:44 c0c0c0 Exp $
 /**
  * Implementation of hook_help().
  */
@@ -123,6 +114,33 @@
     '#description'   => t('Select content types which should be <strong>omitted</strong> from subscription listings.'),
     '#multiple'      => TRUE
   );
+  
+  $form['sub_settings']['email'] = array(
+    '#type'=>'fieldset',
+    '#title' => 'Email settings for subscriptions notification',
+    '#collapsible'=>true,
+    '#collapsed'=>true,
+  );
+  $form['sub_settings']['email']['subscriptions_email_subject'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Subject'),
+    '#default_value' => variable_get('subscriptions_email_subject', ""),
+    '#description'   => t("You may use the following variables: %name (user's name), %type (type of addition, e.g. node, thread),
+            %content_type (the type of content being updated), %title (the node's title), %subject (type of update, e.g. 
+            blog addition)")
+  );
+  $form['sub_settings']['email']['subscriptions_email_body'] = array(
+    '#type'          => 'textarea',
+    '#title'         => t('Body'),
+    '#default_value' => variable_get('subscriptions_email_body', ""),
+    '#description'   => t("You may use the following variables: %name (user's name), %type (type of addition),
+            %content_type (the type of content being updated), %url (the url of the subscribed node), 
+            %site (the url of your site), %manage-url (the url to manage user's 
+            subscriptions), %title (the node's title), %teaser (the node's teaser), %full_node (the entire topic), 
+            %comment (the full comment)"),
+    '#rows'          => 15,
+    );     
+  
   $form['sub_settings']['subscriptions_sendself'] = array(
     '#type'          => 'checkbox',
     '#title'         => t('Notify poster of own posts'),
@@ -173,17 +191,12 @@
  */
 function subscriptions_menu($may_cache) {
   $items = array();
-  
   if ($may_cache) {
     if (variable_get('subscriptions_usersmenu', 0)){
-      global $user;     
-       $items[] = array(
-        'path' => "user/$user->uid/subscriptions", 
-        'title' => t('my subscriptions'),
-        'callback' => 'subscriptions_page', 
-        'access' => user_access('maintain subscriptions'),
-        'type' => MENU_LOCAL_TASK);     
-      
+      global $user;
+      $items[] = array('path' => "user/$user->uid/subscriptions", 'title' => t('my subscriptions'),
+        'callback' => 'subscriptions_page', 'access' => user_access('maintain subscriptions'),
+        'type' => MENU_LOCAL_TASK);
       // My Subscriptios submenus
       if(module_exist('blog')){
         $items[] = array('path' => "user/$user->uid/subscriptions/blogs", 'title' => t('blogs'),
@@ -352,7 +365,7 @@
 /*
  * Get e-mail vars
  */
-function subscriptions_mailvars($sid, $ssid, $uid, $stype, $strsent, $cid = null) {
+function subscriptions_mailvars($sid, $ssid, $uid, $stype, $strsent) {
   global $base_url;
   global $locale;
   $initial_locale = $locale;
@@ -368,18 +381,15 @@
     $result = db_query('SELECT u.uid, u.name, u.mail, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND s.stype = \'node\'', $sid);
     $strtype = 'thread';
     $nid = $sid;
-    $cid = $ssid;
-    $page = subscriptions_comment_page($ssid, $nid);
-    if ($page) {
-      $page = "&page=$page";
-    }
+    $cid = "comment-$ssid";
   }
    // if content type, get vars
-  if ($stype == 'type') {  	
+  if ($stype == 'type') {
     $typestr = 'type'.$sid;
     $result = db_query('SELECT u.mail, u.name, u.uid, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.stype =\''.$typestr.'\'');
     $strtype = 'content type';
     $nid = $ssid;
+    $cid = NULL;
   }
   // if node insert, test if node has a taxonomy else skip
   if ($stype == 'taxa' && !is_null($sid)) {
@@ -388,6 +398,7 @@
     $result = db_query('SELECT u.mail, u.name, u.uid, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND stype = \'taxa\'', $sid);
     $strtype = 'category';
     $nid = $ssid;
+    $cid = NULL;
   }
   // if blog insert, get vars
   if ($stype == 'blog') {
@@ -396,6 +407,7 @@
     $result = db_query('SELECT u.uid, u.name, u.mail, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND s.stype = \'blog\'', $sid);
     $strtype = 'blog';
     $nid = $ssid;
+    $cid = NULL;
   }
   $nobj = node_load($nid);
   // loop through subscribers and call mail function
@@ -414,7 +426,7 @@
     // set teaser variable
     if($subuser->subscriptions_teaser){
       // and set send filter appropriately
-      $teaser = (is_null($cid) ? $nobj->teaser : db_result(db_query('SELECT comment FROM {comments} WHERE cid = ' . $cid)));
+      $teaser = $nobj->teaser;
     }else{
       $teaser = '';
     }
@@ -426,16 +438,32 @@
         $locale = $subscriptions->language;
       }
       $headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
-      $subject = t('[!site] !type subscription update for !name : !subject', array('!site' => variable_get('site_name', 'drupal'), '!type' => t($strtype), '!name' => $subscriptions->name, '!subject' => $subj));
-      $body = t("Greetings, !name.\n\nA !type to which you have subscribed has been updated.\n!title\n!teaser\nTo view the thread, navigate to !url \n\n--\nThis is an automatic message from !site.\nTo manage your subscriptions, browse to !manage-url",
-      array('!name' => $subscriptions->name,
-			'!type' => t($strtype),
-            '!url' => url('node/'. $nid, $page? $page: null, (is_null($cid) ? NULL : "comment-$cid"), 1),
-            '!site' => t(variable_get('site_name', 'drupal')),
-            '!manage-url' =>  variable_get('subscriptions_usersmenu', 0) ? url("user/$subuid/subscriptions", NULL, NULL, 1) : url('subscriptions', NULL, NULL, 1),
-            '!title' => (is_null($cid) ? $nobj->title : $nobj->title . "\n\n" . db_result(db_query('SELECT subject FROM {comments} WHERE cid = ' . $cid)) ),
-            '!teaser'=> $teaser)
-            );
+      $setting_subject = variable_get('subscriptions_email_subject', "");
+      $setting_body    = variable_get('subscriptions_email_body', "");  
+      
+      // if a CCK type then get real name (i.e. "label") other wise just use the node->type
+      if (substr($nobj->type,0,8) == "content_") 
+        $content_type = db_result(db_query("SELECT label FROM {node_type_content} WHERE type_name = '$nobj->type'"));
+      else $content_type = $nobj->type;
+      
+      $subject = t($setting_subject, array(
+            '%site' => variable_get('site_name', 'drupal'),
+            '%type' => $strtype, 
+            '%content_type' => $content_type,
+            '%name' => $subscriptions->name,
+            '%title' => $nobj->title,
+            '%subject' => $subj));
+      $body = t($setting_body, array(
+            '%name' => $subscriptions->name,
+		    '%type' => t($strtype),
+            '%content_type' => $content_type,
+            '%url' => url('node/'. $nid, NULL, $cid, 1),
+            '%site' => t(variable_get('site_name', 'drupal')),
+            '%manage-url' => url('subscriptions', NULL, NULL, 1),
+            '%title' => $nobj->title,
+            '%teaser'=> $teaser,
+            '%full_node' => node_view(node_load($nobj->nid),false,true,false),
+            '%comment' => comment_render($sid, $ssid)));
       // revert to original locale
       $locale = $initial_locale;
       subscriptions_sendmail($subscriptions->name, $subscriptions->mail, $subject, $body, $headers);
@@ -443,7 +471,6 @@
   }
   return $strsent;
 }
-
 /*
  * Subscribes users to nodes in which they post, if not already subscribed
  */
@@ -459,7 +486,6 @@
     }
   }
 }
-
 /**
  * handling for held nodes
  */
@@ -467,42 +493,25 @@
   $strsent = '!';
   $onode = unserialize($heldnode);
   if ($onode->status) {
-    if (!empty($onode->taxonomy)) { 
-      
-      // this handles free taging
-      if ($onode->taxonomy['tags']){
-        $freetags = explode(", ", $onode->taxonomy['tags'][1]); 
-      	foreach($freetags as $tag) {
-      	  $tag_id = taxonomy_get_term_by_name($tag);     	 
-      	  $thetags[$tag_id[0]->tid] = $tag_id[0]->tid;      	  
-      	}
-      	unset($onode->taxonomy['tags']);
-      	$onode->taxonomy[] = $thetags;
-      }    	    	
-      foreach ($onode->taxonomy as $terms){      	          	  
-  	    foreach ($terms as $tid){   	  
-          $strsent = $strsent . subscriptions_mailvars($tid, $onode->nid, $poster, 'taxa', $strsent);
-      	}
-      }         
+    if (!empty($onode->taxonomy)) {
+      foreach ($onode->taxonomy as $tid){
+        $strsent = $strsent . subscriptions_mailvars($tid, $onode->nid, $poster, 'taxa', $strsent);
+      }
     }
-    
     $strsent = $strsent . subscriptions_mailvars($onode->nid, 0, $poster, 'node', $strsent);
-    
     if ($node->type == 'blog') {
       $strsent = $strsent . subscriptions_mailvars($onode->uid, $onode->nid, $poster, 'blog', $strsent);
     }
-  }  
+  }
 }
-
 /**
  * handling for held comments
  */
 function subscriptions_heldcomments($heldcomment, $poster){
   $strsent = '!';
-  $ocom = unserialize($heldcomment);  
+  $ocom = unserialize($heldcomment);
   subscriptions_mailvars($ocom['nid'], $ocom['cid'], $poster, 'node', $strsent);
 }
-
 /**
  * store node changes for later handling
  */
@@ -510,7 +519,6 @@
   $strqry = 'INSERT INTO {subscriptions_holding} ( content, ptype , op, pid )  VALUES (\'%s\', \'%s\', \'%s\', %d)';
   db_query($strqry, serialize($content), $ptype, $op, $pid);
 }
-
 /**
  * Test to see if a post is still active before notifications are sent
  */
@@ -532,7 +540,6 @@
   }
   return $valid;
 }
-
 /**
  * Implementation of cron job.
  */
@@ -546,26 +553,20 @@
     if($proceed){
       // do send
       if($row->ptype == 'comment'){
-      	$content = unserialize($row->content);
-      	// we check the comment status and don't send or delete it
-      	// if it hasn't been approved
-      	$comment = db_fetch_array(db_query("SELECT * FROM comments WHERE cid = %d", $content['cid']));
-        if ($comment['status'] != 1){
-          subscriptions_heldcomments($row->content, $row->pid);
-          // delete processed row
-          db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);
-        }
-      } // row type == 'comment'
-      if($row->ptype == 'node'){
-        subscriptions_heldnodes($row->content, $row->pid);
-        // delete processed row
-        db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);      
-      } // row type == 'node'
-      }else{
-        // do delete
-        db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);
-      }
-    } // for each row in results set
+      subscriptions_heldcomments($row->content, $row->pid);
+      // delete processed row
+      db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);
+    } // row type == 'comment'
+    if($row->ptype == 'node'){
+      subscriptions_heldnodes($row->content, $row->pid);
+      // delete processed row
+      db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);
+    } // row type == 'node'
+    }else{
+      // do delete
+      db_query('DELETE FROM {subscriptions_holding} WHERE rid = %d', $row->rid);
+    }
+  } // for each row in results set
   }  // if using cron to send notifications
 }
 
@@ -590,11 +591,10 @@
   if ($op == 'insert' || ($op == 'update' && $comment->status == 1)) { // ignore deactivated comments
     //  if use_cron is set, insert node actions into holding table
     if(variable_get('subscriptions_usecron', 0)){
-      subscriptions_hold($comment, 'comment' , $op, $user->uid );
+      subscriptions_hold( $comment, 'comment' , $op, $user->uid );
     } else {  // if cron is not used
       $cid = is_null($comment->cid) ? $comment['cid'] : $comment->cid;
       $nobj = node_load($nid);
-      if(!$nobj->status) return;  // node is unpublished
       // send node subscriptions
       $strsent = $strsent . subscriptions_mailvars($nid, $cid, $user->uid, 'node', $strsent);
       // get subscription->node->taxonomy
@@ -602,81 +602,16 @@
       // send to taxonomy subscribers
       if (!empty($taxa)) {
           foreach ($taxa as $tid){
-            $strsent = $strsent . subscriptions_mailvars($tid, $nid, $user->uid, 'taxa', $strsent, $cid);
+            $strsent = $strsent . subscriptions_mailvars($tid, $nid, $user->uid, 'taxa', $strsent);
           }
       }
       // send content type subscriptions
-      $strsent = $strsent . subscriptions_mailvars($nobj->type, $nobj->nid, $user->uid, 'type', $strsent, $cid);
+      $strsent = $strsent . subscriptions_mailvars($nobj->type, $nobj->nid, $user->uid, 'type', $strsent);
     } // end cron test
     subscriptions_autosubscribe($user->uid, $nid);
   }
 }
-
 /**
-* Return the page a comment is on
-*/
-function subscriptions_comment_page($cid, $nid) {
-  
-  $comments_per_page = _comment_get_display_setting('comments_per_page');
-  
-  // load the comment
-  $comment = _comment_load($cid);
- 
-  if (user_access('administer comments')) {
-   // For users with 'administer comments' permission
-   // we have to count published and unpublished comments.
-   $comments_num = comment_num_all($nid, TRUE);
-  }
-  else {
-    $comments_num = isset($comment->comment_count) ? $comment->comment_count : comment_num_all($nid);
-  }
-  
-  if ($comments_num <= $comments_per_page) {
-    // one page of comments only
-    return 0;
-  }
-  else {
-    // Build the database query that retrieves the comment's position
-    // This follows the same sheme as in comment_render().
-    // See comments there for an explanation.
-    $query = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
-    $query_args = array($nid);
-  
-    if (!user_access('administer comments')) {
-      $query .= ' AND status = %d';
-      $query_args[] = COMMENT_PUBLISHED;
-    }
-
-    $mode = _comment_get_display_setting('mode');
-    $order = _comment_get_display_setting('sort');
-    if ($order == COMMENT_ORDER_NEWEST_FIRST) {
-      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
-        $query .= ' AND timestamp > %d';
-        $query_args[] = $comment->timestamp;
-      }
-      else {
-        $query .= " AND thread > '%s'";
-        $query_args[] = $comment->thread;
-      }
-    }
-    else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
-      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
-        $query .= ' AND timestamp < %d';
-        $query_args[] = $comment->timestamp;
-      }
-     else {
-       $query .= " AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '%s'";
-       $query_args[] = substr($comment->thread, 0, -1);
-     }
-   }
-  
-   $count = db_result(db_query($query, $query_args));
-   return floor($count / $comments_per_page);
-  }
-}
-
-
-/**
  * Implementation of hook_nodeapi().
  */
 function subscriptions_nodeapi(&$node, $op, $arg = 0) {
@@ -689,41 +624,36 @@
     if($node->status == '1' && $node->subscriptions_currentstatus == '1'){break;}
     // else, fall through
     case 'insert':
-      if (variable_get('subscriptions_usecron', 0)) {
-        // using cron to send notifications
-        subscriptions_hold( $node, 'node' , $op, $user->uid );
-      }
-      else {  // sending notification on submission
-        if ($node->status) {
-          if (!empty($node->taxonomy)) {
-            $omitted_taxa = variable_get('subscriptions_omitted_taxa', array());
-            foreach ($node->taxonomy as $tid){
-              // send taxonomy subscriptions
-              if (!in_array($tid->vid, $omitted_taxa)) {
-                // send taxonomy subscriptions
-                $strsent = $strsent . subscriptions_mailvars($tid, $node->nid, $user->uid, 'taxa', $strsent);
-              }
-            }
+    if (variable_get('subscriptions_usecron', 0)) {
+      // using cron to send notifications
+      subscriptions_hold( $node, 'node' , $op, $user->uid );
+    }
+    else {  // sending notification on submission
+      if ($node->status) {
+        if (!empty($node->taxonomy)) {
+          foreach ($node->taxonomy as $tid){
+            // send taxonomy subscriptions
+            $strsent = $strsent . subscriptions_mailvars($tid, $node->nid, $user->uid, 'taxa', $strsent);
           }
-          // send content type subscriptions
-          $strsent = $strsent . subscriptions_mailvars($node->type, $node->nid, $user->uid, 'type', $strsent);
-          // send node subscriptions
-          $strsent = $strsent . subscriptions_mailvars($node->nid, 0, $user->uid, 'node', $strsent);
-          if ($node->type == 'blog') {
-            // send blog subscriptions
-            $strsent = $strsent . subscriptions_mailvars($node->uid, $node->nid, $user->uid, 'blog', $strsent);
-          }
         }
-      }  // cron test
-      subscriptions_autosubscribe($user->uid, $node->nid);
-      if (isset($node->subscriptions_subscribe)) {
-        if ($node->subscriptions_subscribe) {
-          subscriptions_add($node->nid, $user->uid, 'node');
+        // send content type subscriptions
+        $strsent = $strsent . subscriptions_mailvars($node->type, $node->nid, $user->uid, 'type', $strsent);
+        // send node subscriptions
+        $strsent = $strsent . subscriptions_mailvars($node->nid, 0, $user->uid, 'node', $strsent);
+        if ($node->type == 'blog') {
+          // send blog subscriptions
+          $strsent = $strsent . subscriptions_mailvars($node->uid, $node->nid, $user->uid, 'blog', $strsent);
         }
-        $user = user_load(array("uid" => $user->uid));
-        user_save($user, array('subscriptions_subscribe' => $node->subscriptions_subscribe));
+        subscriptions_autosubscribe($user->uid, $node->nid);
       }
-    break;
+    }  // cron test
+    if (isset($node->subscriptions_subscribe)) {
+      if ($node->subscriptions_subscribe) {
+        subscriptions_add($node->nid, $user->uid, 'node');
+      }
+      user_save($user, array('subscriptions_subscribe' => $node->subscriptions_subscribe));
+    }
+  break;
   }
 }
 /**
@@ -739,7 +669,6 @@
 		'#type'        => 'fieldset',
 		'#title'       => t('Subscriptions'),
 		'#collapsible' => TRUE,
-		'#collapsed'   => FALSE,
 		'#weight'      => 1,
 		);
     $allsubs = subscriptions_get_user();
@@ -1033,10 +962,8 @@
 /*
  * Pages where users add and delete their subscriptions to nodes
  */
-function subscriptions_page($uid = null) {
- global $user; 
-  
-  
+function subscriptions_page() {
+  global $user;
   $subscribed = false;
   $uid = $user->uid;
   if (!arg(2)) {
@@ -1191,7 +1118,7 @@
       $message .= theme('xml_icon', url("subscriptions/feed"));
       drupal_add_link(array('rel' => 'alternate',
                           'type' => 'application/rss+xml',
-                          'title' => t("!name Subscriptions", array('!name' => $user->name)),
+                          'title' => t("%name Subscriptions", array('%name' => $user->name)),
                           'href' => url('subscriptions/feed')));
       return $message;
       break;
@@ -1218,13 +1145,13 @@
     $cond[] = "tn.tid IN ($tids)";
   }
   // content types link differently and will be excluded from this list
-  $sql = "SELECT n.nid, max( n.created ) AS nc FROM {node} n LEFT JOIN {term_node} tn ON n.nid=tn.nid WHERE n.status=1";
+  $sql = "SELECT n.nid, max( n.created ) AS nc FROM {node} n LEFT JOIN {term_node} tn ON n.nid=tn.nid";
   if ($cond) {
-    $sql.= " AND ( ". implode(' OR ', $cond). " )";
+    $sql.= " WHERE ". implode(' OR ', $cond);
   }
   $sql .= " GROUP BY n.nid ORDER BY nc DESC";
   $result = db_query_range(db_rewrite_sql($sql), 0, variable_get('feed_default_items', 10));
-  $channel['title'] = t("!name Subscriptions", array('!name' => $account->name));
+  $channel['title'] = t("%name Subscriptions", array('%name' => $account->name));
   $channel['link'] = url("subscriptions/feed", NULL, NULL, TRUE);
   // $channel['description'] = ;
   node_feed($result, $channel);
