public function SpoolStorage::countMails in Simplenews 8
Same name and namespace in other branches
- 8.2 src/Spool/SpoolStorage.php \Drupal\simplenews\Spool\SpoolStorage::countMails()
- 3.x src/Spool/SpoolStorage.php \Drupal\simplenews\Spool\SpoolStorage::countMails()
Count data in mail spool table.
Parameters
array $conditions: (Optional) Array of conditions which are applied to the query. Defaults
Return value
int Count of mail spool elements of the passed in arguments.
Overrides SpoolStorageInterface::countMails
File
- src/
Spool/ SpoolStorage.php, line 152
Class
- SpoolStorage
- Default database spool storage.
Namespace
Drupal\simplenews\SpoolCode
public function countMails(array $conditions = array()) {
// Continue to support 'nid' as a condition.
if (!empty($conditions['nid'])) {
$conditions['entity_type'] = 'node';
$conditions['entity_id'] = $conditions['nid'];
unset($conditions['nid']);
}
// Add default status condition if not set.
if (!isset($conditions['status'])) {
$conditions['status'] = array(
SpoolStorageInterface::STATUS_PENDING,
SpoolStorageInterface::STATUS_IN_PROGRESS,
);
}
$query = $this->connection
->select('simplenews_mail_spool');
// Add conditions.
foreach ($conditions as $field => $value) {
if ($field == 'status') {
if (!is_array($value)) {
$value = array(
$value,
);
}
$status_or = new Condition('OR');
foreach ($value as $status) {
// Do not count pending entries unless they are expired.
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();
}