3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
11 namespace Civi\FlexMailer\Listener
;
13 use Civi\FlexMailer\Event\ComposeBatchEvent
;
14 use Civi\FlexMailer\Event\RunEvent
;
15 use Civi\FlexMailer\FlexMailerTask
;
16 use Civi\Token\TokenProcessor
;
17 use Civi\Token\TokenRow
;
20 * Class DefaultComposer
21 * @package Civi\FlexMailer\Listener
23 * The DefaultComposer uses a TokenProcessor to generate all messages as
26 class DefaultComposer
extends BaseListener
{
28 public function onRun(RunEvent
$e) {
29 // FIXME: This probably doesn't belong here...
30 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY
) {
31 \CRM_Core_Smarty
::registerStringResource();
36 * Determine whether this composer knows how to handle this mailing.
38 * @param \CRM_Mailing_DAO_Mailing $mailing
41 public function isSupported(\CRM_Mailing_DAO_Mailing
$mailing) {
46 * Given a mailing and a batch of recipients, prepare
47 * the individual messages (headers and body) for each.
49 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
51 public function onCompose(ComposeBatchEvent
$e) {
52 if (!$this->isActive() ||
!$this->isSupported($e->getMailing())) {
56 $tp = new TokenProcessor(\Civi
::service('dispatcher'),
57 $this->createTokenProcessorContext($e));
59 $tpls = $this->createMessageTemplates($e);
60 $tp->addMessage('subject', $tpls['subject'], 'text/plain');
61 $tp->addMessage('body_text', isset($tpls['text']) ?
$tpls['text'] : '',
63 $tp->addMessage('body_html', isset($tpls['html']) ?
$tpls['html'] : '',
67 foreach ($e->getTasks() as $key => $task) {
68 /** @var \Civi\FlexMailer\FlexMailerTask $task */
69 if (!$task->hasContent()) {
70 $tp->addRow()->context($this->createTokenRowContext($e, $task));
81 foreach ($tp->getRows() as $row) {
82 /** @var \Civi\Token\TokenRow $row */
83 /** @var \Civi\FlexMailer\FlexMailerTask $task */
84 $task = $row->context
['flexMailerTask'];
85 $task->setMailParams(array_merge(
86 $this->createMailParams($e, $task, $row),
87 $task->getMailParams()
93 * Define the contextual parameters for the token-processor.
95 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
98 public function createTokenProcessorContext(ComposeBatchEvent
$e) {
100 'controller' => get_class($this),
101 // FIXME: Use template_type, template_options
102 'smarty' => defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ?
TRUE : FALSE,
103 'mailing' => $e->getMailing(),
104 'mailingId' => $e->getMailing()->id
,
110 * Create contextual data for a message recipient.
112 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
113 * @param \Civi\FlexMailer\FlexMailerTask $task
115 * Contextual data describing the recipient.
116 * Typical values are `contactId` or `mailingJobId`.
118 public function createTokenRowContext(
119 ComposeBatchEvent
$e,
123 'contactId' => $task->getContactId(),
124 'mailingJobId' => $e->getJob()->id
,
125 'mailingActionTarget' => array(
126 'id' => $task->getEventQueueId(),
127 'hash' => $task->getHash(),
128 'email' => $task->getAddress(),
130 'flexMailerTask' => $task,
135 * For a given task, prepare the mailing.
137 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
138 * @param \Civi\FlexMailer\FlexMailerTask $task
139 * @param \Civi\Token\TokenRow $row
141 * A list of email parameters, such as "Subject", "text", and/or "html".
142 * @see \CRM_Utils_Hook::alterMailParams
144 public function createMailParams(
145 ComposeBatchEvent
$e,
146 FlexMailerTask
$task,
150 'Subject' => $row->render('subject'),
151 'text' => $row->render('body_text'),
152 'html' => $row->render('body_html'),
157 * Generate the message templates for use with token-processor.
159 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
161 * A list of templates. Some combination of:
166 public function createMessageTemplates(ComposeBatchEvent
$e) {
167 $templates = $e->getMailing()->getTemplates();
168 if ($this->isClickTracking($e)) {
169 $templates = $this->applyClickTracking($e, $templates);
175 * (Tentative) Alter hyperlinks to perform click-tracking.
177 * This functionality probably belongs somewhere else. The
178 * current placement feels quirky, and it's hard to inspect
179 * via `cv debug:event-dispatcher', but it produces the expected
180 * interactions among tokens and click-tracking.
182 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
183 * @param array $templates
187 protected function applyClickTracking(ComposeBatchEvent
$e, $templates) {
188 $mailing = $e->getMailing();
190 if (!empty($templates['html'])) {
191 $templates['html'] = \Civi
::service('civi_flexmailer_html_click_tracker')
192 ->filterContent($templates['html'], $mailing->id
,
193 '{action.eventQueueId}');
195 if (!empty($templates['text'])) {
196 $templates['text'] = \Civi
::service('civi_flexmailer_text_click_tracker')
197 ->filterContent($templates['text'], $mailing->id
,
198 '{action.eventQueueId}');
205 * Determine whether to enable click-tracking.
207 * @param \Civi\FlexMailer\Event\ComposeBatchEvent $e
210 public function isClickTracking(ComposeBatchEvent
$e) {
211 // Don't track clicks on previews. Doing so would accumulate a lot
213 return $e->getMailing()->url_tracking
&& !$e->isPreview();