forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReportSubscriber.php
103 lines (89 loc) · 3.3 KB
/
ReportSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Mautic\PointBundle\EventListener;
use Mautic\LeadBundle\Report\FieldsBuilder;
use Mautic\PointBundle\Entity\Group;
use Mautic\PointBundle\Entity\GroupContactScore;
use Mautic\ReportBundle\Event\ReportBuilderEvent;
use Mautic\ReportBundle\Event\ReportGeneratorEvent;
use Mautic\ReportBundle\ReportEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReportSubscriber implements EventSubscriberInterface
{
public const CONTEXT_GROUP_SCORE = 'group.score';
public const GROUP_PREFIX = 'pl';
public const GROUP_SCORE_PREFIX = 'ls';
public const GROUP_COLUMNS = [
self::GROUP_PREFIX.'.id' => [
'alias' => 'group_id',
'label' => 'mautic.point.report.group_id',
'type' => 'int',
],
self::GROUP_PREFIX.'.name' => [
'alias' => 'group_name',
'label' => 'mautic.point.report.group_name',
'type' => 'string',
],
self::GROUP_SCORE_PREFIX.'.score' => [
'alias' => 'group_score',
'label' => 'mautic.point.report.group_score',
'type' => 'int',
],
];
/**
* @var string[]
*/
private array $reportContexts = [
self::CONTEXT_GROUP_SCORE,
];
public function __construct(private FieldsBuilder $fieldsBuilder)
{
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
ReportEvents::REPORT_ON_BUILD => ['onReportBuilder', -10],
ReportEvents::REPORT_ON_GENERATE => ['onReportGenerate', -10],
];
}
public function onReportBuilder(ReportBuilderEvent $event): void
{
if (!$event->checkContext($this->reportContexts)) {
return;
}
if ($event->checkContext(self::CONTEXT_GROUP_SCORE)) {
$columns = array_merge(
self::GROUP_COLUMNS,
$event->getLeadColumns()
);
$filters = array_merge(
$columns,
$this->fieldsBuilder->getLeadFilter('l.', 's.')
);
$data = [
'display_name' => 'mautic.point.group.report.table',
'columns' => $columns,
'filters' => $filters,
];
$event->addTable(self::CONTEXT_GROUP_SCORE, $data, 'contacts');
}
}
public function onReportGenerate(ReportGeneratorEvent $event): void
{
if (!$event->checkContext($this->reportContexts)) {
return;
}
$qb = $event->getQueryBuilder();
if ($event->checkContext(self::CONTEXT_GROUP_SCORE)) {
$qb->from(MAUTIC_TABLE_PREFIX.GroupContactScore::TABLE_NAME, self::GROUP_SCORE_PREFIX)
->leftJoin(self::GROUP_SCORE_PREFIX, MAUTIC_TABLE_PREFIX.Group::TABLE_NAME, self::GROUP_PREFIX, self::GROUP_SCORE_PREFIX.'.group_id = '.self::GROUP_PREFIX.'.id')
->leftJoin(self::GROUP_SCORE_PREFIX, MAUTIC_TABLE_PREFIX.'leads', 'l', self::GROUP_SCORE_PREFIX.'.contact_id = l.id');
if ($event->hasFilter('s.leadlist_id')) {
$qb->join('l', MAUTIC_TABLE_PREFIX.'lead_lists_leads', 's', 's.lead_id = l.id AND s.manually_removed = 0');
}
}
$event->setQueryBuilder($qb);
}
}