1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2016 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\DynamicContentBundle\Helper; |
13
|
|
|
|
14
|
|
|
use Mautic\CampaignBundle\Executioner\RealTimeExecutioner; |
15
|
|
|
use Mautic\CoreBundle\Event\TokenReplacementEvent; |
16
|
|
|
use Mautic\DynamicContentBundle\DynamicContentEvents; |
17
|
|
|
use Mautic\DynamicContentBundle\Entity\DynamicContent; |
18
|
|
|
use Mautic\DynamicContentBundle\Model\DynamicContentModel; |
19
|
|
|
use Mautic\EmailBundle\EventListener\MatchFilterForLeadTrait; |
20
|
|
|
use Mautic\LeadBundle\Entity\Lead; |
21
|
|
|
use Mautic\LeadBundle\Entity\Tag; |
22
|
|
|
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
24
|
|
|
|
25
|
|
|
class DynamicContentHelper |
26
|
|
|
{ |
27
|
|
|
use MatchFilterForLeadTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RealTimeExecutioner |
31
|
|
|
*/ |
32
|
|
|
protected $realTimeExecutioner; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ContainerAwareEventDispatcher |
36
|
|
|
*/ |
37
|
|
|
protected $dispatcher; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var DynamicContentModel |
41
|
|
|
*/ |
42
|
|
|
protected $dynamicContentModel; |
43
|
|
|
|
44
|
|
|
public function __construct(DynamicContentModel $dynamicContentModel, RealTimeExecutioner $realTimeExecutioner, EventDispatcherInterface $dispatcher) |
45
|
|
|
{ |
46
|
|
|
$this->dynamicContentModel = $dynamicContentModel; |
47
|
|
|
$this->realTimeExecutioner = $realTimeExecutioner; |
48
|
|
|
$this->dispatcher = $dispatcher; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $slot |
53
|
|
|
* @param Lead|array $lead |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getDynamicContentForLead($slot, $lead) |
58
|
|
|
{ |
59
|
|
|
// Attempt campaign slots first |
60
|
|
|
$dwcActionResponse = $this->realTimeExecutioner->execute('dwc.decision', $slot, 'dynamicContent')->getActionResponses('dwc.push_content'); |
61
|
|
|
if (!empty($dwcActionResponse)) { |
62
|
|
|
return array_shift($dwcActionResponse); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Attempt stored content second |
66
|
|
|
$data = $this->dynamicContentModel->getSlotContentForLead($slot, $lead); |
67
|
|
|
if (!empty($data)) { |
68
|
|
|
$content = $data['content']; |
69
|
|
|
$dwc = $this->dynamicContentModel->getEntity($data['id']); |
70
|
|
|
if ($dwc instanceof DynamicContent) { |
|
|
|
|
71
|
|
|
$content = $this->getRealDynamicContent($slot, $lead, $dwc); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $content; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Finally attempt standalone DWC |
78
|
|
|
return $this->getDynamicContentSlotForLead($slot, $lead); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $slotName |
83
|
|
|
* @param Lead|array $lead |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getDynamicContentSlotForLead($slotName, $lead) |
88
|
|
|
{ |
89
|
|
|
$leadArray = []; |
90
|
|
|
if ($lead instanceof Lead) { |
91
|
|
|
$leadArray = $this->convertLeadToArray($lead); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$dwcs = $this->getDwcsBySlotName($slotName, true); |
95
|
|
|
/** @var DynamicContent $dwc */ |
96
|
|
|
foreach ($dwcs as $dwc) { |
97
|
|
|
if ($dwc->getIsCampaignBased()) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
if ($lead && $this->matchFilterForLead($dwc->getFilters(), $leadArray)) { |
101
|
|
|
return $lead ? $this->getRealDynamicContent($dwc->getSlotName(), $lead, $dwc) : ''; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $content |
110
|
|
|
* @param Lead|array $lead |
111
|
|
|
* |
112
|
|
|
* @return string Content with the {content} tokens replaced with dynamic content |
113
|
|
|
*/ |
114
|
|
|
public function replaceTokensInContent($content, $lead) |
115
|
|
|
{ |
116
|
|
|
// Find all dynamic content tags |
117
|
|
|
preg_match_all('/{(dynamiccontent)=(\w+)(?:\/}|}(?:([^{]*(?:{(?!\/\1})[^{]*)*){\/\1})?)/is', $content, $matches, PREG_SET_ORDER); |
118
|
|
|
|
119
|
|
|
foreach ($matches as $match) { |
120
|
|
|
$slot = $match[2]; |
121
|
|
|
$defaultContent = $match[3]; |
122
|
|
|
|
123
|
|
|
$dwcContent = $this->getDynamicContentForLead($slot, $lead); |
124
|
|
|
|
125
|
|
|
if (!$dwcContent) { |
126
|
|
|
$dwcContent = $defaultContent; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$content = str_replace($matches[0], $dwcContent, $content); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $content; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $content |
137
|
|
|
* @param Lead|null $lead |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function findDwcTokens($content, $lead) |
142
|
|
|
{ |
143
|
|
|
preg_match_all('/{dwc=(.*?)}/', $content, $matches); |
144
|
|
|
|
145
|
|
|
$tokens = []; |
146
|
|
|
if (!empty($matches[1])) { |
147
|
|
|
foreach ($matches[1] as $key => $slotName) { |
148
|
|
|
$token = $matches[0][$key]; |
149
|
|
|
if (!empty($tokens[$token])) { |
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$dwcs = $this->getDwcsBySlotName($slotName); |
154
|
|
|
|
155
|
|
|
/** @var DynamicContent $dwc */ |
156
|
|
|
foreach ($dwcs as $dwc) { |
157
|
|
|
if ($dwc->getIsCampaignBased()) { |
158
|
|
|
continue; |
159
|
|
|
} |
160
|
|
|
$content = $lead ? $this->getRealDynamicContent($dwc->getSlotName(), $lead, $dwc) : ''; |
161
|
|
|
$tokens[$token]['content'] = $content; |
162
|
|
|
$tokens[$token]['filters'] = $dwc->getFilters(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
unset($matches); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $tokens; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $slot |
174
|
|
|
* @param $lead |
175
|
|
|
* @param $dwc |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getRealDynamicContent($slot, $lead, DynamicContent $dwc) |
180
|
|
|
{ |
181
|
|
|
$content = $dwc->getContent(); |
182
|
|
|
// Determine a translation based on contact's preferred locale |
183
|
|
|
/** @var DynamicContent $translation */ |
184
|
|
|
list($ignore, $translation) = $this->dynamicContentModel->getTranslatedEntity($dwc, $lead); |
185
|
|
|
if ($translation !== $dwc) { |
186
|
|
|
// Use translated version of content |
187
|
|
|
$dwc = $translation; |
188
|
|
|
$content = $dwc->getContent(); |
189
|
|
|
} |
190
|
|
|
$this->dynamicContentModel->createStatEntry($dwc, $lead, $slot); |
191
|
|
|
|
192
|
|
|
$tokenEvent = new TokenReplacementEvent($content, $lead, ['slot' => $slot, 'dynamic_content_id' => $dwc->getId()]); |
193
|
|
|
$this->dispatcher->dispatch(DynamicContentEvents::TOKEN_REPLACEMENT, $tokenEvent); |
194
|
|
|
|
195
|
|
|
return $tokenEvent->getContent(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $slotName |
200
|
|
|
* @param bool $publishedOnly |
201
|
|
|
* |
202
|
|
|
* @return array|\Doctrine\ORM\Tools\Pagination\Paginator |
203
|
|
|
*/ |
204
|
|
|
public function getDwcsBySlotName($slotName, $publishedOnly = false) |
205
|
|
|
{ |
206
|
|
|
$filter = [ |
207
|
|
|
'where' => [ |
208
|
|
|
[ |
209
|
|
|
'col' => 'e.slotName', |
210
|
|
|
'expr' => 'eq', |
211
|
|
|
'val' => $slotName, |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
]; |
215
|
|
|
|
216
|
|
|
if ($publishedOnly) { |
217
|
|
|
$filter['where'][] = [ |
218
|
|
|
'col' => 'e.isPublished', |
219
|
|
|
'expr' => 'eq', |
220
|
|
|
'val' => 1, |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this->dynamicContentModel->getEntities( |
225
|
|
|
[ |
226
|
|
|
'filter' => $filter, |
227
|
|
|
'ignore_paginator' => true, |
228
|
|
|
] |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param Lead $lead |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function convertLeadToArray($lead) |
238
|
|
|
{ |
239
|
|
|
return array_merge( |
240
|
|
|
$lead->getProfileFields(), |
241
|
|
|
[ |
242
|
|
|
'tags' => array_map( |
243
|
|
|
function (Tag $v) { |
244
|
|
|
return $v->getId(); |
245
|
|
|
}, |
246
|
|
|
$lead->getTags()->toArray() |
247
|
|
|
), |
248
|
|
|
] |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.