You are here

function simplenews_send_status_update in Simplenews 7

Same name and namespace in other branches
  1. 6.2 includes/simplenews.mail.inc \simplenews_send_status_update()
  2. 6 simplenews.module \simplenews_send_status_update()
  3. 7.2 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_newsletter table contains the overall sent status of each newsletter issue (node). Newsletter issues get the status pending when sending is initiated. 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.

Related topics

4 calls to simplenews_send_status_update()
drush_simplenews_spool_send in ./simplenews.drush.inc
Drush command to send the mail spool queue.
SimplenewsSendTestCase::testProgrammaticNewsletter in tests/simplenews.test
Creates and sends a node using the API.
simplenews_cron in ./simplenews.module
Implements hook_cron().
simplenews_mail_attempt_immediate_send in includes/simplenews.mail.inc
Send mail spool immediatly if cron should not be used.
1 string reference to 'simplenews_send_status_update'
simplenews_mail_attempt_immediate_send in includes/simplenews.mail.inc
Send mail spool immediatly if cron should not be used.

File

includes/simplenews.mail.inc, line 588
Simplenews email send and spool handling

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.
  $query = db_select('simplenews_newsletter', 's');
  $query
    ->innerJoin('node', 'n', 's.nid = n.nid');
  $query
    ->fields('s', array(
    'nid',
    'tid',
  ))
    ->fields('n', array(
    'tnid',
  ))
    ->condition('s.status', SIMPLENEWS_STATUS_SEND_PENDING);
  foreach ($query
    ->execute() as $newsletter) {
    $counts[$newsletter->tnid][$newsletter->nid] = simplenews_count_spool(array(
      'nid' => $newsletter->nid,
    ));
  }

  // 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 $nid => $count) {

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

  // Update overall newsletter status
  if (!empty($send)) {
    foreach ($send as $nid) {
      db_update('simplenews_newsletter')
        ->condition('nid', $nid)
        ->fields(array(
        'status' => SIMPLENEWS_STATUS_SEND_READY,
      ))
        ->execute();
    }
  }
}