View source
<?php
namespace Drupal\simplenews\Spool;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\node\NodeInterface;
class SpoolStorage implements SpoolStorageInterface {
protected $connection;
protected $lock;
protected $config;
protected $moduleHandler;
public function __construct(Connection $connection, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
$this->connection = $connection;
$this->lock = $lock;
$this->config = $config_factory
->get('simplenews.settings');
$this->moduleHandler = $module_handler;
}
public function getMails($limit = self::UNLIMITED, $conditions = array()) {
$messages = array();
if (!empty($conditions['nid'])) {
$conditions['entity_type'] = 'node';
$conditions['entity_id'] = $conditions['nid'];
unset($conditions['nid']);
}
if (!isset($conditions['status'])) {
$conditions['status'] = array(
SpoolStorageInterface::STATUS_PENDING,
SpoolStorageInterface::STATUS_IN_PROGRESS,
);
}
$status_or = new Condition('OR');
$statuses = is_array($conditions['status']) ? $conditions['status'] : array(
$conditions['status'],
);
foreach ($statuses as $status) {
if ($status == SpoolStorageInterface::STATUS_IN_PROGRESS) {
$status_or
->condition((new Condition('AND'))
->condition('status', $status)
->condition('s.timestamp', $this
->getExpirationTime(), '<'));
}
else {
$status_or
->condition('status', $status);
}
}
unset($conditions['status']);
$query = $this->connection
->select('simplenews_mail_spool', 's')
->fields('s')
->condition($status_or)
->orderBy('s.timestamp', 'ASC');
foreach ($conditions as $field => $value) {
$query
->condition($field, $value);
}
if ($this->lock
->acquire('simplenews_acquire_mail')) {
if ($limit > 0) {
$query
->range(0, $limit);
}
foreach ($query
->execute() as $message) {
if (mb_strlen($message->data)) {
$message->data = unserialize($message->data);
}
else {
$message->data = simplenews_subscriber_load_by_mail($message->mail);
}
$messages[$message->msid] = $message;
}
if (count($messages) > 0) {
$this
->updateMails(array_keys($messages), array(
'status' => SpoolStorageInterface::STATUS_IN_PROGRESS,
));
}
$this->lock
->release('simplenews_acquire_mail');
}
return new SpoolList($messages);
}
public function updateMails($msids, array $data) {
$this->connection
->update('simplenews_mail_spool')
->condition('msid', (array) $msids, 'IN')
->fields(array(
'status' => $data['status'],
'error' => isset($data['error']) ? (int) $data['error'] : 0,
'timestamp' => REQUEST_TIME,
))
->execute();
}
public function countMails(array $conditions = array()) {
if (!empty($conditions['nid'])) {
$conditions['entity_type'] = 'node';
$conditions['entity_id'] = $conditions['nid'];
unset($conditions['nid']);
}
if (!isset($conditions['status'])) {
$conditions['status'] = array(
SpoolStorageInterface::STATUS_PENDING,
SpoolStorageInterface::STATUS_IN_PROGRESS,
);
}
$query = $this->connection
->select('simplenews_mail_spool');
foreach ($conditions as $field => $value) {
if ($field == 'status') {
if (!is_array($value)) {
$value = array(
$value,
);
}
$status_or = new Condition('OR');
foreach ($value as $status) {
if ($status == SpoolStorageInterface::STATUS_IN_PROGRESS) {
$status_or
->condition((new Condition('AND'))
->condition('status', $status)
->condition('timestamp', $this
->getExpirationTime(), '<'));
}
else {
$status_or
->condition('status', $status);
}
}
$query
->condition($status_or);
}
else {
$query
->condition($field, $value);
}
}
$query
->addExpression('COUNT(*)', 'count');
return (int) $query
->execute()
->fetchField();
}
public function clear() {
$expiration_time = REQUEST_TIME - $this->config
->get('mail.spool_expire') * 86400;
return $this->connection
->delete('simplenews_mail_spool')
->condition('status', [
SpoolStorageInterface::STATUS_DONE,
SpoolStorageInterface::STATUS_SKIPPED,
], 'IN')
->condition('timestamp', $expiration_time, '<=')
->execute();
}
public function deleteMails(array $conditions) {
if (!empty($conditions['nid'])) {
$conditions['entity_type'] = 'node';
$conditions['entity_id'] = $conditions['nid'];
unset($conditions['nid']);
}
$query = $this->connection
->delete('simplenews_mail_spool');
foreach ($conditions as $condition => $value) {
$query
->condition($condition, $value);
}
return $query
->execute();
}
public function addFromEntity(NodeInterface $node) {
$newsletter = $node->simplenews_issue->entity;
$handler = $node->simplenews_issue->handler;
$handler_settings = $node->simplenews_issue->handler_settings;
$recipient_handler = simplenews_get_recipient_handler($newsletter, $handler, $handler_settings);
$select = $recipient_handler
->buildRecipientQuery();
$select
->addExpression('\'node\'', 'entity_type');
$select
->addExpression($node
->id(), 'entity_id');
$select
->addExpression(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, 'status');
$select
->addExpression(REQUEST_TIME, 'timestamp');
$node->simplenews_issue->subscribers = simplenews_count_subscriptions($node->simplenews_issue->target_id);
$this->connection
->insert('simplenews_mail_spool')
->from($select)
->execute();
$node->simplenews_issue->status = SIMPLENEWS_STATUS_SEND_PENDING;
$this->moduleHandler
->invokeAll('simplenews_spooled', array(
$node,
));
}
public function addMail(array $spool) {
$status = isset($spool['status']) ? $spool['status'] : SpoolStorageInterface::STATUS_PENDING;
$time = isset($spool['time']) ? $spool['time'] : REQUEST_TIME;
$this->connection
->insert('simplenews_mail_spool')
->fields(array(
'mail' => $spool['mail'],
'entity_type' => $spool['entity_type'],
'entity_id' => $spool['entity_id'],
'newsletter_id' => $spool['newsletter_id'],
'snid' => $spool['snid'],
'status' => $status,
'timestamp' => $time,
'data' => serialize($spool['data']),
))
->execute();
}
public function issueSummary(NodeInterface $node) {
$status = $node->simplenews_issue->status;
$summary['sent_count'] = (int) $node->simplenews_issue->sent_count;
$summary['count'] = (int) $node->simplenews_issue->subscribers;
if ($status == SIMPLENEWS_STATUS_SEND_READY) {
$summary['description'] = t('Newsletter issue sent to @count subscribers.', [
'@count' => $summary['count'],
]);
}
elseif ($status == SIMPLENEWS_STATUS_SEND_PENDING) {
$summary['description'] = t('Newsletter issue is pending, @sent mails sent out of @count.', [
'@sent' => $summary['sent_count'],
'@count' => $summary['count'],
]);
}
else {
$summary['count'] = simplenews_count_subscriptions($node->simplenews_issue->target_id);
if ($status == SIMPLENEWS_STATUS_SEND_NOT) {
$summary['description'] = t('Newsletter issue will be sent to @count subscribers.', [
'@count' => $summary['count'],
]);
}
else {
$summary['description'] = t('Newsletter issue will be sent to @count subscribers on publish.', [
'@count' => $summary['count'],
]);
}
}
return $summary;
}
protected function getExpirationTime() {
$timeout = $this->config
->get('mail.spool_progress_expiration');
$expiration_time = REQUEST_TIME - $timeout;
return $expiration_time;
}
}