1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2014 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\AssetBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Mautic\AssetBundle\Helper\TokenHelper; |
15
|
|
|
use Mautic\CoreBundle\Event\BuilderEvent; |
16
|
|
|
use Mautic\CoreBundle\Helper\BuilderTokenHelperFactory; |
17
|
|
|
use Mautic\CoreBundle\Security\Permissions\CorePermissions; |
18
|
|
|
use Mautic\EmailBundle\EmailEvents; |
19
|
|
|
use Mautic\EmailBundle\Event\EmailSendEvent; |
20
|
|
|
use Mautic\LeadBundle\Tracker\ContactTracker; |
21
|
|
|
use Mautic\PageBundle\Event\PageDisplayEvent; |
22
|
|
|
use Mautic\PageBundle\PageEvents; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
24
|
|
|
|
25
|
|
|
class BuilderSubscriber implements EventSubscriberInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $assetToken = '{assetlink=(.*?)}'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CorePermissions |
34
|
|
|
*/ |
35
|
|
|
private $security; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TokenHelper |
39
|
|
|
*/ |
40
|
|
|
private $tokenHelper; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ContactTracker |
44
|
|
|
*/ |
45
|
|
|
private $contactTracker; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var BuilderTokenHelperFactory |
49
|
|
|
*/ |
50
|
|
|
private $builderTokenHelperFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* BuilderSubscriber constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
CorePermissions $security, |
57
|
|
|
TokenHelper $tokenHelper, |
58
|
|
|
ContactTracker $contactTracker, |
59
|
|
|
BuilderTokenHelperFactory $builderTokenHelperFactory |
60
|
|
|
) { |
61
|
|
|
$this->security = $security; |
62
|
|
|
$this->tokenHelper = $tokenHelper; |
63
|
|
|
$this->contactTracker = $contactTracker; |
64
|
|
|
$this->builderTokenHelperFactory = $builderTokenHelperFactory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public static function getSubscribedEvents() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
EmailEvents::EMAIL_ON_BUILD => ['onBuilderBuild', 0], |
74
|
|
|
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0], |
75
|
|
|
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0], |
76
|
|
|
PageEvents::PAGE_ON_BUILD => ['onBuilderBuild', 0], |
77
|
|
|
PageEvents::PAGE_ON_DISPLAY => ['onPageDisplay', 0], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function onBuilderBuild(BuilderEvent $event) |
82
|
|
|
{ |
83
|
|
|
if ($event->tokensRequested($this->assetToken)) { |
84
|
|
|
$tokenHelper = $this->builderTokenHelperFactory->getBuilderTokenHelper('asset'); |
85
|
|
|
$event->addTokensFromHelper($tokenHelper, $this->assetToken, 'title', 'id', true); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function onEmailGenerate(EmailSendEvent $event) |
90
|
|
|
{ |
91
|
|
|
$lead = $event->getLead(); |
92
|
|
|
$leadId = (int) (null !== $lead ? $lead['id'] : null); |
|
|
|
|
93
|
|
|
$email = $event->getEmail(); |
94
|
|
|
$tokens = $this->generateTokensFromContent($event, $leadId, $event->getSource(), null === $email ? null : $email->getId()); |
|
|
|
|
95
|
|
|
$event->addTokens($tokens); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function onPageDisplay(PageDisplayEvent $event) |
99
|
|
|
{ |
100
|
|
|
$page = $event->getPage(); |
101
|
|
|
$lead = $this->security->isAnonymous() ? $this->contactTracker->getContact() : null; |
102
|
|
|
$leadId = $lead ? $lead->getId() : null; |
103
|
|
|
$tokens = $this->generateTokensFromContent($event, $leadId, ['page', $page->getId()]); |
104
|
|
|
$content = $event->getContent(); |
105
|
|
|
|
106
|
|
|
if (!empty($tokens)) { |
107
|
|
|
$content = str_ireplace(array_keys($tokens), $tokens, $content); |
108
|
|
|
} |
109
|
|
|
$event->setContent($content); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param PageDisplayEvent|EmailSendEvent $event |
114
|
|
|
* @param int $leadId |
115
|
|
|
* @param array $source |
116
|
|
|
* @param null $emailId |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private function generateTokensFromContent($event, $leadId, $source = [], $emailId = null) |
121
|
|
|
{ |
122
|
|
|
if ($event instanceof PageDisplayEvent || ($event instanceof EmailSendEvent && $event->shouldAppendClickthrough())) { |
123
|
|
|
$clickthrough = [ |
124
|
|
|
'source' => $source, |
125
|
|
|
'lead' => $leadId ?? false, |
126
|
|
|
'email' => $emailId ?? false, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->tokenHelper->findAssetTokens($event->getContent(), array_filter($clickthrough ?? [])); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|