You are here

function simplenews_send_status_update in Simplenews 6

Same name and namespace in other branches
  1. 6.2 includes/simplenews.mail.inc \simplenews_send_status_update()
  2. 7.2 includes/simplenews.mail.inc \simplenews_send_status_update()
  3. 7 includes/simplenews.mail.inc \simplenews_send_status_update()

Update newsletter sent status.

Set newsletter sent status based on email sent status in spool table. Translated and untranslated nodes get a different treatment.

The spool table holds data for emails to be sent and (optionally) already send emails. The simplenews_newsletters table contains the overall sent status of each newsletter issue (node). Newsletter issues get the status pending when sending is innitiated. As long as unsend emails exist in the spool, the status of the newsletter remains unsend. When no pending emails are found the newsletter status is set 'send'.

Translated newsletters are a group of nodes that share the same tnid ({node}.tnid). Only one node of the group is found in the spool, but all nodes should share the same state. Therefore they are checked for the combined number of emails in the spool.

1 call to simplenews_send_status_update()
simplenews_cron in ./simplenews.module
Implementation of hook_cron().

File

./simplenews.module, line 1949
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_send_status_update() {
  $counts = array();

  // number pending of emails in the spool
  $sum = array();

  // sum of emails in the spool per tnid (translation id)
  $send = array();

  // nodes with the status 'send'
  // For each pending newsletter count the number of pending emails in the spool.
  $result = db_query("SELECT s.nid, s.vid, s.tid, n.tnid FROM {simplenews_newsletters} s JOIN {node} n ON s.nid = n.nid AND s.vid = n.vid WHERE s.s_status = %d", SIMPLENEWS_STATUS_SEND_PENDING);
  while ($newsletter = db_fetch_object($result)) {

    // nid-vid are combined in one unique key.
    $counts[$newsletter->tnid][$newsletter->nid . '-' . $newsletter->vid] = simplenews_count_spool($newsletter->nid, $newsletter->vid);
  }

  // Determine which nodes are send per translation group and per individual node.
  foreach ($counts as $tnid => $node_count) {

    // The sum of emails per tnid is the combined status result for the group of translated nodes.
    // Untranslated nodes have tnid == 0 which will be ignored later.
    $sum[$tnid] = array_sum($node_count);
    foreach ($node_count as $nidvid => $count) {

      // Translated nodes (tnid != 0)
      if ($tnid != '0' && $sum[$tnid] == '0') {
        $send[] = $nidvid;
      }
      elseif ($tnid == '0' && $count == '0') {
        $send[] = $nidvid;
      }
    }
  }

  // Update overall newsletter status
  if (!empty($send)) {
    foreach ($send as $nidvid) {

      // Split the combined key 'nid-vid'
      $nid = strtok($nidvid, '-');
      $vid = strtok('-');
      db_query("UPDATE {simplenews_newsletters} SET s_status = '%s' WHERE nid = %d AND vid = %d", SIMPLENEWS_STATUS_SEND_READY, $nid, $vid);
    }
  }
}