Files
mautic-marketing-program-bu…/Entity/AudienceRepository.php
T
Alexandre Gain 71ff11390b refactor(plugin): rename Plan to Program and add audience/UTM routes
Rename the Plan bundle to Program across config, routes, menus, and
controllers (Plan -> Initiative), bump version to 0.16.0, and introduce
new audience and UTM taxonomy routes and permissions to support the
expanded Programs feature set.
2026-11-13 22:01:32 +01:00

91 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\ProgramBundle\Entity;
use Doctrine\DBAL\Connection;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* @extends CommonRepository<Audience>
*/
class AudienceRepository extends CommonRepository
{
public function getTableAlias(): string
{
return 'pa';
}
/**
* The contact count of one segment: lead_lists_leads rows with manually_removed = 0.
*/
public function getSegmentContactCount(int $leadlistId): int
{
$count = $this->getEntityManager()->getConnection()->fetchOne(
'SELECT COUNT(ll.lead_id) FROM lead_lists_leads ll WHERE ll.leadlist_id = :leadlistId AND ll.manually_removed = 0',
['leadlistId' => $leadlistId]
);
return (int) $count;
}
/**
* Contact counts (lead_lists_leads rows with manually_removed = 0) for the segments of one
* audience, keyed by segment id. Used by the list view to show "Segment (123)".
*
* @return array<int, int> leadlist_id => contact count
*/
public function getSegmentContactCounts(int $audienceId): array
{
$rows = $this->getEntityManager()->getConnection()->fetchAllAssociative(
'SELECT s.leadlist_id, COUNT(ll.lead_id) AS cnt
FROM initiative_audiences_segments s
JOIN lead_lists_leads ll ON ll.leadlist_id = s.leadlist_id AND ll.manually_removed = 0
WHERE s.audience_id = :audienceId
GROUP BY s.leadlist_id',
['audienceId' => $audienceId]
);
$counts = [];
foreach ($rows as $row) {
$counts[(int) $row['leadlist_id']] = (int) $row['cnt'];
}
return $counts;
}
/**
* Contact counts for the segments of every audience in the list view, keyed by audience id then
* segment id. One query for the whole page.
*
* @param list<int> $audienceIds
*
* @return array<int, array<int, int>> audience_id => (leadlist_id => contact count)
*/
public function getSegmentContactCountsForAudiences(array $audienceIds): array
{
if ([] === $audienceIds) {
return [];
}
$connection = $this->getEntityManager()->getConnection();
$rows = $connection->fetchAllAssociative(
'SELECT s.audience_id, s.leadlist_id, COUNT(ll.lead_id) AS cnt
FROM initiative_audiences_segments s
JOIN lead_lists_leads ll ON ll.leadlist_id = s.leadlist_id AND ll.manually_removed = 0
WHERE s.audience_id IN (:audienceIds)
GROUP BY s.audience_id, s.leadlist_id',
['audienceIds' => $audienceIds],
['audienceIds' => Connection::PARAM_INT_ARRAY]
);
$counts = [];
foreach ($rows as $row) {
$counts[(int) $row['audience_id']][(int) $row['leadlist_id']] = (int) $row['cnt'];
}
return $counts;
}
}