diff --git a/notify.install b/notify.install index 69b57ed..2337b5e 100755 --- a/notify.install +++ b/notify.install @@ -14,6 +14,7 @@ function notify_schema() { 'comment' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '2'), 'attempts' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'), 'teasers' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'), + 'send_last' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), ), 'primary key' => array('uid'), ); @@ -21,6 +22,18 @@ function notify_schema() { return $schema; } +function notify_update_6001() { + $ret = array(); + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + $ret[] = update_sql("ALTER TABLE {notify} ADD COLUMN send_last int NOT NULL DEFAULT 0;"); + break; + } + + return $ret; +} + function notify_install() { // Create my tables. drupal_install_schema('notify'); @@ -35,4 +48,4 @@ function notify_uninstall() { db_query("DELETE FROM {variable} WHERE name LIKE 'notify_%'"); cache_clear_all('variables', 'cache'); unset($conf[$name]); -} +} \ No newline at end of file diff --git a/notify.module b/notify.module index ad7a678..11a0d46 100644 --- a/notify.module +++ b/notify.module @@ -146,7 +146,7 @@ function notify_user($type, &$edit, &$user, $category = NULL) { case 'insert': if (isset($edit['notify_decision']) && $edit['notify_decision'] == 1) { - db_query('INSERT INTO {notify} (uid, status, node, teasers, comment) VALUES (%d, %d, %d, %d, %d)', $user->uid, 1, 1, 0, 0); + 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); $edit['notify_decision'] = NULL; } break; @@ -159,8 +159,6 @@ function notify_user($type, &$edit, &$user, $category = NULL) { * Returns form fields to be added to User Regsitration form */ function _notify_user_reg_fields() { - if (!user_access('access notify')) return; - // Get the variable for how often the notifications are sent out $period = variable_get("notify_send", 86400); @@ -176,7 +174,7 @@ function _notify_user_reg_fields() { '#type' => 'checkbox', '#title' => t('Receive email notifications of new content posted to this site. Notifications are sent every ' . format_interval($period) . '.'), '#return_value' => 1, - '#default_value' => variable_get('notify_reg_default', 1), + '#default_value' => 1, ); return $fields; @@ -211,8 +209,8 @@ function notify_menu() { 'title' => 'Notification settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('notify_user_settings_form', 1), - 'access callback' => 'notify_user_access', - 'access arguments' => array(1), + 'access callback' => 'user_access', + 'access arguments' => array('access notify'), 'type' => MENU_LOCAL_TASK ); $items['admin/user/user/notify'] = array( @@ -228,20 +226,6 @@ function notify_menu() { } /** - * Checks access to notifications settings tab - */ -function notify_user_access($account = NULL) { - return $account->uid && - ( - // Always let users view their own profile. - ($GLOBALS['user']->uid == $account->uid && user_access('access notify')) || - // Administrators can view all accounts. - user_access('administer notify') - ); -} - - -/** * Register the themeing the form data into a table at * admin/user/user/notify * @@ -262,7 +246,7 @@ function notify_theme() { function notify_user_settings_form(&$form_state, $arg) { global $user; - if ($user->uid != $arg->uid && !user_access('administer notify') ) { + if ($user->uid != $arg->uid && $user->uid != 1) { drupal_access_denied(); return; } @@ -273,7 +257,7 @@ function notify_user_settings_form(&$form_state, $arg) { 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.send_last 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) { @@ -308,6 +292,11 @@ function notify_user_settings_form(&$form_state, $arg) { '#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")) + ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save settings')); return $form; @@ -339,6 +328,7 @@ function notify_admin_users() { $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); $form['users'][$notify->uid]['attempts'] = array('#type' => 'textfield', '#size' => 2, '#default_value' => $notify->attempts ? intval($notify->attempts) : 0); + $form['users'][$notify->uid]['send_last'] = array('#type' => 'markup', '#value' => $notify->send_last > 0 ? date('n/j/y g:i a', $notify->send_last) : "never"); } $form['flush'] = array( @@ -388,7 +378,7 @@ function notify_admin_users_submit($form, &$form_state) { */ 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')); + $header = array(t('Username'), t('E-mail address'), t('Content'), t('Teasers'), t('Comment'), t('Failed attempts'), t('Last Send')); $rows = array(); foreach (element_children($form['users']) as $uid) { @@ -475,20 +465,31 @@ function _notify_send($send_start = NULL) { } // Fetch new comments. - $comments = array(); - if (module_exists('comment')) { - $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name, c.status FROM {comments} c INNER JOIN {node} nn ON c.nid = nn.nid WHERE c.timestamp > %d AND c.timestamp <= %d ' . $nodetypes_query . ' ORDER BY c.nid, c.timestamp', 'c'), $period, $send_start); - while ($comment = db_fetch_object($cresult)) { - $comments[$comment->nid][] = $comment; - } + $comments = array(); + $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name FROM {comments} c INNER JOIN {node} n ON c.nid = n.nid WHERE c.status = %d AND c.timestamp > %d AND c.timestamp <= %d '. $reqntype . ' ORDER BY c.nid, c.timestamp', 'c'), COMMENT_PUBLISHED, $period, time()); + while ($comment = db_fetch_object($cresult)) { + $comments[$comment->nid][] = $comment; } if (count($nodes) || count($comments)) { // Fetch users with notify enabled - $uresult = db_query('SELECT u.uid, u.name, u.mail, u.language, 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)); + $uresult = db_query('SELECT u.uid, u.name, u.mail, u.language, n.status, n.node, n.teasers, n.comment, n.send_last 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)); while ($user = db_fetch_object($uresult)) { + // check to see if the send_last for this user is > the global + // notify send_last setting. if so, this indicates cron was aborted + // for taking too long and that this user should be considered done. + // technically, we should look for new updates since then, but since + // cron isn't finishing in time, it's better to let it continue to the + // rest of the users. + $notify_send_last = variable_get('notify_send_last', 0); + if ( !empty($user->send_last) && $notify_send_last <= $user->send_last ) { + // enable this if you want lots of debug messages confirming it's + // doing the right thing + continue; + } + // Switch current user to this account to use node_access functions, etc. _notify_switch_user($user->uid); @@ -529,7 +530,7 @@ function _notify_send($send_start = NULL) { // Prepend node e-mail header as long as user could access at least one node. if ($node_count > 0) { $node_body = $separator ."\n" - . t('Recent content - !count', array('!count' => format_plural(count($nodes), '1 new post', '@count new posts'))) ."\n" + . t('Recent content - !count', array('!count' => format_plural(count($nodes), '1 new post', count($nodes) .' new posts'))) ."\n" . $separator ."\n\n". $node_body; } } @@ -555,7 +556,7 @@ function _notify_send($send_start = NULL) { if ($comment_body) { $comment_body .= $mini_separator ."\n\n"; } - $comment_body .= t('!count attached to !type posted by !author: !title', array('!count' => format_plural(count($comment), '1 new comment', '@count new comments'), '!title' => $node->title, '!type' => node_get_types('name', $node), '!author' => $node->name ? $node->name : variable_get('anonymous', 'Anonymous'))) ."\n"; + $comment_body .= t('!count attached to !type posted by !author: !title', array('!count' => format_plural(count($comment), '1 new comment', count($comment).' new comments'), '!title' => $nodes[$nid]->title, '!type' => node_get_types('name', $nodes[$nid]), '!author' => $nodes[$nid]->name ? $nodes[$nid]->name : variable_get('anonymous', 'Anonymous'))) ."\n"; $comment_count = 0; foreach ($comment as $c) { @@ -575,7 +576,7 @@ function _notify_send($send_start = NULL) { if ($total_comment_count > 0) { $comment_body = $separator ."\n" - . t('Recent comments - !count', array('!count' => format_plural($total_comment_count, '1 new comment', '@count new comments'))) ."\n" + . t('Recent comments - !count', array('!count' => format_plural($total_comment_count, '1 new comment', $total_comment_count.' new comments'))) ."\n" . $separator ."\n\n". $comment_body; } } @@ -596,6 +597,7 @@ function _notify_send($send_start = NULL) { $num_sent++; watchdog('notify', 'User %name (%mail) notified successfully.', array('%name' => $user->name, '%mail' => $user->mail), WATCHDOG_INFO); } + db_query('UPDATE {notify} SET send_last = UNIX_TIMESTAMP(NOW()) WHERE uid = %d', $user->uid); } } }