4 use Civi\Token\Event\TokenRenderEvent
;
5 use Civi\Token\Event\TokenValueEvent
;
6 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
9 * Class TokenCompatSubscriber
12 * This class provides a compatibility layer for using CRM_Utils_Token
13 * helpers within TokenProcessor.
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.
21 class TokenCompatSubscriber
implements EventSubscriberInterface
{
26 public static function getSubscribedEvents() {
28 Events
::TOKEN_EVALUATE
=> 'onEvaluate',
29 Events
::TOKEN_RENDER
=> 'onRender',
36 * @param TokenValueEvent $e
37 * @throws TokenException
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.
44 $hookTokens = array();
45 \CRM_Utils_Hook
::tokens($hookTokens);
46 $categories = array_keys($hookTokens);
47 $e->getTokenProcessor()->context
['hookTokenCategories'] = $categories;
49 $messageTokens = $e->getTokenProcessor()->getMessageTokens();
51 foreach ($e->getRows() as $row) {
52 if (empty($row->context
['contact'])) {
54 array('contact_id', '=', $row->context
['contactId'], 0, 0),
56 list($contact, $_) = \CRM_Contact_BAO_Query
::apiQuery($params);
57 $contact = reset($contact); //CRM-4524
58 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
59 // FIXME: Need to differentiate errors which kill the batch vs the individual row.
60 throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context
['contactId']);
64 $contact = $row->context
['contact'];
67 if (!empty($row->context
['tmpTokenParams'])) {
68 // merge activity tokens with contact array
69 // this is pretty weird.
70 $contact = array_merge($contact, $row->context
['tmpTokenParams']);
73 // Note: This is a small contract change from the past; data should be missing
75 //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']);
76 \CRM_Utils_Hook
::tokenValues($contact,
77 $row->context
['contactId'],
78 empty($row->context
['mailingJob']) ?
NULL : $row->context
['mailingJob']->id
,
80 $row->context
['controller']
83 $row->context('contact', $contact);
88 * Apply the various CRM_Utils_Token helpers.
90 * @param TokenRenderEvent $e
92 public function onRender(TokenRenderEvent
$e) {
93 $isHtml = ($e->message
['format'] == 'text/html');
94 $useSmarty = !empty($e->context
['smarty']);
96 $e->string = \CRM_Utils_Token
::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain
::getDomain(), $isHtml, $e->message
['tokens'], $useSmarty);
98 if (!empty($e->context
['contact'])) {
99 $e->string = \CRM_Utils_Token
::replaceContactTokens($e->string, $e->context
['contact'], $isHtml, $e->message
['tokens'], FALSE, $useSmarty);
101 // FIXME: This may depend on $contact being merged with hook values.
102 $e->string = \CRM_Utils_Token
::replaceHookTokens($e->string, $e->context
['contact'], $e->context
['hookTokenCategories'], $isHtml, $useSmarty);
104 \CRM_Utils_Token
::replaceGreetingTokens($e->string, NULL, $e->context
['contact']['contact_id'], NULL, $useSmarty);
108 $smarty = \CRM_Core_Smarty
::singleton();
109 $e->string = $smarty->fetch("string:" . $e->string);