1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2015 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\EmailBundle\EventListener; |
||
13 | |||
14 | use Mautic\CoreBundle\Event\TokenReplacementEvent; |
||
15 | use Mautic\EmailBundle\EmailEvents; |
||
16 | use Mautic\EmailBundle\Entity\Email; |
||
17 | use Mautic\EmailBundle\Event\EmailSendEvent; |
||
18 | use Mautic\LeadBundle\Entity\Lead; |
||
19 | use Mautic\LeadBundle\Helper\PrimaryCompanyHelper; |
||
20 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
21 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
22 | |||
23 | class TokenSubscriber implements EventSubscriberInterface |
||
24 | { |
||
25 | use MatchFilterForLeadTrait; |
||
26 | |||
27 | /** |
||
28 | * @var EventDispatcherInterface |
||
29 | */ |
||
30 | private $dispatcher; |
||
31 | |||
32 | /** |
||
33 | * @var PrimaryCompanyHelper |
||
34 | */ |
||
35 | private $primaryCompanyHelper; |
||
36 | |||
37 | public function __construct(EventDispatcherInterface $dispatcher, PrimaryCompanyHelper $primaryCompanyHelper) |
||
38 | { |
||
39 | $this->dispatcher = $dispatcher; |
||
40 | $this->primaryCompanyHelper = $primaryCompanyHelper; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public static function getSubscribedEvents() |
||
47 | { |
||
48 | return [ |
||
49 | EmailEvents::EMAIL_ON_SEND => ['decodeTokens', 254], |
||
50 | EmailEvents::EMAIL_ON_DISPLAY => ['decodeTokens', 254], |
||
51 | EmailEvents::TOKEN_REPLACEMENT => ['onTokenReplacement', 254], |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | public function decodeTokens(EmailSendEvent $event) |
||
56 | { |
||
57 | if ($event->isDynamicContentParsing()) { |
||
58 | // prevent a loop |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | // Find and replace encoded tokens for trackable URL conversion |
||
63 | $content = $event->getContent(); |
||
64 | $content = preg_replace('/(%7B)(.*?)(%7D)/i', '{$2}', $content, -1, $count); |
||
65 | $event->setContent($content); |
||
66 | |||
67 | if ($plainText = $event->getPlainText()) { |
||
68 | $plainText = preg_replace('/(%7B)(.*?)(%7D)/i', '{$2}', $plainText); |
||
69 | $event->setPlainText($plainText); |
||
70 | } |
||
71 | |||
72 | $email = $event->getEmail(); |
||
73 | if ($dynamicContentAsArray = $email instanceof Email ? $email->getDynamicContent() : null) { |
||
74 | $lead = $event->getLead(); |
||
75 | $tokens = $event->getTokens(); |
||
76 | $tokenEvent = new TokenReplacementEvent( |
||
77 | null, |
||
78 | $lead, |
||
79 | [ |
||
80 | 'tokens' => $tokens, |
||
81 | 'lead' => null, |
||
82 | 'dynamicContent' => $dynamicContentAsArray, |
||
83 | 'idHash' => $event->getIdHash(), |
||
84 | ], |
||
85 | |||
86 | ); |
||
87 | $this->dispatcher->dispatch(EmailEvents::TOKEN_REPLACEMENT, $tokenEvent); |
||
88 | $event->addTokens($tokenEvent->getTokens()); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | public function onTokenReplacement(TokenReplacementEvent $event) |
||
93 | { |
||
94 | $clickthrough = $event->getClickthrough(); |
||
95 | |||
96 | if (!array_key_exists('dynamicContent', $clickthrough)) { |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | $lead = $event->getLead(); |
||
101 | $tokens = $clickthrough['tokens']; |
||
102 | $tokenData = $clickthrough['dynamicContent']; |
||
103 | |||
104 | if ($lead instanceof Lead) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
105 | $lead = $this->primaryCompanyHelper->getProfileFieldsWithPrimaryCompany($lead); |
||
106 | } else { |
||
107 | $lead = $this->primaryCompanyHelper->mergePrimaryCompanyWithProfileFields($lead['id'], $lead); |
||
108 | } |
||
109 | |||
110 | foreach ($tokenData as $data) { |
||
111 | // Default content |
||
112 | $filterContent = $data['content']; |
||
113 | |||
114 | foreach ($data['filters'] as $filter) { |
||
115 | if ($this->matchFilterForLead($filter['filters'], $lead)) { |
||
116 | $filterContent = $filter['content']; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Replace lead tokens in dynamic content (but no recurrence on dynamic content to avoid infinite loop) |
||
121 | $emailSendEvent = new EmailSendEvent( |
||
122 | null, |
||
123 | [ |
||
124 | 'content' => $filterContent, |
||
125 | 'email' => $event->getPassthrough(), |
||
126 | 'idHash' => !empty($clickthrough['idHash']) ? $clickthrough['idHash'] : null, |
||
127 | 'tokens' => $tokens, |
||
128 | 'lead' => $lead, |
||
129 | ], |
||
130 | true |
||
131 | ); |
||
132 | |||
133 | $this->dispatcher->dispatch(EmailEvents::EMAIL_ON_DISPLAY, $emailSendEvent); |
||
134 | $untokenizedContent = $emailSendEvent->getContent(true); |
||
135 | |||
136 | $event->addToken('{dynamiccontent="'.$data['tokenName'].'"}', $untokenizedContent); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 |