-
Notifications
You must be signed in to change notification settings - Fork 57
/
EmailSubscriber.php
130 lines (110 loc) · 3.66 KB
/
EmailSubscriber.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace MauticPlugin\MauticAdvancedTemplatesBundle\EventListener;
use Mautic\CampaignBundle\Entity\Lead;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event as Events;
use Mautic\EmailBundle\Helper\PlainTextHelper;
use Mautic\CoreBundle\Exception as MauticException;
use Mautic\LeadBundle\Model\LeadModel;
use MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
/**
* Class EmailSubscriber.
*/
class EmailSubscriber implements EventSubscriberInterface
{
/**
* @var LeadModel
*/
private $leadModel;
/**
* @var TemplateProcessor $templateProcessor ;
*/
protected $templateProcessor;
/**
* @var LoggerInterface $logger ;
*/
protected $logger;
/**
* EmailSubscriber constructor.
*
* @param TokenHelper $tokenHelper
*/
public function __construct(TemplateProcessor $templateProcessor, LeadModel $leadModel, Logger $logger)
{
$this->templateProcessor = $templateProcessor;
$this->leadModel = $leadModel;
$this->logger = $logger;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 300],
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0],
];
}
private function getProperties(Events\EmailSendEvent $event) {
$tokens = [];
if (!$event->getEmail()) {
return [
'subject' => $event->getSubject(),
'content' => $event->getContent(),
'tokens' => $tokens,
];
}
$email = $event->getEmail();
$subject = $email->getSubject();
$content = $email->getCustomHtml();
$dynamic = $email->getDynamicContent();
foreach ($dynamic as $prop) {
$tokens[$prop['tokenName']] = $prop['content'];
}
//Add arbritrary tokens when using the email send api
$originalTokens = $event->getTokens();
foreach($originalTokens as $k => $v) {
$tokens[preg_replace('/^{(.*)}$/', '${1}', $k)] = $v;
}
return [
'subject' => $subject,
'content' => $content,
'tokens' => $tokens,
];
}
/**
* Search and replace tokens with content
*
* @param Events\EmailSendEvent $event
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
*/
public function onEmailGenerate(Events\EmailSendEvent $event)
{
$this->logger->info('onEmailGenerate MauticAdvancedTemplatesBundle\EmailSubscriber');
if ($event->isDynamicContentParsing()) {
return;
}
$props = $this->getProperties($event);
$lead = $event->getLead();
$leadmodel = $this->leadModel->getEntity($lead['id']);
$lead['tags'] = [];
if ($leadmodel && count($leadmodel->getTags()) > 0) {
foreach ($leadmodel->getTags() as $tag) {
$lead['tags'][] = $tag->getTag();
}
}
$subject = $this->templateProcessor->processTemplate($props['subject'], $lead);
$event->setSubject($subject);
$content = $this->templateProcessor->processTemplate($props['content'], $lead, $props['tokens']);
$content = $this->templateProcessor->addTrackingPixel($content);
$event->setContent($content);
if ( empty( trim($event->getPlainText()) ) ) {
$event->setPlainText( (new PlainTextHelper($content))->getText() );
}
}
}