--- notify.module	Sun Apr 24 19:16:30 2011
+++ notify.module.new	Sun Apr 24 19:14:46 2011
@@ -33,6 +33,8 @@ function notify_help($section) {
  */
 function notify_admin_settings() {
   $period = array(
+//    60          => format_interval(60),    // for test purposes
+//    300         => format_interval(300),   // for test purposes
     900         => format_interval(900),
     1800        => format_interval(1800),
     3600        => format_interval(3600),
@@ -51,13 +53,14 @@ function notify_admin_settings() {
 
   $form = array();
 
-  $form['notify_settings'] = array(
+  $form['notify_schedule_settings'] = array(
     '#type' => 'fieldset',
-    '#title' => t('E-mail notification settings'),
+    '#title' => t('E-mail notification schedule settings'),
     '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
   );
 
-  $form['notify_settings']['notify_send'] = array(
+  $form['notify_schedule_settings']['notify_send'] = array(
     '#type' => 'select',
     '#title' => 'Send notifications every',
     '#default_value' => variable_get('notify_send', array(86400)),
@@ -65,35 +68,72 @@ function notify_admin_settings() {
     '#description' => 'How often should new content notifications be sent? Requires cron to be running at least this often.',
   );
 
-  $form['notify_settings']['notify_send_hour'] = array(
+  $form['notify_schedule_settings']['notify_send_hour'] = array(
     '#type' => 'select',
-    '#title' => 'Hour to Send Notifications',
+    '#title' => 'Hour to send notifications, when one day or greater',
     '#description' => 'Specify the hour (24-hour clock) in which notifications should be sent, if the frequency is one day or greater.',
     '#default_value' => variable_get('notify_send_hour', 9),
     '#options' => array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23),
   );
 
-  $form['notify_settings']['notify_attempts'] = array(
+  $form['notify_schedule_settings']['notify_attempts'] = array(
     '#type' => 'select',
     '#title' => 'Number of failed sends after which notifications are disabled',
     '#default_value' => variable_get('notify_attempts', array(5)),
     '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20),
   );
 
-  $form['notify_settings']['notify_reg_default'] = array(
+  $form['notify_default_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('E-mail content default settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+
+  $form['notify_default_settings']['notify_reg_default'] = array(
     '#type' => 'checkbox',
-    '#title' => 'Notification checkbox default on new user registration form',
+    '#title' => 'Show email notification checkbox on new user registration form',
     '#return_value' => 1,
     '#default_value' => variable_get('notify_reg_default', 1),
   );
 
-  $form['notify_settings']['notify_include_updates'] = array(
+  $form['notify_default_settings']['notify_include_updates'] = array(
     '#type' => 'checkbox',
     '#title' => 'Include updated posts in notifications',
     '#return_value' => 1,
     '#default_value' => variable_get('notify_include_updates', 1),
   );
 
+  $form['notify_default_settings']['notify_status_default'] = array(
+    '#type' => 'radios',
+    '#title' => 'Email notification status default for new user',
+    '#return_value' => 1,
+    '#default_value' => variable_get('notify_status_default', 1),
+    '#options' =>  array(t('Disabled'), t('Enabled')),
+  );
+
+  $form['notify_default_settings']['notify_node_default'] = array(
+    '#type' => 'radios',
+    '#title' => 'New content default',
+    '#return_value' => 1,
+    '#default_value' => variable_get('notify_node_default', 1),
+    '#options' =>  array(t('Disabled'), t('Enabled')),
+  );
+
+  $form['notify_default_settings']['notify_teasers_default'] = array('#type' => 'radios',
+    '#title' => t('Content amount default'),
+    '#default_value' => variable_get('notify_teasers_default', 1),    // set default; 0 = titles, 1 = titles+teasers, 2 = titles+body
+    '#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')),
+    '#description' => t('Select the default amount of each post to see in notification e-mails.'),
+  );
+    
+  $form['notify_default_settings']['notify_comment_default'] = array('#type' => 'radios',
+    '#title' => t('New comments default'),
+    '#default_value' => variable_get('notify_comment_default', 1),      // set default to enabled
+    '#options' => array(t('Disabled'), t('Enabled')),
+    '#description' => t('Default to include new comments in the notification mail.'),
+    );
+
   $set = 'ntype';
   $form[$set] = array(
     '#type' => 'fieldset',
@@ -123,12 +163,12 @@ function notify_cron() {
   $send_interval = variable_get('notify_send', 86400);
   $send_hour = variable_get('notify_send_hour', 9);
   $send_start = time();
-  if ( ($send_start - $send_last > $send_interval)
-    && (date('G', $send_start) >= $send_hour || $send_interval < 86400)
-    && ($send_interval != -1) //special case of settings to send 'never'
+	if ( ($send_interval != -1)  // not disabled, settings not set to send 'never'
+    && ($send_start - $send_last > $send_interval)
+    && (($send_interval < 86400) || (date('G', $send_start) >= $send_hour))
       ) {
     _notify_send($send_start);
-    variable_set('notify_send_last', $send_start);
+//    variable_set('notify_send_last', $send_start);  // Moved to _notify_send, rather than have in two places
   }
 }
 
@@ -147,7 +187,7 @@ function notify_user($type, &$edit, &$us
 
     case 'insert':
       if (isset($edit['notify_decision']) && $edit['notify_decision'] == 1) {
-        db_query('INSERT INTO {notify} (uid, status, node, teasers, comment, send_last) VALUES (%d, %d, %d, %d, %d, %d)', $user->uid, 1, 1, 0, 0, 0);
+        db_query('INSERT INTO {notify} (uid, status, node, teasers, comment, send_last) VALUES (%d, %d, %d, %d, %d, %d)', $user->uid, variable_get('notify_status_default'), variable_get('notify_node_default'), variable_get('notify_teasers_default'), variable_get('notify_comment_default'), 0);
         $edit['notify_decision'] = NULL;
       }
       break;
@@ -160,10 +200,11 @@ function notify_user($type, &$edit, &$us
  * Returns form fields to be added to User Regsitration form
  */
 function _notify_user_reg_fields() {
-  if (!user_access('access notify')) return;
+//  if (!user_access('access notify')) return;
+  if (0 == variable_get('notify_reg_default')) return ; 
 
   // Get the variable for how often the notifications are sent out
-  $period = variable_get("notify_send", 86400);
+  $period = variable_get('notify_send', 86400);
 
   // Add a fieldset containing a checkbox for users to accept
   // getting updates on the registration form.
@@ -175,9 +216,10 @@ function _notify_user_reg_fields() {
   // Add the checkbox to the fieldset
   $fields['notify_agree']['notify_decision'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Receive email notifications of new content posted to this site. Notifications are sent every ' . format_interval($period) . '.'),
+    '#title' => t('Receive email notifications of new content posted to this site.'),
     '#return_value' => 1,
-    '#default_value' => variable_get('notify_reg_default', 1),
+    '#default_value' => variable_get('notify_status_default', 1),
+	'#description' => t('You can change this setting on the "My account" page at any time. Notifications are sent every ' . format_interval($period) . '.'),
   );
 
   return $fields;
@@ -188,7 +230,7 @@ function _notify_user_reg_fields() {
  * Implementation of hook_perm().
  */
 function notify_perm() {
-  return array('access notify', 'administer notify');
+  return array('access notify', 'access notify details', 'administer notify');
 }
 
 /**
@@ -281,38 +323,69 @@ function notify_user_settings_form(&$for
         drupal_set_message(t('Your e-mail address must be specified on your <a href="@url">my account</a> page.', array('@url' => url('user/'. $account->uid .'/edit'))), 'error');
   }
 
+  $send_interval = variable_get('notify_send', 86400);
+
   $form['notify_page_master'] = array('#type' => 'fieldset', '#title' => t('Master switch'));
   $form['notify_page_master']['status'] = array('#type' => 'radios',
     '#title' => t('Notify status'),
     '#default_value' => $notify->status,
     '#options' =>  array(t('Disabled'), t('Enabled')),
-    '#description' => t('Do you wish to receive periodic e-mails when new content is posted?'),
+    '#description' => t('Do you wish to receive periodic e-mails when new content is posted?  Notifications are sent every ' . format_interval($send_interval) . '.'),
   );
 
-  $form['notify_page_detailed'] = array('#type' => 'fieldset', '#title' => t('Detailed settings'));
-  $form['notify_page_detailed']['node'] = array('#type' => 'radios',
-    '#title' => t('Notify new content'),
-    '#default_value' => $notify->node,
-    '#options' => array(t('Disabled'), t('Enabled')),
-    '#description' => t('Include new posts in the notification mail.'),
-  );
-  $form['notify_page_detailed']['teasers'] = array('#type' => 'radios',
-    '#title' => t('Content'),
-    '#default_value' => $notify->teasers,
-    '#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')),
-    '#description' => t('Select the amount of each post that you would like to see in your notification e-mails.'),
-  );
-  $form['notify_page_detailed']['comment'] = array('#type' => 'radios',
-    '#title' => t('Notify new comments'),
-    '#default_value' => $notify->comment,
-    '#options' => array(t('Disabled'), t('Enabled')),
-    '#description' => t('Include new comments in the notification mail.'),
-  );
+  // If node detail settings don't exist use defaults
+  if ($notify->node == NULL) {
+    $node_value = variable_get('notify_node_default');
+	}
+  else {
+	$node_value = $notify->node;
+	}
+
+  if (user_access('access notify details')) {
+    $form['notify_page_detailed'] = array('#type' => 'fieldset', '#title' => t('Detailed settings'));
+    $form['notify_page_detailed']['node'] = array('#type' => 'radios',
+      '#title' => t('Notify new content'),
+      '#default_value' => $node_value,
+      '#options' => array(t('Disabled'), t('Enabled')),
+      '#description' => t('Include new posts in the notification mail.'),
+    );
+
+  // If teasers detail settings don't exist use defaults
+  if ($notify->teasers == NULL) {
+    $teasers_value = variable_get('notify_teasers_default');
+	}
+  else {
+	$teasers_value = $notify->teasers;
+	}
+
+    $form['notify_page_detailed']['teasers'] = array('#type' => 'radios',
+      '#title' => t('Content'),
+      '#default_value' => $teasers_value,
+      '#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')),
+      '#description' => t('Select the amount of each post that you would like to see in your notification e-mails.'),
+    );
+
+  // If comment detail settings don't exist use defaults
+  if ($notify->comment == NULL) {
+    $comment_value = variable_get('notify_comment_default');
+	}
+  else {
+	  $comment_value = $notify->comment;
+	}
+
+    $form['notify_page_detailed']['comment'] = array('#type' => 'radios',
+      '#title' => t('Notify new comments'),
+      '#default_value' => $comment_value,
+      '#options' => array(t('Disabled'), t('Enabled')),
+      '#description' => t('Include new comments in the notification mail.'),
+    );
+	}
+ 
   $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
   $form['notify_send_last'] = array('#type' => 'markup',
     '#title' => t('Notify last send'),
     '#description' => t('The date of the last notification for this user.'),
-    '#value' => t('Last Send: !date', array('!date' => $notify->send_last > 0 ? date('n/j/y g:i a', $notify->send_last) : "never"))
+    '#value' => t('Last Send: !date', array('!date' => $notify->send_last > 0 ? date('n/j/y g:i a', $notify->send_last) : "never")),
     );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
 
@@ -322,7 +395,7 @@ function notify_user_settings_form(&$for
 function notify_user_settings_form_submit($form, &$form_state) {
   unset($form);
   db_query('DELETE FROM {notify} WHERE uid = %d', $form_state['values']['uid']);
-  db_query('INSERT INTO {notify} (uid, status, node, teasers, comment) VALUES (%d, %d, %d, %d, %d)', $form_state['values']['uid'], $form_state['values']['status'], $form_state['values']['node'], $form_state['values']['teasers'], $form_state['values']['comment']);
+  db_query('INSERT INTO {notify} (uid, status, node, teasers, comment, send_last) VALUES (%d, %d, %d, %d, %d, %d)', $form_state['values']['uid'], $form_state['values']['status'], $form_state['values']['node'], $form_state['values']['teasers'], $form_state['values']['comment'], $form_state['values']['send_last']);
   drupal_set_message(t('Notify settings saved.'));
 }
 
@@ -332,15 +405,19 @@ function notify_user_settings_form_submi
 function notify_admin_users() {
   $form = array();
   $form['#tree'] = TRUE;
-  $form['info'] = array('#value' => t('The following table shows all users that have notifications enabled.'));
+//  $form['info'] = array('#value' => t('The following table shows all users that have notifications enabled.'));
+  $form['info'] = array('#value' => t('The following table shows all users that have notification settings.'));
 
   $form['users'] = array();
 
-  $result = db_query('SELECT u.uid, u.name, u.mail, n.* FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE n.status = 1 AND u.status = 1 ORDER BY u.name');
+  // Changed to show all users notify settings whether status is enabled or disabled, "WHERE n.status = 0 OR n.status =1 AND ..." is required to avoid a single erroneous row being displayed
+//  $result = db_query('SELECT u.uid, u.name, u.mail, n.* FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE n.status = 1 AND u.status = 1 ORDER BY u.name');
+  $result = db_query('SELECT u.uid, u.name, u.mail, n.* FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE n.status = 0 OR n.status = 1 AND u.status = 1 ORDER BY u.name');
   while ($notify = db_fetch_object($result)) {
     $form['users'][$notify->uid] = array();
     $form['users'][$notify->uid]['name'] = array('#type' => 'markup', '#value' => theme('username', $notify));
     $form['users'][$notify->uid]['mail'] = array('#type' => 'markup', '#value' => $notify->mail);
+	$form['users'][$notify->uid]['status'] = array('#type' => 'checkbox', '#default_value' => $notify->status);
     $form['users'][$notify->uid]['node'] = array('#type' => 'checkbox', '#default_value' => $notify->node);
     $form['users'][$notify->uid]['teasers'] = array('#type' => 'select', '#default_value' => $notify->teasers, '#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')));
     $form['users'][$notify->uid]['comment'] = array('#type' => 'checkbox', '#default_value' => $notify->comment);
@@ -368,7 +445,7 @@ function notify_admin_users_submit($form
 
   if ($form_state['values']['users']) {
     foreach ($form_state['values']['users'] as $uid => $settings) {
-      db_query('UPDATE {notify} SET node = %d, teasers = %d, comment = %d, attempts = %d WHERE uid = %d', $settings['node'], $settings['teasers'], $settings['comment'], $settings['attempts'], $uid);
+      db_query('UPDATE {notify} SET status = %d, node = %d, teasers = %d, comment = %d, attempts = %d WHERE uid = %d', $settings['status'], $settings['node'], $settings['teasers'], $settings['comment'], $settings['attempts'], $uid);
     }
   }
   drupal_set_message(t('Notify settings saved.'));
@@ -376,7 +453,7 @@ function notify_admin_users_submit($form
   if ($form_state['values']['flush']) {
     $send_start = time();
     list($num_sent, $num_failed) = _notify_send($send_start);
-    variable_set('notify_send_last', $send_start);
+//    variable_set('notify_send_last', $send_start);          // move to _notify_send to have in one place only rather than two
 
     if ($num_sent > 0) {
       drupal_set_message(t('!count pending notification e-mails have been sent.', array('!count' => $num_sent)));
@@ -395,7 +472,7 @@ function notify_admin_users_submit($form
  */
 function theme_notify_admin_users($form) {
   $output = drupal_render($form['info']);
-  $header = array(t('Username'), t('E-mail address'), t('Content'), t('Teasers'), t('Comment'), t('Failed attempts'), t('Last Send'));
+  $header = array(t('Username'), t('E-mail address'), t('Status'), t('Content'), t('Teasers'), t('Comment'), t('Failed attempts'), t('Last Send'));
 
   $rows = array();
   foreach (element_children($form['users']) as $uid) {
@@ -441,9 +518,13 @@ function _notify_content($node, $notify)
  *
  * TODO: Needs some cleanup and themability.
  */
+
 function _notify_send($send_start = NULL) {
   $send_start = $send_start ? $send_start : time();
-  $period = variable_get('notify_send_last', $send_start - variable_get('notify_send', 86400));
+  $send_interval = variable_get('notify_send', 86400);
+//  $period = variable_get('notify_send_last', $send_start - variable_get('notify_send', 86400));
+  $period = variable_get('notify_send_last', $send_start - $send_interval);
+  variable_set('notify_send_last', $send_start);        // Moved from hook_cron and notify_admin_user_submit
   $separator = '------------------------------------------------------------------------------';
   $mini_separator = '---';
 
@@ -656,14 +737,14 @@ function _notify_switch_user($uid = NULL
     // Should a user visit cron.php, or should this module be invoked via poormanscron and _notify_send() does not complete,
     // the visitor will end up logged in as the "switched to user" for subsequent requests unless we disable saving the session
     // until we are sure we're the invoking user again.
-    session_save_session(FALSE);
-    $user = user_load(array('uid' => $uid));
+    session_save_session(FALSE); // D7: use drupal_save_session(FALSE);
+    $user = user_load(array('uid' => $uid)); // D7: use user_load($uid);
   }
   // Retrieve the initial user, can be called multiple times.
   elseif (count($orig_user)) {
     $user = array_shift($orig_user);
     array_unshift($orig_user, $user);
-    session_save_session(TRUE);
+    session_save_session(TRUE); // D7: use drupal_save_session(TRUE);
   }
   // Store the initial user.
   else {
