Commit | Line | Data |
---|---|---|
43ceab3f TO |
1 | <?php |
2 | namespace Civi\Token; | |
3 | ||
4 | use Civi\Token\Event\TokenRenderEvent; | |
5 | use Civi\Token\Event\TokenValueEvent; | |
6 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
7 | ||
8 | /** | |
9 | * Class TokenCompatSubscriber | |
10 | * @package Civi\Token | |
11 | * | |
12 | * This class provides a compatibility layer for using CRM_Utils_Token | |
13 | * helpers within TokenProcessor. | |
14 | * | |
15 | * THIS IS NOT A GOOD EXAMPLE TO EMULATE. The class exists to two | |
16 | * bridge two different designs. CRM_Utils_Token has some | |
17 | * undesirable elements (like iterative token substitution). | |
18 | * However, if you're refactor CRM_Utils_Token or improve the | |
19 | * bridge, then it makes sense to update this class. | |
20 | */ | |
21 | class TokenCompatSubscriber implements EventSubscriberInterface { | |
22 | ||
23 | /** | |
24 | * @inheritDoc | |
25 | */ | |
26 | public static function getSubscribedEvents() { | |
27 | return array( | |
28 | Events::TOKEN_EVALUATE => 'onEvaluate', | |
29 | Events::TOKEN_RENDER => 'onRender', | |
30 | ); | |
31 | } | |
32 | ||
33 | /** | |
34 | * Load token data. | |
35 | * | |
36 | * @param TokenValueEvent $e | |
37 | * @throws TokenException | |
38 | */ | |
39 | public function onEvaluate(TokenValueEvent $e) { | |
40 | // For reasons unknown, replaceHookTokens requires a pre-computed list of | |
41 | // hook *categories* (aka entities aka namespaces). We'll cache | |
42 | // this in the TokenProcessor's context. | |
43 | ||
44 | $hookTokens = array(); | |
45 | \CRM_Utils_Hook::tokens($hookTokens); | |
46 | $categories = array_keys($hookTokens); | |
47 | $e->getTokenProcessor()->context['hookTokenCategories'] = $categories; | |
48 | ||
49 | $messageTokens = $e->getTokenProcessor()->getMessageTokens(); | |
50 | ||
51 | foreach ($e->getRows() as $row) { | |
7558391f | 52 | $contactId = $row->context['contactId']; |
43ceab3f TO |
53 | if (empty($row->context['contact'])) { |
54 | $params = array( | |
7558391f | 55 | array('contact_id', '=', $contactId, 0, 0), |
43ceab3f TO |
56 | ); |
57 | list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params); | |
58 | $contact = reset($contact); //CRM-4524 | |
59 | if (!$contact || is_a($contact, 'CRM_Core_Error')) { | |
60 | // FIXME: Need to differentiate errors which kill the batch vs the individual row. | |
61 | throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); | |
62 | } | |
63 | } | |
64 | else { | |
65 | $contact = $row->context['contact']; | |
66 | } | |
67 | ||
68 | if (!empty($row->context['tmpTokenParams'])) { | |
69 | // merge activity tokens with contact array | |
70 | // this is pretty weird. | |
71 | $contact = array_merge($contact, $row->context['tmpTokenParams']); | |
72 | } | |
73 | ||
7558391f | 74 | $contactArray = !is_array($contactId) ? array($contactId => $contact) : $contact; |
75 | ||
43ceab3f TO |
76 | // Note: This is a small contract change from the past; data should be missing |
77 | // less randomly. | |
78 | //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']); | |
7558391f | 79 | \CRM_Utils_Hook::tokenValues($contactArray, |
80 | (array) $contactId, | |
43ceab3f TO |
81 | empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id, |
82 | $messageTokens, | |
83 | $row->context['controller'] | |
84 | ); | |
85 | ||
051938c6 | 86 | // merge the custom tokens in the $contact array |
87 | if (!empty($contactArray[$contactId])) { | |
88 | $contact = array_merge($contact, $contactArray[$contactId]); | |
89 | } | |
43ceab3f TO |
90 | $row->context('contact', $contact); |
91 | } | |
92 | } | |
93 | ||
94 | /** | |
95 | * Apply the various CRM_Utils_Token helpers. | |
96 | * | |
97 | * @param TokenRenderEvent $e | |
98 | */ | |
99 | public function onRender(TokenRenderEvent $e) { | |
100 | $isHtml = ($e->message['format'] == 'text/html'); | |
101 | $useSmarty = !empty($e->context['smarty']); | |
102 | ||
43ceab3f TO |
103 | $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain::getDomain(), $isHtml, $e->message['tokens'], $useSmarty); |
104 | ||
105 | if (!empty($e->context['contact'])) { | |
106 | $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], FALSE, $useSmarty); | |
43ceab3f TO |
107 | |
108 | // FIXME: This may depend on $contact being merged with hook values. | |
109 | $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty); | |
e66cb98f TO |
110 | |
111 | \CRM_Utils_Token::replaceGreetingTokens($e->string, NULL, $e->context['contact']['contact_id'], NULL, $useSmarty); | |
43ceab3f TO |
112 | } |
113 | ||
114 | if ($useSmarty) { | |
115 | $smarty = \CRM_Core_Smarty::singleton(); | |
116 | $e->string = $smarty->fetch("string:" . $e->string); | |
117 | } | |
118 | } | |
119 | ||
120 | } |