| 1 |
<?php |
<?php |
| 2 |
/* $Id$ */ |
/* $Id: notify_by_views.module,v 1.2 2008/08/27 22:37:53 matt2000 Exp $ */ |
| 3 |
/** |
/** |
| 4 |
* Display help and module information |
* Display help and module information |
| 5 |
* @param section which section of the site we're displaying help |
* @param section which section of the site we're displaying help |
| 132 |
'#title' => t('Arguments for ') . $view_name, |
'#title' => t('Arguments for ') . $view_name, |
| 133 |
'#default_value' => variable_get('notify_by_views_'. $view_name .'_args', '' ), |
'#default_value' => variable_get('notify_by_views_'. $view_name .'_args', '' ), |
| 134 |
); |
); |
| 135 |
|
|
| 136 |
|
$form['notify_by_views_set']['notify_by_views_'. $view_name .'_template'] = array( |
| 137 |
|
'#type' => 'textarea', |
| 138 |
|
'#title' => t('Template for ') . $view_name, |
| 139 |
|
'#default_value' => variable_get("notify_by_views_{$view_name}_template", _notify_by_views_default_text()), |
| 140 |
|
); |
| 141 |
} |
} |
| 142 |
|
|
| 143 |
$form['notify_by_views_set']['notify_by_views_default_schedule'] = array( |
$form['notify_by_views_set']['notify_by_views_default_schedule'] = array( |
| 273 |
* is a valid value from notify_by_views.frequency |
* is a valid value from notify_by_views.frequency |
| 274 |
*/ |
*/ |
| 275 |
|
|
| 276 |
function _notify_by_views_mail_send($nodes, $frequency, $view_name) { |
function _notify_by_views_mail_send($nodes, $frequency, $view_name, $test = FALSE) { |
| 277 |
|
|
| 278 |
if ($frequency == 'never') { |
if ($frequency == 'never') { |
| 279 |
drupal_set_message(t("You're trying to send to users who have selected to 'never' receive notifications. This is not allowed."), 'error'); |
drupal_set_message(t("You're trying to send to users who have selected to 'never' receive notifications. This is not allowed."), 'error'); |
| 284 |
$nodes = array($nodes->nid => $nodes); |
$nodes = array($nodes->nid => $nodes); |
| 285 |
} |
} |
| 286 |
|
|
|
$separator = '------------------------------------------------------------------------------'; |
|
|
$mini_separator = '---'; |
|
|
|
|
| 287 |
$num_sent = 0; |
$num_sent = 0; |
| 288 |
$num_failed = 0; |
$num_failed = 0; |
| 289 |
|
|
| 304 |
// Fetch comments. |
// Fetch comments. |
| 305 |
if ($frequency != 'immediate') { //increase performance by only checking for comments on cron runs |
if ($frequency != 'immediate') { //increase performance by only checking for comments on cron runs |
| 306 |
$nids = "(c.nid=". implode(" OR c.nid=", array_keys($nodes)) .")"; //what nodes are we looking at? |
$nids = "(c.nid=". implode(" OR c.nid=", array_keys($nodes)) .")"; //what nodes are we looking at? |
| 307 |
$cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name FROM {comments} c WHERE c.status = %d AND %s ORDER BY c.nid, c.timestamp', 'c'), COMMENT_PUBLISHED, $nids); |
$cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.comment, c.name FROM {comments} c WHERE c.status = %d AND %s ORDER BY c.nid, c.timestamp', 'c'), COMMENT_PUBLISHED, $nids); |
| 308 |
$comments = array(); |
$comments = array(); |
| 309 |
while ($comment = db_fetch_object($cresult)) { |
while ($comment = db_fetch_object($cresult)) { |
| 310 |
$comments[$comment->nid][] = $comment; |
$comments[$comment->nid][] = $comment; |
| 320 |
if (user_access('access content') && count($nodes)) { |
if (user_access('access content') && count($nodes)) { |
| 321 |
$node_count = 0; |
$node_count = 0; |
| 322 |
|
|
| 323 |
|
//get a template |
| 324 |
|
$template = variable_get("notify_by_views_{$view_name}_template", _notify_by_views_default_text()); |
| 325 |
|
//break up template into its parts |
| 326 |
|
$tpl = explode('!section',$template); |
| 327 |
|
|
| 328 |
foreach ($nodes as $node) { |
foreach ($nodes as $node) { |
| 329 |
// Skip to next if this user is NOT allowed to view this node. |
// Skip to next if this user is NOT allowed to view this node. |
| 330 |
if (!node_access('view', $node)) { |
if (!node_access('view', $node)) { |
| 348 |
$status = t('Unpublished'); |
$status = t('Unpublished'); |
| 349 |
} |
} |
| 350 |
|
|
| 351 |
if ($node_count > 0) { |
|
| 352 |
$node_body .= $mini_separator ."\n\n"; |
$title = t('@title', array('@title' => $node->title)); |
| 353 |
} |
$author = ($node->name ? $node->name : variable_get('anonymous', 'Anonymous')); |
| 354 |
$node_body .= ++$node_count .'. '. t('@title', array('@title' => $node->title)) ."\n"; |
$info = $status ." ". node_get_types('name', $node); |
| 355 |
$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"; |
$url = url('node/'. $node->nid, NULL, NULL, TRUE); |
| 356 |
$node_body .= '[ '. url('node/'. $node->nid, NULL, NULL, TRUE) ." ]\n\n"; |
$content = $html ? _notify_content_html($node, $user) :_notify_content($node, $user); |
| 357 |
$node_body .= _notify_content($node, $user) ."\n"; |
++$node_count; |
| 358 |
|
|
| 359 |
|
//replace tokens in template with their content |
| 360 |
|
$node_body .= strtr($tpl[2], array( |
| 361 |
|
'!title' => $title, |
| 362 |
|
'!author' => $author, |
| 363 |
|
'!num' => $node_count, |
| 364 |
|
'!info' => $info, |
| 365 |
|
'!url' => $url, |
| 366 |
|
'!content' => $content, |
| 367 |
|
)); //construct node display from template |
| 368 |
} |
} |
| 369 |
|
|
| 370 |
// Prepend node e-mail header as long as user could access at least one node. |
// Prepend node e-mail header as long as user could access at least one node. |
| 371 |
if ($node_count > 0) { |
if ($node_count > 0) { |
| 372 |
$node_body = $separator ."\n" |
$node_body = strtr($tpl[1], array( |
| 373 |
. t('Recent content - @count', array('@count' => format_plural(count($nodes), '1 new post', '@count new posts'))) ."\n" |
'!header' => t('Posts'), |
| 374 |
. $separator ."\n\n". $node_body; |
'!count' => $node_count, |
| 375 |
|
)) |
| 376 |
|
. $node_body; //add template node header |
| 377 |
} |
} |
| 378 |
} |
} |
| 379 |
|
|
| 392 |
continue; |
continue; |
| 393 |
} |
} |
| 394 |
|
|
|
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' => $nodes[$nid]->title, '@type' => node_get_types('name', $nodes[$nid]), '@author' => $nodes[$nid]->name ? $nodes[$nid]->name : variable_get('anonymous', 'Anonymous'))) ."\n"; |
|
|
|
|
| 395 |
$comment_count = 0; |
$comment_count = 0; |
| 396 |
foreach ($comment as $c) { |
foreach ($comment as $c) { |
| 397 |
$comment_body .= ' '. ++$comment_count .'. '. t('@title by @author', array('@title' => $c->subject, '@author' => ($c->name ? $c->name : variable_get(anonymous, 'Anonymous')))) ."\n" |
|
| 398 |
.' '. url('node/'. $nid, NULL, 'comment-'. $c->cid, TRUE) ."\n\n"; |
$title = $c->subject; |
| 399 |
$total_comment_count++; |
$author = $c->name ? $c->name : variable_get(anonymous, 'Anonymous'); |
| 400 |
} |
$info = t('Comment Attached to @type posted by @author: @title', array( |
| 401 |
|
'@title' => $nodes[$nid]->title, |
| 402 |
|
'@type' => node_get_types('name', $nodes[$nid]), |
| 403 |
|
'@author' => $nodes[$nid]->name ? $nodes[$nid]->name : variable_get('anonymous', 'Anonymous'))) ."\n"; |
| 404 |
|
$content = $c->comment; |
| 405 |
|
$url = url('node/'. $nid, NULL, 'comment-'. $c->cid, TRUE); |
| 406 |
|
|
| 407 |
|
++$total_comment_count; |
| 408 |
|
|
| 409 |
|
$comment_body .= strtr($tpl[2], array( |
| 410 |
|
'!title' => $title, |
| 411 |
|
'!author' => $author, |
| 412 |
|
'!num' => $total_comment_count, |
| 413 |
|
'!info' => $info, |
| 414 |
|
'!url' => $url, |
| 415 |
|
'!content' => $content, |
| 416 |
|
)); //construct comment display from template |
| 417 |
|
} |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
if ($total_comment_count > 0) { |
if ($total_comment_count > 0) { |
| 421 |
$comment_body = $separator ."\n" |
|
| 422 |
. t('Recent comments - @count', array('@count' => format_plural($total_comment_count, '1 new comment', '@count new comments'))) ."\n" |
$comment_body = strtr($tpl[1], array( |
| 423 |
. $separator ."\n\n". $comment_body; |
'!header' => t('Comments'), |
| 424 |
|
'!count' => $total_comment_count, |
| 425 |
|
)) |
| 426 |
|
. $comment_body; //add template comment section heading |
| 427 |
} |
} |
| 428 |
} |
} |
| 429 |
|
|
| 436 |
$from_name = variable_get('site_name', 'Drupal'); |
$from_name = variable_get('site_name', 'Drupal'); |
| 437 |
$subject = t('@sitename new content notification for @username', array('@username' => $user->name, '@sitename' => variable_get('site_name', 'Drupal'))); |
$subject = t('@sitename new content notification for @username', array('@username' => $user->name, '@sitename' => variable_get('site_name', 'Drupal'))); |
| 438 |
|
|
| 439 |
$body = t('Greetings @user,', array('@user' => $user->name)) ."\n\n". $body; |
$body = strtr($tpl[0], array('!uname' => $user->name)) . $body; //add template header |
| 440 |
|
$body .= "\n". t('This is an automatic e-mail from @sitename.', array('@sitename' => variable_get('site_name', 'Drupal')))."\n"; |
|
$body .= "\n-- \n"; |
|
|
$body .= t('This is an automatic e-mail from @sitename.', array('@sitename' => variable_get('site_name', 'Drupal')))."\n"; |
|
| 441 |
$body .= t('To stop receiving these e-mails, change your notification preferences at @notify-url', array('@notify-url' => url("user/$user->uid/notify" , NULL, NULL, TRUE)))."\n"; |
$body .= t('To stop receiving these e-mails, change your notification preferences at @notify-url', array('@notify-url' => url("user/$user->uid/notify" , NULL, NULL, TRUE)))."\n"; |
| 442 |
|
|
| 443 |
$headers = array();//'From' => "$from_name <$from>"); |
$headers = array();//'From' => "$from_name <$from>"); |
| 444 |
if (!drupal_mail('notify_by_views_'. $view_name, $user->mail, $subject, wordwrap($body, 72), $from, $headers)) { |
|
| 445 |
|
//Here's where we really send it, unless test is True |
| 446 |
|
$success = $test ? TRUE : drupal_mail('notify_by_views_'. $view_name, $user->mail, $subject, wordwrap($body, 72), $from, $headers); |
| 447 |
|
|
| 448 |
|
if (!$success) { |
| 449 |
$num_failed++; |
$num_failed++; |
| 450 |
db_query('UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d', $user->uid); |
db_query('UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d', $user->uid); |
| 451 |
watchdog('error', t('Notify_by_Views: User %name (%mail) could not be notified. Mail error.', array('%name' => $user->name, '%mail' => $user->mail))); |
watchdog('error', t('Notify_by_Views: User %name (%mail) could not be notified. Mail error.', array('%name' => $user->name, '%mail' => $user->mail))); |
| 452 |
} |
} |
| 453 |
else { |
else { |
| 454 |
$num_sent++; |
$num_sent++; |
| 455 |
db_query('UPDATE {notify_by_views_schedule} SET last_mailing = %d WHERE uid = %d', time(), $user->uid); |
if (!$test) db_query('UPDATE {notify_by_views_schedule} SET last_mailing = %d WHERE uid = %d', time(), $user->uid); |
| 456 |
watchdog('user', t('Notify_by_Views: User %name (%mail) notified successfully.', array('%name' => $user->name, '%mail' => $user->mail))); |
watchdog('user', t('Notify_by_Views: User %name (%mail) notified successfully.', array('%name' => $user->name, '%mail' => $user->mail))); |
| 457 |
} |
} |
| 458 |
} |
} |
| 459 |
} |
} |
| 460 |
// Restore user. |
// Restore user. |
| 461 |
_notify_switch_user(); |
_notify_switch_user(); |
| 462 |
return array($num_sent, $num_failed); |
|
| 463 |
|
$ret = array($num_sent, $num_failed); |
| 464 |
|
if ($test) $ret[] = $body; |
| 465 |
|
|
| 466 |
|
return $ret; |
| 467 |
|
} |
| 468 |
|
|
| 469 |
|
/** |
| 470 |
|
* Returns a default text template for messages |
| 471 |
|
*/ |
| 472 |
|
function _notify_by_views_default_text() { |
| 473 |
|
$separator = "------------------------------------------------------------------------------\n"; |
| 474 |
|
return t("Greetings !uname,\n\n!section{$separator}!header\nNew items: !count\n{$separator}!section!num. !title by !author \n!info\n[ !url ]\n\n!content\n\n---\n!section{$separator}"); |
| 475 |
} |
} |
| 476 |
|
|
| 477 |
/** |
/** |
| 496 |
//drupal_set_message($view_name ." for User $user->uid : ". var_export($args,TRUE)); |
//drupal_set_message($view_name ." for User $user->uid : ". var_export($args,TRUE)); |
| 497 |
$view_results = views_build_view('items', $view, $args); |
$view_results = views_build_view('items', $view, $args); |
| 498 |
|
|
| 499 |
drupal_set_message("View results for user $user->uid : "); |
//drupal_set_message("View results for user $user->uid : "); |
| 500 |
dvm($view_results['items']); //debug |
//dvm($view_results['items']); //debug |
| 501 |
|
|
| 502 |
|
|
| 503 |
if (count($view_results['items'])) { |
if (count($view_results['items'])) { |