Merge pull request #12285 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Mailing / ActionTokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2018 |
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 */
43 class 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+}"
50 parent::__construct('action', array(
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'),
61 ));
62 }
63
64 /**
65 * @inheritDoc
66 */
67 public function checkActive(\Civi\Token\TokenProcessor $processor) {
68 return !empty($processor->context['mailingId']) || !empty($processor->context['mailing']);
69 }
70
71 /**
72 * @inheritDoc
73 */
74 public function evaluateToken(
75 \Civi\Token\TokenRow $row,
76 $entity,
77 $field,
78 $prefetch = NULL
79 ) {
80 // Most CiviMail action tokens were implemented via getActionTokenReplacement().
81 // However, {action.subscribeUrl} has a second implementation via
82 // replaceSubscribeInviteTokens(). The two appear mostly the same.
83 // We use getActionTokenReplacement() since it's more consistent. However,
84 // this doesn't provide the dynamic/parameterized tokens of
85 // replaceSubscribeInviteTokens().
86
87 if (empty($row->context['mailingJobId']) || empty($row->context['mailingActionTarget']['hash'])) {
88 throw new \CRM_Core_Exception("Error: Cannot use action tokens unless context defines mailingJobId and mailingActionTarget.");
89 }
90
91 if ($field === 'eventQueueId') {
92 $row->format('text/plain')->tokens($entity, $field, $row->context['mailingActionTarget']['id']);
93 return;
94 }
95
96 list($verp, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls(
97 $row->context['mailingJobId'],
98 $row->context['mailingActionTarget']['id'],
99 $row->context['mailingActionTarget']['hash'],
100 // Note: Behavior is already undefined for SMS/'phone' mailings...
101 $row->context['mailingActionTarget']['email']
102 );
103
104 $row->format('text/plain')->tokens($entity, $field,
105 CRM_Utils_Token::getActionTokenReplacement(
106 $field, $verp, $urls, FALSE));
107 $row->format('text/html')->tokens($entity, $field,
108 CRM_Utils_Token::getActionTokenReplacement(
109 $field, $verp, $urls, TRUE));
110 }
111
112 }