Add groups

[MAILPOET-2657]
This commit is contained in:
Pavel Dohnal
2020-08-03 15:15:17 +02:00
committed by Veljko V
parent 22c8c9a8df
commit 9f615548b2
2 changed files with 63 additions and 1 deletions

View File

@ -222,6 +222,7 @@ class Subscriber extends Model {
}
public static function groups($data) {
return [
[
'name' => 'all',

View File

@ -72,7 +72,68 @@ class SubscriberListingRepository extends ListingRepository {
}
public function getGroups(ListingDefinition $definition): array {
return [];// TODO
$queryBuilder = clone $this->queryBuilder;
$this->applyFromClause($queryBuilder);
// total count
$countQueryBuilder = clone $queryBuilder;
$countQueryBuilder->select('COUNT(s) AS subscribersCount');
$countQueryBuilder->andWhere('s.deletedAt IS NULL');
$totalCount = (int)$countQueryBuilder->getQuery()->getSingleScalarResult();
// trashed count
$trashedCountQueryBuilder = clone $queryBuilder;
$trashedCountQueryBuilder->select('COUNT(s) AS subscribersCount');
$trashedCountQueryBuilder->andWhere('s.deletedAt IS NOT NULL');
$trashedCount = (int)$trashedCountQueryBuilder->getQuery()->getSingleScalarResult();
// count-by-status query
$queryBuilder->select('s.status, COUNT(s) AS subscribersCount');
$queryBuilder->andWhere('s.deletedAt IS NULL');
$queryBuilder->groupBy('s.status');
$map = [];
foreach ($queryBuilder->getQuery()->getResult() as $item) {
$map[$item['status']] = (int)$item['subscribersCount'];
}
return [
[
'name' => 'all',
'label' => WPFunctions::get()->__('All', 'mailpoet'),
'count' => $totalCount,
],
[
'name' => SubscriberEntity::STATUS_SUBSCRIBED,
'label' => WPFunctions::get()->__('Subscribed', 'mailpoet'),
'count' => $map[SubscriberEntity::STATUS_SUBSCRIBED] ?? 0,
],
[
'name' => SubscriberEntity::STATUS_UNCONFIRMED,
'label' => WPFunctions::get()->__('Unconfirmed', 'mailpoet'),
'count' => $map[SubscriberEntity::STATUS_UNCONFIRMED] ?? 0,
],
[
'name' => SubscriberEntity::STATUS_UNSUBSCRIBED,
'label' => WPFunctions::get()->__('Unsubscribed', 'mailpoet'),
'count' => $map[SubscriberEntity::STATUS_UNSUBSCRIBED] ?? 0,
],
[
'name' => SubscriberEntity::STATUS_INACTIVE,
'label' => WPFunctions::get()->__('Inactive', 'mailpoet'),
'count' => $map[SubscriberEntity::STATUS_INACTIVE] ?? 0,
],
[
'name' => SubscriberEntity::STATUS_BOUNCED,
'label' => WPFunctions::get()->__('Bounced', 'mailpoet'),
'count' => $map[SubscriberEntity::STATUS_BOUNCED] ?? 0,
],
[
'name' => 'trash',
'label' => WPFunctions::get()->__('Trash', 'mailpoet'),
'count' => $trashedCount,
],
];
}
public function getFilters(ListingDefinition $definition): array {