Load list of segments on form editor page using doctrine

[MAILPOET-3164]
This commit is contained in:
Rostislav Wolny
2021-01-15 16:37:35 +01:00
committed by Veljko V
parent 09fa34f6e6
commit ade48fc359
2 changed files with 17 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder;
use MailPoet\Config\Localizer; use MailPoet\Config\Localizer;
use MailPoet\CustomFields\CustomFieldsRepository; use MailPoet\CustomFields\CustomFieldsRepository;
use MailPoet\Entities\FormEntity; use MailPoet\Entities\FormEntity;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Form\Block; use MailPoet\Form\Block;
use MailPoet\Form\FormFactory; use MailPoet\Form\FormFactory;
use MailPoet\Form\Renderer as FormRenderer; use MailPoet\Form\Renderer as FormRenderer;
@ -74,9 +75,9 @@ use MailPoet\Form\Templates\Templates\Template7Widget;
use MailPoet\Form\Util\CustomFonts; use MailPoet\Form\Util\CustomFonts;
use MailPoet\Form\Util\Export; use MailPoet\Form\Util\Export;
use MailPoet\Models\Form; use MailPoet\Models\Form;
use MailPoet\Models\Segment;
use MailPoet\Router\Endpoints\FormPreview; use MailPoet\Router\Endpoints\FormPreview;
use MailPoet\Router\Router; use MailPoet\Router\Router;
use MailPoet\Segments\SegmentSubscribersRepository;
use MailPoet\Settings\Pages; use MailPoet\Settings\Pages;
use MailPoet\Settings\UserFlagsController; use MailPoet\Settings\UserFlagsController;
use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader; use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader;
@ -116,6 +117,9 @@ class FormEditor {
/** @var WPPostListLoader */ /** @var WPPostListLoader */
private $wpPostListLoader; private $wpPostListLoader;
/** @var SegmentSubscribersRepository */
private $segmentSubscribersRepository;
private $activeTemplates = [ private $activeTemplates = [
FormEntity::DISPLAY_TYPE_POPUP => [ FormEntity::DISPLAY_TYPE_POPUP => [
Template1Popup::ID, Template1Popup::ID,
@ -200,7 +204,8 @@ class FormEditor {
Localizer $localizer, Localizer $localizer,
UserFlagsController $userFlags, UserFlagsController $userFlags,
WPPostListLoader $wpPostListLoader, WPPostListLoader $wpPostListLoader,
TemplateRepository $templateRepository TemplateRepository $templateRepository,
SegmentSubscribersRepository $segmentSubscribersRepository
) { ) {
$this->pageRenderer = $pageRenderer; $this->pageRenderer = $pageRenderer;
$this->customFieldsRepository = $customFieldsRepository; $this->customFieldsRepository = $customFieldsRepository;
@ -213,6 +218,7 @@ class FormEditor {
$this->templatesRepository = $templateRepository; $this->templatesRepository = $templateRepository;
$this->userFlags = $userFlags; $this->userFlags = $userFlags;
$this->wpPostListLoader = $wpPostListLoader; $this->wpPostListLoader = $wpPostListLoader;
$this->segmentSubscribersRepository = $segmentSubscribersRepository;
} }
public function render() { public function render() {
@ -234,7 +240,7 @@ class FormEditor {
'shortcode' => Export::get('shortcode', $form), 'shortcode' => Export::get('shortcode', $form),
], ],
'mailpoet_pages' => Pages::getMailPoetPages(), 'mailpoet_pages' => Pages::getMailPoetPages(),
'segments' => Segment::getSegmentsWithSubscriberCount(), 'segments' => $this->segmentSubscribersRepository->getSimpleSegmentListWithSubscribersCounts(SegmentEntity::TYPE_DEFAULT),
'styles' => $this->formRenderer->getCustomStyles($form), 'styles' => $this->formRenderer->getCustomStyles($form),
'date_types' => array_map(function ($label, $value) { 'date_types' => array_map(function ($label, $value) {
return [ return [

View File

@ -59,7 +59,7 @@ 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 subscribed subscribers.
* @return array<array{id: string, name: string, type: string, subscribers: int}> * @return array<array{id: string, name: string, type: string, subscribers: int}>
*/ */
public function getSimpleSegmentListWithSubscribersCounts(): array { public function getSimpleSegmentListWithSubscribersCounts(string $type = null): array {
$subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName();
$subscribersSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); $subscribersSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName();
$segmentsTable = $this->entityManager->getClassMetadata(SegmentEntity::class)->getTableName(); $segmentsTable = $this->entityManager->getClassMetadata(SegmentEntity::class)->getTableName();
@ -82,6 +82,13 @@ class SegmentSubscribersRepository {
->addGroupBy('segments.type') ->addGroupBy('segments.type')
->orderBy('segments.name') ->orderBy('segments.name')
->setParameter('statusSubscribed', SubscriberEntity::STATUS_SUBSCRIBED); ->setParameter('statusSubscribed', SubscriberEntity::STATUS_SUBSCRIBED);
if ($type) {
$segmentsDataQuery
->andWhere('segments.type = :typeParam')
->setParameter('typeParam', $type);
}
$statement = $this->executeQuery($segmentsDataQuery); $statement = $this->executeQuery($segmentsDataQuery);
$segments = $statement->fetchAll(); $segments = $statement->fetchAll();