| 1 |
<?php
|
| 2 |
// $Id: notify.module,v 2.77 2009/06/23 23:31:12 matt2000 Exp $
|
| 3 |
|
| 4 |
define('NOTIFY_NODE_TYPE', 'notify_node_type_');
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Implementation of hook_help().
|
| 8 |
*/
|
| 9 |
function notify_help($section) {
|
| 10 |
switch ($section) {
|
| 11 |
case 'admin/help#notify':
|
| 12 |
$output = '<p>'. t('The notification module allows users to subscribe to periodic e-mails which include all new or revised content and/or comments much like the daily news letters sent by some websites. Even if this feature is not configured for normal site users, it can be a useful feature for an administrator of a site to monitor content submissions and comment posts.') .'</p>';
|
| 13 |
$output .= '<p>'. t('The administrator sets the frequency of the e-mails in the notify administration interface. They can also set how many e-mail failures should occur before notify stops sending notifications. Note that cron must be enabled for notifications to be sent out.') .'</p>';
|
| 14 |
$output .= t('<p>You can</p><ul><li>set up your site to run tasks automatically at required intervals. For more information, see <a href="@admin-help-system">cron</a>.</li><li>administer notify <a href="@admin-settings-notify">administer >> settings >> notify</a>.</li></ul>', array('@admin-help-system' => url('admin/help/system'), '@admin-settings-notify' => url('admin/settings/notify')));
|
| 15 |
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@notify">Notify page</a>.', array('@notify' => 'http://www.drupal.org/handbook/modules/notify/')) .'</p>';
|
| 16 |
return $output;
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Menu callback; display notify settings page.
|
| 22 |
*/
|
| 23 |
function notify_admin_settings() {
|
| 24 |
$period = array(
|
| 25 |
900 => format_interval(900),
|
| 26 |
1800 => format_interval(1800),
|
| 27 |
3600 => format_interval(3600),
|
| 28 |
10800 => format_interval(10800),
|
| 29 |
21600 => format_interval(21600),
|
| 30 |
32400 => format_interval(32400),
|
| 31 |
43200 => format_interval(43200),
|
| 32 |
86400 => format_interval(86400),
|
| 33 |
172800 => format_interval(172800),
|
| 34 |
259200 => format_interval(259200),
|
| 35 |
604800 => format_interval(604800),
|
| 36 |
1209600 => format_interval(1209600),
|
| 37 |
2419200 => format_interval(2419200),
|
| 38 |
-1 => t('Never'),
|
| 39 |
);
|
| 40 |
|
| 41 |
$form = array();
|
| 42 |
|
| 43 |
$form['notify_settings'] = array(
|
| 44 |
'#type' => 'fieldset',
|
| 45 |
'#title' => t('E-mail notification settings'),
|
| 46 |
'#collapsible' => TRUE,
|
| 47 |
);
|
| 48 |
|
| 49 |
$form['notify_settings']['notify_send'] = array(
|
| 50 |
'#type' => 'select',
|
| 51 |
'#title' => 'Send notifications every',
|
| 52 |
'#default_value' => variable_get('notify_send', array(86400)),
|
| 53 |
'#options' => $period,
|
| 54 |
'#description' => 'How often should new content notifications be sent? Requires cron to be running at least this often.',
|
| 55 |
);
|
| 56 |
|
| 57 |
$form['notify_settings']['notify_send_hour'] = array(
|
| 58 |
'#type' => 'select',
|
| 59 |
'#title' => 'Hour to Send Notifications',
|
| 60 |
'#description' => 'Specify the hour (24-hour clock) in which notifications should be sent, if the frequency is one day or greater.',
|
| 61 |
'#default_value' => variable_get('notify_send_hour', 9),
|
| 62 |
'#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),
|
| 63 |
);
|
| 64 |
|
| 65 |
$form['notify_settings']['notify_attempts'] = array(
|
| 66 |
'#type' => 'select',
|
| 67 |
'#title' => 'Number of failed sends after which notifications are disabled',
|
| 68 |
'#default_value' => variable_get('notify_attempts', array(5)),
|
| 69 |
'#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20),
|
| 70 |
);
|
| 71 |
|
| 72 |
$form[notify_settings]['notify_reg_default'] = array(
|
| 73 |
'#type' => 'checkbox',
|
| 74 |
'#title' => 'Notification checkbox default on new user registration form',
|
| 75 |
'#return_value' => 1,
|
| 76 |
'#default_value' => variable_get('notify_reg_default',1),
|
| 77 |
);
|
| 78 |
|
| 79 |
$set = 'ntype';
|
| 80 |
$form[$set] = array(
|
| 81 |
'#type' => 'fieldset',
|
| 82 |
'#title' => 'Notification by node type',
|
| 83 |
'#collapsible' => true,
|
| 84 |
'#collapsed' => false,
|
| 85 |
'#description' => 'Having nothing checked defaults to sending notifications about all node types.'
|
| 86 |
);
|
| 87 |
|
| 88 |
foreach (node_get_types('types', array()) as $type => $object) {
|
| 89 |
$form[$set][NOTIFY_NODE_TYPE . $type] = array(
|
| 90 |
'#type' => 'checkbox',
|
| 91 |
'#title' => $object->name,
|
| 92 |
'#return_value' => 1,
|
| 93 |
'#default_value' => variable_get(NOTIFY_NODE_TYPE . $type, 0),
|
| 94 |
);
|
| 95 |
}
|
| 96 |
|
| 97 |
return system_settings_form($form);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_cron().
|
| 102 |
*/
|
| 103 |
function notify_cron() {
|
| 104 |
$send_last = variable_get('notify_send_last', 0);
|
| 105 |
$send_interval = variable_get('notify_send', 86400);
|
| 106 |
$send_hour = variable_get('notify_send_hour', 9);
|
| 107 |
if ( (time() - $send_last > $send_interval)
|
| 108 |
&& (date('G', time()) >= $send_hour || $send_interval < 86400)
|
| 109 |
&& ($send_interval != -1) //special case of settings to send 'never'
|
| 110 |
) {
|
| 111 |
_notify_send();
|
| 112 |
variable_set('notify_send_last', time());
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Implementation of hook_user().
|
| 118 |
*/
|
| 119 |
function notify_user($type, &$edit, &$user, $category = NULL) {
|
| 120 |
switch ($type) {
|
| 121 |
case 'delete':
|
| 122 |
db_query('DELETE FROM {notify} WHERE uid = %d', $user->uid);
|
| 123 |
break;
|
| 124 |
|
| 125 |
case 'register':
|
| 126 |
return _notify_user_reg_fields();
|
| 127 |
break;
|
| 128 |
|
| 129 |
case 'insert':
|
| 130 |
if (isset($edit['notify_decision']) && $edit['notify_decision'] == 1){
|
| 131 |
db_query('INSERT INTO {notify} (uid, status, node, teasers, comment) VALUES (%d, %d, %d, %d, %d)', $user->uid, 1, 1, 0, 0);
|
| 132 |
$edit['notify_decision'] = NULL;
|
| 133 |
}
|
| 134 |
break;
|
| 135 |
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Returns form fields to be added to User Regsitration form
|
| 142 |
*/
|
| 143 |
function _notify_user_reg_fields() {
|
| 144 |
if (!user_access('access notify')) return;
|
| 145 |
|
| 146 |
// Get the variable for how often the notifications are sent out
|
| 147 |
$period = variable_get("notify_send", 86400);
|
| 148 |
|
| 149 |
// Add a fieldset containing a checkbox for users to accept
|
| 150 |
// getting updates on the registration form.
|
| 151 |
$fields['notify_agree'] = array(
|
| 152 |
'#type' => 'fieldset',
|
| 153 |
'#title' => t('Daily Email News Notifications')
|
| 154 |
);
|
| 155 |
|
| 156 |
// Add the checkbox to the fieldset
|
| 157 |
$fields['notify_agree']['notify_decision'] = array(
|
| 158 |
'#type' => 'checkbox',
|
| 159 |
'#title' => t('Receive email notifications of news posted to this site. Notifications are sent every ' . format_interval($period).'.'),
|
| 160 |
'#return_value' => 1,
|
| 161 |
'#default_value' => variable_get('notify_reg_default',1),
|
| 162 |
'#description' => t('By accepting to receive <em>daily email news notifications</em>, you will be kept informed of news associated with ' . variable_get('site_name', 'Drupal') . '.')
|
| 163 |
);
|
| 164 |
|
| 165 |
return $fields;
|
| 166 |
}
|
| 167 |
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Implementation of hook_perm().
|
| 171 |
*/
|
| 172 |
function notify_perm() {
|
| 173 |
return array('access notify', 'administer notify');
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Implementation of hook_menu().
|
| 178 |
*
|
| 179 |
* @return array
|
| 180 |
*/
|
| 181 |
function notify_menu() {
|
| 182 |
$items = array();
|
| 183 |
|
| 184 |
$items['admin/settings/notify'] = array(
|
| 185 |
'title' => 'Notification settings',
|
| 186 |
'description' => 'Adjust settings for new content notifications sent by e-mail.',
|
| 187 |
'page callback' => 'drupal_get_form',
|
| 188 |
'page arguments' => array('notify_admin_settings'),
|
| 189 |
'access callback' => 'user_access',
|
| 190 |
'access arguments' => array('administer notify'),
|
| 191 |
'type' => MENU_NORMAL_ITEM,
|
| 192 |
);
|
| 193 |
$items['user/%user/notify'] = array(
|
| 194 |
'title' => 'Notification settings',
|
| 195 |
'page callback' => 'drupal_get_form',
|
| 196 |
'page arguments' => array('notify_user_settings_form', 1),
|
| 197 |
'access callback' => 'notify_user_access',
|
| 198 |
'access arguments' => array(1),
|
| 199 |
'type' => MENU_LOCAL_TASK
|
| 200 |
);
|
| 201 |
$items['admin/user/user/notify'] = array(
|
| 202 |
'title' => 'Notifications',
|
| 203 |
'page callback' => 'drupal_get_form',
|
| 204 |
'page arguments' => array('notify_admin_users'),
|
| 205 |
'access callback' => 'user_access',
|
| 206 |
'access arguments' => array('administer notify'),
|
| 207 |
'type' => MENU_LOCAL_TASK,
|
| 208 |
);
|
| 209 |
|
| 210 |
return $items;
|
| 211 |
}
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Checks access to notifications settings tab
|
| 215 |
*/
|
| 216 |
function notify_user_access($account = NULL)
|
| 217 |
{
|
| 218 |
return $account->uid &&
|
| 219 |
(
|
| 220 |
// Always let users view their own profile.
|
| 221 |
($GLOBALS['user']->uid == $account->uid && user_access('access notify')) ||
|
| 222 |
// Administrators can view all accounts.
|
| 223 |
user_access('administer notify')
|
| 224 |
);
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* Register the themeing the form data into a table at
|
| 229 |
* admin/user/user/notify
|
| 230 |
*
|
| 231 |
* @return array
|
| 232 |
*/
|
| 233 |
function notify_theme() {
|
| 234 |
return array(
|
| 235 |
'notify_admin_users' => array(
|
| 236 |
'arguments' => array('form' => NULL)
|
| 237 |
)
|
| 238 |
);
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Menu callback; show user notification options.
|
| 243 |
*/
|
| 244 |
function notify_user_settings_form(&$form_state, $arg) {
|
| 245 |
global $user;
|
| 246 |
if ($user->uid != $arg->uid && !user_access('administer notify') ) {
|
| 247 |
drupal_access_denied();
|
| 248 |
return;
|
| 249 |
}
|
| 250 |
|
| 251 |
$account = user_load(array('uid' => $arg->uid));
|
| 252 |
if (!is_object($account)) {
|
| 253 |
drupal_not_found();
|
| 254 |
return;
|
| 255 |
}
|
| 256 |
|
| 257 |
$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);
|
| 258 |
$notify = db_fetch_object($result);
|
| 259 |
$form = array();
|
| 260 |
if (!$notify->mail) {
|
| 261 |
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');
|
| 262 |
}
|
| 263 |
|
| 264 |
$form['notify_page_master'] = array('#type' => 'fieldset', '#title' => 'Master switch');
|
| 265 |
$form['notify_page_master']['status'] = array('#type' => 'radios',
|
| 266 |
'#title' => 'Notify status',
|
| 267 |
'#default_value' => $notify->status,
|
| 268 |
'#options' => array(t('Disabled'), t('Enabled')),
|
| 269 |
'#description' => t('Do you wish to receive periodic e-mails when new content is posted?'),
|
| 270 |
);
|
| 271 |
|
| 272 |
$form['notify_page_detailed'] = array('#type' => 'fieldset', '#title' => t('Detailed settings'));
|
| 273 |
$form['notify_page_detailed']['node'] = array('#type' => 'radios',
|
| 274 |
'#title' => 'Notify new content',
|
| 275 |
'#default_value' => $notify->node,
|
| 276 |
'#options' => array(t('Disabled'), t('Enabled')),
|
| 277 |
'#description' => t('Include new content in the notification mail.'),
|
| 278 |
);
|
| 279 |
$form['notify_page_detailed']['teasers'] = array('#type' => 'radios',
|
| 280 |
'#title' => 'Content',
|
| 281 |
'#default_value' => $notify->teasers,
|
| 282 |
'#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')),
|
| 283 |
'#description' => t('Select the amount of each post that you would like to see in your notification e-mails.'),
|
| 284 |
);
|
| 285 |
$form['notify_page_detailed']['comment'] = array('#type' => 'radios',
|
| 286 |
'#title' => t('Notify new comments'),
|
| 287 |
'#default_value' => $notify->comment,
|
| 288 |
'#options' => array(t('Disabled'), t('Enabled')),
|
| 289 |
'#description' => t('Include new comments in the notification mail.'),
|
| 290 |
);
|
| 291 |
$form['uid'] = array('#type' => 'value', '#value' => $account->uid);
|
| 292 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
|
| 293 |
|
| 294 |
return $form;
|
| 295 |
}
|
| 296 |
|
| 297 |
function notify_user_settings_form_submit($form, &$form_state) {
|
| 298 |
unset($form);
|
| 299 |
db_query('DELETE FROM {notify} WHERE uid = %d', $form_state['values']['uid']);
|
| 300 |
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']);
|
| 301 |
drupal_set_message(t('Notify settings saved.'));
|
| 302 |
}
|
| 303 |
|
| 304 |
/**
|
| 305 |
* Menu callback; show admininster user notification settings form.
|
| 306 |
*/
|
| 307 |
function notify_admin_users() {
|
| 308 |
$form = array();
|
| 309 |
$form['#tree'] = TRUE;
|
| 310 |
$form['info'] = array('#value' => t('The following table shows all users that have notifications enabled.'));
|
| 311 |
|
| 312 |
$form['users'] = array();
|
| 313 |
|
| 314 |
$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');
|
| 315 |
while ($notify = db_fetch_object($result)) {
|
| 316 |
$form['users'][$notify->uid] = array();
|
| 317 |
$form['users'][$notify->uid]['name'] = array('#type' => 'markup', '#value' => theme('username', $notify));
|
| 318 |
$form['users'][$notify->uid]['mail'] = array('#type' => 'markup', '#value' => $notify->mail);
|
| 319 |
$form['users'][$notify->uid]['node'] = array('#type' => 'checkbox', '#default_value' => $notify->node);
|
| 320 |
$form['users'][$notify->uid]['teasers'] = array('#type' => 'select', '#default_value' => $notify->teasers, '#options' => array(t('Title only'), t('Title + Teaser'), t('Title + Body')));
|
| 321 |
$form['users'][$notify->uid]['comment'] = array('#type' => 'checkbox', '#default_value' => $notify->comment);
|
| 322 |
$form['users'][$notify->uid]['attempts'] = array('#type' => 'textfield', '#size' => 2, '#default_value' => $notify->attempts ? intval($notify->attempts) : 0);
|
| 323 |
}
|
| 324 |
|
| 325 |
$form['flush'] = array(
|
| 326 |
'#title' => t('Flush e-mail queue'),
|
| 327 |
'#type' => 'checkbox',
|
| 328 |
'#default_value' => FALSE,
|
| 329 |
'#description' => t('Send out any pending notification e-mails currently in queue.'),
|
| 330 |
);
|
| 331 |
|
| 332 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
|
| 333 |
|
| 334 |
return $form;
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Submit for the notify_admin form.
|
| 339 |
*/
|
| 340 |
function notify_admin_users_submit($form, &$form_state) {
|
| 341 |
unset($form);
|
| 342 |
|
| 343 |
if ($form_state['values']['users']) {
|
| 344 |
foreach ($form_state['values']['users'] as $uid => $settings) {
|
| 345 |
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);
|
| 346 |
}
|
| 347 |
}
|
| 348 |
drupal_set_message(t('Notify settings saved.'));
|
| 349 |
|
| 350 |
if ($form_state['values']['flush']) {
|
| 351 |
list($num_sent, $num_failed) = _notify_send();
|
| 352 |
variable_set('notify_send_last', time());
|
| 353 |
|
| 354 |
if ($num_sent > 0) {
|
| 355 |
drupal_set_message(t('!count pending notification e-mails have been sent.', array('!count' => $num_sent)));
|
| 356 |
}
|
| 357 |
elseif ($num_failed > 0) {
|
| 358 |
drupal_set_message(t('!count notification e-mails could not be sent.', array('!count' => $num_failed)), 'error');
|
| 359 |
}
|
| 360 |
else {
|
| 361 |
drupal_set_message(t('No notification e-mails needed to be sent.'));
|
| 362 |
}
|
| 363 |
}
|
| 364 |
}
|
| 365 |
|
| 366 |
/**
|
| 367 |
* Theme function to theme the admin user settings form in a table format.
|
| 368 |
*/
|
| 369 |
function theme_notify_admin_users($form) {
|
| 370 |
$output = drupal_render($form['info']);
|
| 371 |
$header = array(t('Username'), t('E-mail address'), t('Content'), t('Teasers'), t('Comment'), t('Failed attempts'));
|
| 372 |
|
| 373 |
$rows = array();
|
| 374 |
foreach (element_children($form['users']) as $uid) {
|
| 375 |
$row = array();
|
| 376 |
foreach (element_children($form['users'][$uid]) as $entry_key) {
|
| 377 |
unset($form['users'][$uid][$entry_key]['#title']);
|
| 378 |
|
| 379 |
$row[] = drupal_render($form['users'][$uid][$entry_key]);
|
| 380 |
}
|
| 381 |
$rows[] = $row;
|
| 382 |
}
|
| 383 |
|
| 384 |
if (!$rows) {
|
| 385 |
$rows[] = array(array('data' => t('No users have notifications enabled.'), 'colspan' => 6));
|
| 386 |
}
|
| 387 |
$output .= theme('table', $header, $rows);
|
| 388 |
|
| 389 |
$output .= drupal_render($form);
|
| 390 |
return $output;
|
| 391 |
}
|
| 392 |
|
| 393 |
/**
|
| 394 |
* Formatting of outgoing mail, taken from mail.inc, part of project.module
|
| 395 |
*/
|
| 396 |
function _notify_content($node, $notify) {
|
| 397 |
static $i = 0;
|
| 398 |
|
| 399 |
switch ($notify->teasers) {
|
| 400 |
case 0:
|
| 401 |
return;
|
| 402 |
case 1:
|
| 403 |
$txt = check_markup($node->teaser, $node->format, FALSE);
|
| 404 |
break;
|
| 405 |
case 2:
|
| 406 |
$txt = check_markup($node->body, $node->format, FALSE);
|
| 407 |
}
|
| 408 |
|
| 409 |
return drupal_html_to_text($txt);
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
* Helper function to send the notification email.
|
| 414 |
*
|
| 415 |
* TODO: Needs some cleanup and themability.
|
| 416 |
*/
|
| 417 |
function _notify_send() {
|
| 418 |
$period = variable_get('notify_send_last', time() - variable_get('notify_send', 86400));
|
| 419 |
$separator = '------------------------------------------------------------------------------';
|
| 420 |
$mini_separator = '---';
|
| 421 |
|
| 422 |
$num_sent = 0;
|
| 423 |
$num_failed = 0;
|
| 424 |
|
| 425 |
_notify_switch_user(); // Store current user
|
| 426 |
|
| 427 |
// Fetch all node type authorized by notify settings
|
| 428 |
$ntype = array();
|
| 429 |
foreach (node_get_types() as $type => $name) {
|
| 430 |
if (variable_get(NOTIFY_NODE_TYPE . $type, 0)) {
|
| 431 |
$ntype[] = $type;
|
| 432 |
}
|
| 433 |
if (count($ntype) >= 1) {
|
| 434 |
$reqntype = "AND (n.type = '". implode("' OR n.type = '", $ntype) ."') ";
|
| 435 |
}
|
| 436 |
else {
|
| 437 |
$reqntype = '';
|
| 438 |
}
|
| 439 |
}
|
| 440 |
|
| 441 |
// Fetch users with notify enabled
|
| 442 |
$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));
|
| 443 |
|
| 444 |
while ($user = db_fetch_object($uresult)) {
|
| 445 |
// Switch current user to this account to use node_access functions, etc.
|
| 446 |
_notify_switch_user($user->uid);
|
| 447 |
|
| 448 |
// Fetch all new nodes and 'load' it to get proper body, etc.
|
| 449 |
$nresult = db_query(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE (n.status = 1 OR n.moderate = 1) '. $reqntype . ' AND ((n.created > %d AND n.created <= %d) OR (n.changed > %d AND n.changed <= %d)) ORDER BY n.created'), $period, time(), $period, time());
|
| 450 |
$nodes = array();
|
| 451 |
while ($node = db_fetch_object($nresult)) {
|
| 452 |
$nodes[$node->nid] = node_load($node->nid);
|
| 453 |
}
|
| 454 |
|
| 455 |
// Fetch new comments.
|
| 456 |
$comments = array();
|
| 457 |
if (module_exists('comment')) {
|
| 458 |
$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());
|
| 459 |
while ($comment = db_fetch_object($cresult)) {
|
| 460 |
$comments[$comment->nid][] = $comment;
|
| 461 |
}
|
| 462 |
}
|
| 463 |
|
| 464 |
$node_body = '';
|
| 465 |
$comment_body = '';
|
| 466 |
|
| 467 |
// Write new node content to e-mail if user has permissions and nodes are
|
| 468 |
// ready to be sent.
|
| 469 |
if ($user->node && user_access('access content') && count($nodes)) {
|
| 470 |
|
| 471 |
$node_count = 0;
|
| 472 |
foreach ($nodes as $node) {
|
| 473 |
// Skip to next if this user is NOT allowed to view this node.
|
| 474 |
if (!node_access('view', $node)) {
|
| 475 |
continue;
|
| 476 |
}
|
| 477 |
|
| 478 |
// TODO: Add functionality to hook into moderation modules?
|
| 479 |
if ($node->moderate == 1) {
|
| 480 |
$status = t('Queued');
|
| 481 |
}
|
| 482 |
elseif ($node->status == 1) {
|
| 483 |
$status = t('Published');
|
| 484 |
}
|
| 485 |
elseif ($node->status == 0) {
|
| 486 |
$status = t('Unpublished');
|
| 487 |
}
|
| 488 |
|
| 489 |
if ($node_count > 0) {
|
| 490 |
$node_body .= $mini_separator ."\n\n";
|
| 491 |
}
|
| 492 |
$node_body .= ++$node_count .'. '. $node->title ."\n";
|
| 493 |
$node_body .= t('!status !type by !author', array('!status' => $status, '!type' => node_get_types('name', $node), '!author' => ($node->name ? $node->name : variable_get('anonymous', 'Anonymous')))) ."\n";
|
| 494 |
$node_body .= '[ '. url('node/'. $node->nid, array('absolute' => TRUE)) ." ]\n\n";
|
| 495 |
$node_body .= _notify_content($node, $user) ."\n";
|
| 496 |
}
|
| 497 |
|
| 498 |
// Prepend node e-mail header as long as user could access at least one node.
|
| 499 |
if ($node_count > 0) {
|
| 500 |
$node_body = $separator ."\n"
|
| 501 |
. t('Recent content - !count', array('!count' => format_plural(count($nodes), '1 new post', '@count new posts'))) ."\n"
|
| 502 |
. $separator ."\n\n". $node_body;
|
| 503 |
}
|
| 504 |
}
|
| 505 |
|
| 506 |
// Write new comments to e-mail if user has permissions and there are
|
| 507 |
// comments to be sent.
|
| 508 |
if ($user->comment && user_access('access comments') && count($comments)) {
|
| 509 |
$total_comment_count = 0;
|
| 510 |
foreach ($comments as $nid => $comment) {
|
| 511 |
// If we don't already have the node, fetch it.
|
| 512 |
if (!isset($nodes[$nid])) {
|
| 513 |
$nodes[$nid] = node_load($nid);
|
| 514 |
}
|
| 515 |
|
| 516 |
// Don't show comments if we're not allowed to view this node.
|
| 517 |
if (!node_access('view', $nodes[$nid])) {
|
| 518 |
continue;
|
| 519 |
}
|
| 520 |
|
| 521 |
if ($comment_body) {
|
| 522 |
$comment_body .= $mini_separator ."\n\n";
|
| 523 |
}
|
| 524 |
$comment_body .= t('!count attached to !type posted by !author: !title', array('!count' => format_plural(count($comment), '1 new comment', '@count 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";
|
| 525 |
|
| 526 |
$comment_count = 0;
|
| 527 |
foreach ($comment as $c) {
|
| 528 |
$comment_body .= ' '. ++$comment_count .'. '. t('!title by !author', array('!title' => $c->subject, '!author' => ($c->name ? $c->name : variable_get('anonymous', 'Anonymous')))) ."\n"
|
| 529 |
.' '. url('node/'. $nid, array('fragment' => 'comment-'. $c->cid, 'absolute' => TRUE)) ."\n\n";
|
| 530 |
$total_comment_count++;
|
| 531 |
}
|
| 532 |
}
|
| 533 |
|
| 534 |
if ($total_comment_count > 0) {
|
| 535 |
$comment_body = $separator ."\n"
|
| 536 |
. t('Recent comments - !count', array('!count' => format_plural($total_comment_count, '1 new comment', '@count new comments'))) ."\n"
|
| 537 |
. $separator ."\n\n". $comment_body;
|
| 538 |
}
|
| 539 |
}
|
| 540 |
|
| 541 |
$body = $node_body . $comment_body;
|
| 542 |
|
| 543 |
// If there was anything new, send mail.
|
| 544 |
if ($body) {
|
| 545 |
// Set up initial values for e-mail.
|
| 546 |
|
| 547 |
$headers = array();//'From' => "$from_name <$from>");
|
| 548 |
if (!drupal_mail('notify', 'send', $user->mail, user_preferred_language($user), array('content' => wordwrap($body, 72)))) {
|
| 549 |
$num_failed++;
|
| 550 |
db_query('UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d', $user->uid);
|
| 551 |
watchdog('notify', 'User %name (%mail) could not be notified. Mail error.', array('%name' => $user->name, '%mail' => $user->mail), WATCHDOG_ERROR);
|
| 552 |
}
|
| 553 |
else {
|
| 554 |
$num_sent++;
|
| 555 |
watchdog('notify', 'User %name (%mail) notified successfully.', array('%name' => $user->name, '%mail' => $user->mail), WATCHDOG_INFO);
|
| 556 |
}
|
| 557 |
}
|
| 558 |
}
|
| 559 |
// Restore user.
|
| 560 |
_notify_switch_user();
|
| 561 |
return array($num_sent, $num_failed);
|
| 562 |
}
|
| 563 |
|
| 564 |
function notify_mail($key, &$message, $params) {
|
| 565 |
global $user;
|
| 566 |
|
| 567 |
$message['subject'] = t('!sitename new content notification for !username', array('!username' => $user->name, '!sitename' => variable_get('site_name', 'Drupal')));
|
| 568 |
$message['body'] = t('Greetings !user,', array('!user' => $user->name)) ."\n\n";
|
| 569 |
$message['body'] .= $params['content'];
|
| 570 |
$message['body'] .= "\n-- \n";
|
| 571 |
$message['body'] .= t('This is an automatic e-mail from !sitename.', array('!sitename' => variable_get('site_name', 'Drupal'))) ."\n";
|
| 572 |
$message['body'] .= t('To stop receiving these e-mails, change your notification preferences at !notify-url', array('!notify-url' => url("user/$user->uid/notify" , array('absolute' => TRUE)))) ."\n";
|
| 573 |
}
|
| 574 |
|
| 575 |
/**
|
| 576 |
* Switch from original user to mail submission user and back.
|
| 577 |
*
|
| 578 |
* NOTE: Copied from mailhandler
|
| 579 |
*
|
| 580 |
* Note: You first need to run _notify_switch_user without
|
| 581 |
* argument to store the current user. Call _notify_switch_user
|
| 582 |
* without argument to set the user back to the original user.
|
| 583 |
*
|
| 584 |
* @param $uid The user ID to switch to
|
| 585 |
*/
|
| 586 |
function _notify_switch_user($uid = NULL) {
|
| 587 |
global $user;
|
| 588 |
static $orig_user = array();
|
| 589 |
|
| 590 |
if (isset($uid)) {
|
| 591 |
// Should a user visit cron.php, or should this module be invoked via poormanscron and _notify_send() does not complete,
|
| 592 |
// the visitor will end up logged in as the "switched to user" for subsequent requests unless we disable saving the session
|
| 593 |
// until we are sure we're the invoking user again.
|
| 594 |
session_save_session(FALSE);
|
| 595 |
$user = user_load(array('uid' => $uid));
|
| 596 |
}
|
| 597 |
// Retrieve the initial user, can be called multiple times.
|
| 598 |
else if (count($orig_user)) {
|
| 599 |
$user = array_shift($orig_user);
|
| 600 |
array_unshift($orig_user, $user);
|
| 601 |
session_save_session(TRUE);
|
| 602 |
}
|
| 603 |
// Store the initial user.
|
| 604 |
else {
|
| 605 |
$orig_user[] = $user;
|
| 606 |
}
|
| 607 |
}
|