Refactor fetching list of subscribers for export
[MAILPOET-3146]
This commit is contained in:
committed by
Veljko V
parent
c7152e9448
commit
cd578e6475
@ -74,23 +74,27 @@ class SegmentSubscribersRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is fetches list of all segments basic data and count of subscribed subscribers.
|
||||
* This method is fetches list of all segments basic data and count of subscribers.
|
||||
* @return array<array{id: string, name: string, type: string, subscribers: int}>
|
||||
*/
|
||||
public function getSimpleSegmentListWithSubscribersCounts(string $type = null): array {
|
||||
public function getSimpleSegmentListWithSubscribersCounts(
|
||||
string $segmentType = null,
|
||||
string $subscriberStatus = SubscriberEntity::STATUS_SUBSCRIBED
|
||||
): array {
|
||||
$subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName();
|
||||
$subscribersSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName();
|
||||
$segmentsTable = $this->entityManager->getClassMetadata(SegmentEntity::class)->getTableName();
|
||||
|
||||
$countCondition = "subscribers.deleted_at IS NULL";
|
||||
if ($subscriberStatus) {
|
||||
$countCondition .= " AND subscribers.status= :subscriberStatus AND subsegments.status = :subscriberStatus";
|
||||
}
|
||||
|
||||
$segmentsDataQuery = $this->entityManager
|
||||
->getConnection()
|
||||
->createQueryBuilder()
|
||||
->select(
|
||||
"segments.id, segments.name, segments.type, COUNT(IF(
|
||||
subsegments.status = :statusSubscribed
|
||||
AND subscribers.deleted_at IS NULL
|
||||
AND subscribers.status= :statusSubscribed
|
||||
, 1, NULL)) as subscribers"
|
||||
"segments.id, segments.name, segments.type, COUNT(IF($countCondition, 1, NULL)) as subscribers"
|
||||
)->from($segmentsTable, 'segments')
|
||||
->leftJoin('segments', $subscribersSegmentsTable, 'subsegments', "subsegments.segment_id = segments.id")
|
||||
->leftJoin('subsegments', $subscribersTable, 'subscribers', "subscribers.id = subsegments.subscriber_id")
|
||||
@ -98,13 +102,16 @@ class SegmentSubscribersRepository {
|
||||
->groupBy('segments.id')
|
||||
->addGroupBy('segments.name')
|
||||
->addGroupBy('segments.type')
|
||||
->orderBy('segments.name')
|
||||
->setParameter('statusSubscribed', SubscriberEntity::STATUS_SUBSCRIBED);
|
||||
->orderBy('segments.name');
|
||||
|
||||
if ($type) {
|
||||
if ($subscriberStatus) {
|
||||
$segmentsDataQuery->setParameter('subscriberStatus', $subscriberStatus);
|
||||
}
|
||||
|
||||
if ($segmentType) {
|
||||
$segmentsDataQuery
|
||||
->andWhere('segments.type = :typeParam')
|
||||
->setParameter('typeParam', $type);
|
||||
->setParameter('typeParam', $segmentType);
|
||||
}
|
||||
|
||||
$statement = $this->executeQuery($segmentsDataQuery);
|
||||
|
@ -3,10 +3,8 @@
|
||||
namespace MailPoet\Subscribers\ImportExport;
|
||||
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\DynamicSegments\FreePluginConnectors\AddToNewslettersSegments;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Segments\SegmentSubscribersRepository;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
@ -17,35 +15,37 @@ class ImportExportFactory {
|
||||
/** @var string|null */
|
||||
public $action;
|
||||
|
||||
/** @var AddToNewslettersSegments */
|
||||
private $addToNewslettersSegments;
|
||||
|
||||
/** @var SegmentSubscribersRepository */
|
||||
private $segmentSubscribersRepository;
|
||||
|
||||
public function __construct($action = null) {
|
||||
$this->action = $action;
|
||||
$this->addToNewslettersSegments = ContainerWrapper::getInstance()->get(AddToNewslettersSegments::class);
|
||||
$this->segmentSubscribersRepository = ContainerWrapper::getInstance()->get(SegmentSubscribersRepository::class);
|
||||
}
|
||||
|
||||
public function getSegments() {
|
||||
$segments = $this->segmentSubscribersRepository->getSimpleSegmentListWithSubscribersCounts();
|
||||
|
||||
if ($this->action === self::IMPORT_ACTION) {
|
||||
$segments = $this->segmentSubscribersRepository->getSimpleSegmentListWithSubscribersCounts();
|
||||
$segments = array_values(array_filter($segments, function($segment) {
|
||||
return in_array($segment['type'], [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS]);
|
||||
}));
|
||||
} else {
|
||||
$segments = Segment::getSegmentsForExport();
|
||||
$segments = $this->addToNewslettersSegments->add($segments);
|
||||
$segments = $this->segmentSubscribersRepository->getSimpleSegmentListWithSubscribersCounts(null, '');
|
||||
$segments = array_values(array_filter($segments, function($segment) {
|
||||
return $segment['subscribers'] > 0;
|
||||
}));
|
||||
$withoutSegmentCount = $this->segmentSubscribersRepository->getSubscribersWithoutSegmentCount();
|
||||
if ($withoutSegmentCount) {
|
||||
$segments[] = [
|
||||
'id' => 0,
|
||||
'name' => __('Not in a List', 'mailpoet'),
|
||||
'subscribers' => $withoutSegmentCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return array_map(function($segment) {
|
||||
if (!$segment['name']) $segment['name'] = __('Not In List', 'mailpoet');
|
||||
if (!$segment['id']) $segment['id'] = 0;
|
||||
return [
|
||||
'id' => $segment['id'],
|
||||
'name' => $segment['name'],
|
||||
|
@ -9,7 +9,9 @@ use MailPoet\Models\SubscriberSegment;
|
||||
use MailPoet\Subscribers\ImportExport\ImportExportFactory;
|
||||
|
||||
class ImportExportFactoryTest extends \MailPoetTest {
|
||||
/** @var ImportExportFactory */
|
||||
public $exportFactory;
|
||||
/** @var ImportExportFactory */
|
||||
public $importFactory;
|
||||
|
||||
public function _before() {
|
||||
|
Reference in New Issue
Block a user