Merge pull request #14266 from seamuslee001/dev_core_369
[civicrm-core.git] / CRM / Mailing / ActionTokens.php
CommitLineData
56df2d06
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
56df2d06 6 +--------------------------------------------------------------------+
6b83d5bd 7 | Copyright CiviCRM LLC (c) 2004-2019 |
56df2d06
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * Class CRM_Mailing_ActionTokens
31 *
32 * Generate "action.*" tokens for mailings.
33 *
34 * To activate these tokens, the TokenProcessor context must specify:
35 * "mailingJobId" (int)
36 * "mailingActionTarget" (array) with keys:
37 * 'id' => int, event queue ID
38 * 'hash' => string, event queue hash code
39 * 'contact_id' => int, contact_id,
40 * 'email' => string, email
41 * 'phone' => string, phone
42 */
43class CRM_Mailing_ActionTokens extends \Civi\Token\AbstractTokenSubscriber {
44
45 /**
46 * Class constructor.
47 */
48 public function __construct() {
49 // TODO: Think about supporting dynamic tokens like "{action.subscribe.\d+}"
be2fb01f 50 parent::__construct('action', [
56df2d06
TO
51 'subscribeUrl' => ts('Subscribe URL (Action)'),
52 'forward' => ts('Forward URL (Action)'),
53 'optOut' => ts('Opt-Out (Action)'),
54 'optOutUrl' => ts('Opt-Out URL (Action)'),
55 'reply' => ts('Reply (Action)'),
56 'unsubscribe' => ts('Unsubscribe (Action)'),
57 'unsubscribeUrl' => ts('Unsubscribe URL (Action)'),
58 'resubscribe' => ts('Resubscribe (Action)'),
59 'resubscribeUrl' => ts('Resubscribe URL (Action)'),
60 'eventQueueId' => ts('Event Queue ID'),
be2fb01f 61 ]);
56df2d06
TO
62 }
63
9091e034
TO
64 /**
65 * @inheritDoc
66 */
67 public function checkActive(\Civi\Token\TokenProcessor $processor) {
e026b9e4
TO
68 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
69 || in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
9091e034
TO
70 }
71
56df2d06 72 /**
0ba33ffb 73 * @inheritDoc
56df2d06
TO
74 */
75 public function evaluateToken(
76 \Civi\Token\TokenRow $row,
77 $entity,
78 $field,
79 $prefetch = NULL
80 ) {
81 // Most CiviMail action tokens were implemented via getActionTokenReplacement().
82 // However, {action.subscribeUrl} has a second implementation via
83 // replaceSubscribeInviteTokens(). The two appear mostly the same.
84 // We use getActionTokenReplacement() since it's more consistent. However,
85 // this doesn't provide the dynamic/parameterized tokens of
86 // replaceSubscribeInviteTokens().
87
88 if (empty($row->context['mailingJobId']) || empty($row->context['mailingActionTarget']['hash'])) {
e026b9e4
TO
89 // Strictly speaking, it doesn't make much sense to generate action-tokens when there's no job ID, but traditional CiviMail
90 // does this in v5.6+ for "Preview" functionality. Relaxing this strictness check ensures parity between newer+older styles.
91 // throw new \CRM_Core_Exception("Error: Cannot use action tokens unless context defines mailingJobId and mailingActionTarget.");
56df2d06
TO
92 }
93
94 if ($field === 'eventQueueId') {
95 $row->format('text/plain')->tokens($entity, $field, $row->context['mailingActionTarget']['id']);
96 return;
97 }
98
99 list($verp, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls(
100 $row->context['mailingJobId'],
101 $row->context['mailingActionTarget']['id'],
102 $row->context['mailingActionTarget']['hash'],
103 // Note: Behavior is already undefined for SMS/'phone' mailings...
104 $row->context['mailingActionTarget']['email']
105 );
106
107 $row->format('text/plain')->tokens($entity, $field,
108 CRM_Utils_Token::getActionTokenReplacement(
109 $field, $verp, $urls, FALSE));
110 $row->format('text/html')->tokens($entity, $field,
111 CRM_Utils_Token::getActionTokenReplacement(
112 $field, $verp, $urls, TRUE));
113 }
114
56df2d06 115}