You are here

function simplenews_send_newsletter_action in Simplenews 6

Same name and namespace in other branches
  1. 6.2 simplenews_action/simplenews_action.module \simplenews_send_newsletter_action()

A configurable Drupal action. Send a simplenews newsletter. hook = cron: Send a newsletter to all subscribers. hook = user: Send a newsletter to the user who triggered the action.

Available context: $context['nid'] newsletter id $context['title'] newsletter title $context['resend'] allow resending of a previously send newsletter

See also

simplenews_send_newsletter_action_form()

simplenews_send_newsletter_action_submit()

File

simplenews_action/simplenews_action.module, line 65
simplenews_action.inc Provide actions for simplenews.

Code

function simplenews_send_newsletter_action(&$object, $context = array()) {
  if ($context['hook'] == 'cron' || $context['hook'] == 'user' || $context['hook'] == 'simplenews') {

    // Determine newsletter receipients
    $accounts = array();
    if ($context['hook'] == 'user' || $context['hook'] == 'simplenews') {
      $accounts[] = $context['account'];
    }

    // Get sent status of this newsletter. The global newsletter sent status is used not the sent status of individual emails.
    $s_status = db_result(db_query("SELECT s_status FROM {simplenews_newsletters} WHERE nid = %d", $context['nid']));

    // Send newsletter if resend is allowed OR newsletter is not yet send.
    if ($context['resend'] or $s_status == SIMPLENEWS_STATUS_SEND_NOT) {
      if ($context['hook'] == 'cron') {

        // Set newsletter sent status to pending if send by cron.
        db_query("UPDATE {simplenews_newsletters} SET s_status = %d WHERE nid = %d", SIMPLENEWS_STATUS_SEND_PENDING, $context['nid']);
      }
      simplenews_send_node($context['nid'], $accounts);
    }
    watchdog('action', 'Simplenews newsletter %title send.', array(
      '%title' => $context['title'],
    ));
  }
}