CRM-19690 - CRM_Mailing_TokensTest - Define negative test scenario
[civicrm-core.git] / CRM / Mailing / ActionTokens.php
CommitLineData
56df2d06
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2016 |
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+}"
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 * Evaluate the content of a single token.
66 *
67 * @param \Civi\Token\TokenRow $row
68 * The record for which we want token values.
69 * @param string $entity
70 * @param string $field
71 * The name of the token field.
72 * @param mixed $prefetch
73 * Any data that was returned by the prefetch().
74 *
75 * @return mixed
76 * @throws \CRM_Core_Exception
77 */
78 public function evaluateToken(
79 \Civi\Token\TokenRow $row,
80 $entity,
81 $field,
82 $prefetch = NULL
83 ) {
84 // Most CiviMail action tokens were implemented via getActionTokenReplacement().
85 // However, {action.subscribeUrl} has a second implementation via
86 // replaceSubscribeInviteTokens(). The two appear mostly the same.
87 // We use getActionTokenReplacement() since it's more consistent. However,
88 // this doesn't provide the dynamic/parameterized tokens of
89 // replaceSubscribeInviteTokens().
90
91 if (empty($row->context['mailingJobId']) || empty($row->context['mailingActionTarget']['hash'])) {
92 throw new \CRM_Core_Exception("Error: Cannot use action tokens unless context defines mailingJobId and mailingActionTarget.");
93 }
94
95 if ($field === 'eventQueueId') {
96 $row->format('text/plain')->tokens($entity, $field, $row->context['mailingActionTarget']['id']);
97 return;
98 }
99
100 list($verp, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls(
101 $row->context['mailingJobId'],
102 $row->context['mailingActionTarget']['id'],
103 $row->context['mailingActionTarget']['hash'],
104 // Note: Behavior is already undefined for SMS/'phone' mailings...
105 $row->context['mailingActionTarget']['email']
106 );
107
108 $row->format('text/plain')->tokens($entity, $field,
109 CRM_Utils_Token::getActionTokenReplacement(
110 $field, $verp, $urls, FALSE));
111 $row->format('text/html')->tokens($entity, $field,
112 CRM_Utils_Token::getActionTokenReplacement(
113 $field, $verp, $urls, TRUE));
114 }
115
56df2d06 116}