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\EmailBundle\Event; |
||
13 | |||
14 | use Mautic\CoreBundle\Event\CommonEvent; |
||
15 | use Mautic\EmailBundle\Entity\Email; |
||
16 | use Mautic\EmailBundle\Helper\MailHelper; |
||
17 | use Mautic\EmailBundle\Helper\PlainTextHelper; |
||
18 | use Mautic\LeadBundle\Entity\Lead; |
||
19 | |||
20 | /** |
||
21 | * Class EmailSendEvent. |
||
22 | */ |
||
23 | class EmailSendEvent extends CommonEvent |
||
24 | { |
||
25 | /** |
||
26 | * @var MailHelper |
||
27 | */ |
||
28 | private $helper; |
||
29 | |||
30 | /** |
||
31 | * @var Mail |
||
32 | */ |
||
33 | private $email; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $content = ''; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $plainText = ''; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $subject = ''; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $idHash; |
||
54 | |||
55 | /** |
||
56 | * @var Lead |
||
57 | */ |
||
58 | private $lead; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $source; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $tokens = []; |
||
69 | |||
70 | /** |
||
71 | * @var internalSend |
||
72 | */ |
||
73 | private $internalSend = false; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | private $textHeaders = []; |
||
79 | |||
80 | /** |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $isDynamicContentParsing; |
||
84 | |||
85 | /** |
||
86 | * EmailSendEvent constructor. |
||
87 | * |
||
88 | * @param array $args |
||
89 | * @param bool $isDynamicContentParsing |
||
90 | */ |
||
91 | public function __construct(MailHelper $helper = null, $args = [], $isDynamicContentParsing = false) |
||
92 | { |
||
93 | $this->helper = $helper; |
||
94 | |||
95 | if (isset($args['content'])) { |
||
96 | $this->content = $args['content']; |
||
97 | } |
||
98 | |||
99 | if (isset($args['plainText'])) { |
||
100 | $this->plainText = $args['plainText']; |
||
101 | } |
||
102 | |||
103 | if (isset($args['subject'])) { |
||
104 | $this->subject = $args['subject']; |
||
105 | } |
||
106 | |||
107 | if (isset($args['email'])) { |
||
108 | $this->email = $args['email']; |
||
109 | } |
||
110 | |||
111 | if (!$this->subject && isset($args['email']) && $args['email'] instanceof Email) { |
||
112 | $this->subject = $args['email']->getSubject(); |
||
113 | } |
||
114 | |||
115 | if (isset($args['idHash'])) { |
||
116 | $this->idHash = $args['idHash']; |
||
117 | } |
||
118 | |||
119 | if (isset($args['lead'])) { |
||
120 | $this->lead = $args['lead']; |
||
121 | } |
||
122 | |||
123 | if (isset($args['source'])) { |
||
124 | $this->source = $args['source']; |
||
125 | } |
||
126 | |||
127 | if (isset($args['tokens'])) { |
||
128 | $this->tokens = $args['tokens']; |
||
129 | } |
||
130 | |||
131 | if (isset($args['internalSend'])) { |
||
132 | $this->internalSend = $args['internalSend']; |
||
133 | } elseif (null !== $helper) { |
||
134 | $this->internalSend = $helper->isInternalSend(); |
||
135 | } |
||
136 | |||
137 | if (isset($args['textHeaders'])) { |
||
138 | $this->textHeaders = $args['textHeaders']; |
||
139 | } |
||
140 | |||
141 | $this->isDynamicContentParsing = $isDynamicContentParsing; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Check if this email is an internal send or to the lead; if an internal send, don't append lead tracking. |
||
146 | * |
||
147 | * @return internalSend |
||
148 | */ |
||
149 | public function isInternalSend() |
||
150 | { |
||
151 | return $this->internalSend; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Return if the transport and mailer is in batch mode (tokenized emails). |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function inTokenizationMode() |
||
160 | { |
||
161 | return (null !== $this->helper) ? $this->helper->inTokenizationMode() : false; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Returns the Email entity. |
||
166 | * |
||
167 | * @return Email |
||
168 | */ |
||
169 | public function getEmail() |
||
170 | { |
||
171 | return (null !== $this->helper) ? $this->helper->getEmail() : $this->email; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get email content. |
||
176 | * |
||
177 | * @param $replaceTokens |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getContent($replaceTokens = false) |
||
182 | { |
||
183 | if (null !== $this->helper) { |
||
184 | $content = $this->helper->getBody(); |
||
185 | } else { |
||
186 | $content = $this->content; |
||
187 | } |
||
188 | |||
189 | return ($replaceTokens) ? str_replace(array_keys($this->getTokens()), $this->getTokens(), $content) : $content; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Set email content. |
||
194 | * |
||
195 | * @param $content |
||
196 | */ |
||
197 | public function setContent($content) |
||
198 | { |
||
199 | if (null !== $this->helper) { |
||
200 | $this->helper->setBody($content, 'text/html', null, true); |
||
201 | } else { |
||
202 | $this->content = $content; |
||
203 | } |
||
204 | $this->setGeneratedPlainText(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get email content. |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getPlainText() |
||
213 | { |
||
214 | if (null !== $this->helper) { |
||
215 | return $this->helper->getPlainText(); |
||
216 | } else { |
||
217 | return $this->plainText; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param $content |
||
223 | */ |
||
224 | public function setPlainText($content) |
||
225 | { |
||
226 | if (null !== $this->helper) { |
||
227 | $this->helper->setPlainText($content); |
||
228 | } else { |
||
229 | $this->plainText = $content; |
||
230 | } |
||
231 | $this->setGeneratedPlainText(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Check if plain text is empty. If yes, generate it. |
||
236 | */ |
||
237 | private function setGeneratedPlainText() |
||
238 | { |
||
239 | $htmlContent = $this->getContent(); |
||
240 | if ('' === $this->getPlainText() && '' !== $htmlContent) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
241 | $parser = new PlainTextHelper(); |
||
242 | $generatedPlainText = $parser->setHtml($htmlContent)->getText(); |
||
243 | if ('' !== $generatedPlainText) { |
||
244 | $this->setPlainText($generatedPlainText); |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getSubject() |
||
253 | { |
||
254 | if (null !== $this->helper) { |
||
255 | return $this->helper->getSubject(); |
||
256 | } else { |
||
257 | return $this->subject; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $subject |
||
263 | * |
||
264 | * @return EmailSendEvent |
||
265 | */ |
||
266 | public function setSubject($subject) |
||
267 | { |
||
268 | if (null !== $this->helper) { |
||
269 | $this->helper->setSubject($subject); |
||
270 | } else { |
||
271 | $this->subject = $subject; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get the MailHelper object. |
||
277 | * |
||
278 | * @return MailHelper |
||
279 | */ |
||
280 | public function getHelper() |
||
281 | { |
||
282 | return $this->helper; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | public function getLead() |
||
289 | { |
||
290 | return (null !== $this->helper) ? $this->helper->getLead() : $this->lead; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getIdHash() |
||
297 | { |
||
298 | return (null !== $this->helper) ? $this->helper->getIdHash() : $this->idHash; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @return array |
||
303 | */ |
||
304 | public function getSource() |
||
305 | { |
||
306 | return (null !== $this->helper) ? $this->helper->getSource() : $this->source; |
||
307 | } |
||
308 | |||
309 | public function addTokens(array $tokens) |
||
310 | { |
||
311 | $this->tokens = array_merge($this->tokens, $tokens); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param $key |
||
316 | * @param $value |
||
317 | */ |
||
318 | public function addToken($key, $value) |
||
319 | { |
||
320 | $this->tokens[$key] = $value; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Get token array. |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | public function getTokens($includeGlobal = true) |
||
329 | { |
||
330 | $tokens = $this->tokens; |
||
331 | |||
332 | if ($includeGlobal && null !== $this->helper) { |
||
333 | $tokens = array_merge($this->helper->getGlobalTokens(), $tokens); |
||
334 | } |
||
335 | |||
336 | return $tokens; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @param $name |
||
341 | * @param $value |
||
342 | */ |
||
343 | public function addTextHeader($name, $value) |
||
344 | { |
||
345 | if (null !== $this->helper) { |
||
346 | $this->helper->addCustomHeader($name, $value); |
||
347 | } else { |
||
348 | $this->textHeaders[$name] = $value; |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return array |
||
354 | */ |
||
355 | public function getTextHeaders() |
||
356 | { |
||
357 | return (null !== $this->helper) ? $this->helper->getCustomHeaders() : $this->textHeaders; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Check if the listener should append it's own clickthrough in URLs or if the email tracking URL conversion process should take care of it. |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function shouldAppendClickthrough() |
||
366 | { |
||
367 | return !$this->isInternalSend() && null === $this->getEmail(); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Generate a clickthrough array for URLs. |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | public function generateClickthrough() |
||
376 | { |
||
377 | $source = $this->getSource(); |
||
378 | $email = $this->getEmail(); |
||
379 | $clickthrough = [ |
||
380 | //what entity is sending the email? |
||
381 | 'source' => $source, |
||
382 | //the email being sent to be logged in page hit if applicable |
||
383 | 'email' => (null != $email) ? $email->getId() : null, |
||
384 | 'stat' => $this->getIdHash(), |
||
385 | ]; |
||
386 | $lead = $this->getLead(); |
||
387 | if (null !== $lead) { |
||
388 | $clickthrough['lead'] = $lead['id']; |
||
389 | } |
||
390 | |||
391 | return $clickthrough; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Get the content hash to note if the content has been changed. |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getContentHash() |
||
400 | { |
||
401 | if (null !== $this->helper) { |
||
402 | return $this->helper->getContentHash(); |
||
403 | } else { |
||
404 | return md5($this->getContent().$this->getPlainText()); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function isDynamicContentParsing() |
||
412 | { |
||
413 | return $this->isDynamicContentParsing; |
||
414 | } |
||
415 | } |
||
416 |