From 1c75b02da3ea06ba4ecc9f0730e79b9ba83aad82 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 18 Jan 2022 11:10:57 +1300 Subject: [PATCH] Only format smarty aliases as money if specified smarty aliases allow tokens to be assigned to the template as smarty variables. They are very new, have only been implemented in these 2 places and are not accessible from outside of core, which both have unit test cover. When trying to extend it to cover another variable (totalTaxAmount) in another template I found that a raw format is appropriate. I think it makes sense to explicitly specify. Code comment Co-authored-by: Tim Otten --- .../WorkflowMessage/RecurringCancelled.php | 16 +++++++++++++- .../WorkflowMessage/RecurringEdit.php | 21 ++++++++++++++++++- Civi/Token/TokenCompatSubscriber.php | 13 ++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CRM/Contribute/WorkflowMessage/RecurringCancelled.php b/CRM/Contribute/WorkflowMessage/RecurringCancelled.php index 2ffdfc14cd..93c39e53b0 100644 --- a/CRM/Contribute/WorkflowMessage/RecurringCancelled.php +++ b/CRM/Contribute/WorkflowMessage/RecurringCancelled.php @@ -19,8 +19,22 @@ class CRM_Contribute_WorkflowMessage_RecurringCancelled extends Civi\WorkflowMes */ public $contact; + /** + * Export tokens to smarty as variables. + * + * The key represents the smarty token and the value is the token as + * requested from the token processor. + * + * The token is 'the entire part between the curly quotes' eg. + * + * '{contribution_recur.amount|crmMoney}. + * + * Unlike using the contribution directly it will default to 'raw' formatting. + * + * @param array $export + */ protected function exportExtraTokenContext(array &$export): void { - $export['smartyTokenAlias']['amount'] = 'contribution_recur.amount'; + $export['smartyTokenAlias']['amount'] = 'contribution_recur.amount|crmMoney'; $export['smartyTokenAlias']['recur_frequency_unit'] = 'contribution_recur.frequency_unit:label'; $export['smartyTokenAlias']['recur_frequency_interval'] = 'contribution_recur.frequency_interval'; } diff --git a/CRM/Contribute/WorkflowMessage/RecurringEdit.php b/CRM/Contribute/WorkflowMessage/RecurringEdit.php index 7e211c8724..495db5c5b7 100644 --- a/CRM/Contribute/WorkflowMessage/RecurringEdit.php +++ b/CRM/Contribute/WorkflowMessage/RecurringEdit.php @@ -33,13 +33,32 @@ class CRM_Contribute_WorkflowMessage_RecurringEdit extends Civi\WorkflowMessage\ */ public $receiptFromEmail; + /** + * Export tokens to smarty as variables. + * + * The key represents the smarty token and the value is the token as + * requested from the token processor. + * + * The token is 'the entire part between the curly quotes' eg. + * + * '{contribution_recur.amount|crmMoney}. + * + * Unlike using the contribution directly it will default to 'raw' formatting. + * + * @param array $export + */ protected function exportExtraTokenContext(array &$export): void { $export['smartyTokenAlias']['installments'] = 'contribution_recur.installments'; - $export['smartyTokenAlias']['amount'] = 'contribution_recur.amount'; + $export['smartyTokenAlias']['amount'] = 'contribution_recur.amount|crmMoney'; $export['smartyTokenAlias']['recur_frequency_unit'] = 'contribution_recur.frequency_unit:label'; $export['smartyTokenAlias']['recur_frequency_interval'] = 'contribution_recur.frequency_interval'; } + /** + * Extra variables to be exported to smarty based on being calculated. + * + * @param array $export + */ protected function exportExtraTplParams(array &$export): void { if (empty($export['receipt_from_email']) && !empty($this->from)) { // At a minimum, we can at least autofill 'receipt_from_email' in the case where it's missing. diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 6274d8f29c..b349f9c380 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -70,9 +70,18 @@ class TokenCompatSubscriber implements EventSubscriberInterface { if ($useSmarty) { $smartyVars = []; foreach ($e->context['smartyTokenAlias'] ?? [] as $smartyName => $tokenName) { - $smartyVars[$smartyName] = \CRM_Utils_Array::pathGet($e->row->tokens, explode('.', $tokenName), $e->context['locale'] ?? NULL); + $tokenParts = explode('|', $tokenName); + $modifier = $tokenParts[1] ?? ''; + $smartyVars[$smartyName] = \CRM_Utils_Array::pathGet($e->row->tokens, explode('.', $tokenParts[0]), $e->context['locale'] ?? NULL); if ($smartyVars[$smartyName] instanceof \Brick\Money\Money) { - $smartyVars[$smartyName] = \Civi::format()->money($smartyVars[$smartyName]->getAmount(), $smartyVars[$smartyName]->getCurrency()); + // TODO: We should reuse the filters from TokenProcessor::filterTokenValue() + if ($modifier === 'crmMoney') { + $smartyVars[$smartyName] = \Civi::format() + ->money($smartyVars[$smartyName]->getAmount(), $smartyVars[$smartyName]->getCurrency()); + } + else { + $smartyVars[$smartyName] = $smartyVars[$smartyName]->getAmount(); + } } } \CRM_Core_Smarty::singleton()->pushScope($smartyVars); -- 2.25.1