You are here

public function Mailer::updateSendStatus in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::updateSendStatus()
  2. 3.x src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::updateSendStatus()

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.

Overrides MailerInterface::updateSendStatus

1 call to Mailer::updateSendStatus()
Mailer::attemptImmediateSend in src/Mail/Mailer.php
Send mail spool immediatly if cron should not be used.

File

src/Mail/Mailer.php, line 431

Class

Mailer
Default Mailer.

Namespace

Drupal\simplenews\Mail

Code

public function updateSendStatus() {
  $counts = array();

  // number of pending 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 = \Drupal::entityQuery('node');
  $nids = $query
    ->condition('simplenews_issue.status', SIMPLENEWS_STATUS_SEND_PENDING)
    ->execute();
  $nodes = Node::loadMultiple($nids);
  if ($nodes) {
    foreach ($nodes as $nid => $node) {
      $counts[$node->simplenews_issue->target_id][$nid] = $this->spoolStorage
        ->countMails(array(
        'entity_id' => $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);
      $node->simplenews_issue->status = SIMPLENEWS_STATUS_SEND_READY;
      $node
        ->save();
    }
  }
}