You are here

function simplenews_send_status_update in Simplenews 7.2

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 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 612
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 = new EntityFieldQuery();
  $nodes = $query
    ->entityCondition('entity_type', 'node')
    ->fieldCondition(variable_get('simplenews_issue_status_field', 'simplenews_issue_status'), 'value', SIMPLENEWS_STATUS_SEND_PENDING)
    ->execute();
  if ($nodes) {
    foreach (reset($nodes) as $node) {
      $counts[simplenews_issue_newsletter_id($node)][$node->nid] = simplenews_count_spool(array(
        'entity_id' => $node->nid,
        'entity_type' => 'node',
      ));
    }
  }

  // Determine which nodes are send per translation group and per individual node.
  foreach ($counts as $newsletter_id => $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[$newsletter_id] = array_sum($node_count);
    foreach ($node_count as $nid => $count) {

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

  // Update overall newsletter status
  if (!empty($send)) {
    foreach ($send as $nid) {
      $node = node_load($nid);
      simplenews_issue_status($node, SIMPLENEWS_STATUS_SEND_READY);
      node_save($node);
    }
  }
}