Skip to content

Commit

Permalink
Merge pull request #22547 from eileenmcnaughton/smart
Browse files Browse the repository at this point in the history
Only format smarty aliases as money if specified
  • Loading branch information
demeritcowboy authored Jan 25, 2022
2 parents 04f0066 + 1c75b02 commit ac90ec6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
16 changes: 15 additions & 1 deletion CRM/Contribute/WorkflowMessage/RecurringCancelled.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
21 changes: 20 additions & 1 deletion CRM/Contribute/WorkflowMessage/RecurringEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,18 @@ public function onRender(TokenRenderEvent $e): void {
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);
Expand Down

0 comments on commit ac90ec6

Please sign in to comment.