Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Complete decommissioning of CRM/Contribute/Form/Task/PDFLetterCommon.php #20172

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public function generateHtml(&$contact, $contribution, $groupBy, $contributions,
CRM_Core_Session::setStatus(ts('You have selected the table cell separator, but one or more token fields are not placed inside a table cell. This would result in invalid HTML, so comma separators have been used instead.'));
}
$validated = TRUE;
$html = str_replace($separator, $realSeparator, CRM_Contribute_Form_Task_PDFLetterCommon::resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator, $groupedContributions));
$html = str_replace($separator, $realSeparator, $this->resolveTokens($html_message, $contact, $contribution, $messageToken, $grouped, $separator, $groupedContributions));
}

return $html;
Expand Down Expand Up @@ -539,4 +539,30 @@ public static function isValidHTMLWithTableSeparator($tokens, $html) {
return TRUE;
}

/**
*
* @param string $html_message
* @param array $contact
* @param array $contribution
* @param array $messageToken
* @param bool $grouped
* Does this letter represent more than one contribution.
* @param string $separator
* What is the preferred letter separator.
* @param array $contributions
*
* @return string
*/
protected function resolveTokens(string $html_message, $contact, $contribution, $messageToken, $grouped, $separator, $contributions): string {
if ($grouped) {
$tokenHtml = CRM_Utils_Token::replaceMultipleContributionTokens($separator, $html_message, $contributions, $messageToken);
}
else {
// no change to normal behaviour to avoid risk of breakage
$tokenHtml = CRM_Utils_Token::replaceContributionTokens($html_message, $contribution, TRUE, $messageToken);
}
$useSmarty = (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY);
return CRM_Core_BAO_MessageTemplate::renderMessageTemplate(['text' => '', 'html' => $tokenHtml, 'subject' => ''], !$useSmarty, $contact['contact_id'], ['contact' => $contact])['html'];
}

}
46 changes: 0 additions & 46 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php

This file was deleted.

19 changes: 18 additions & 1 deletion CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,13 @@ public static function getContactTokenReplacement(
public static function &replaceHookTokens(
$str,
&$contact,
&$categories,
$categories = NULL,
$html = FALSE,
$escapeSmarty = FALSE
) {
if (!$categories) {
$categories = self::getTokenCategories();
}
foreach ($categories as $key) {
$str = preg_replace_callback(
self::tokenRegex($key),
Expand All @@ -797,6 +800,20 @@ function ($matches) use (&$contact, $key, $html, $escapeSmarty) {
return $str;
}

/**
* Get the categories required for rendering tokens.
*
* @return array
*/
public static function getTokenCategories(): array {
if (!isset(\Civi::$statics[__CLASS__]['token_categories'])) {
$tokens = [];
\CRM_Utils_Hook::tokens($tokens);
\Civi::$statics[__CLASS__]['token_categories'] = array_keys($tokens);
}
return \Civi::$statics[__CLASS__]['token_categories'];
}

/**
* Parse html through Smarty resolving any smarty functions.
* @param string $tokenHtml
Expand Down
13 changes: 5 additions & 8 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ public static function getSubscribedEvents() {
* @throws TokenException
*/
public function onEvaluate(TokenValueEvent $e) {
// For reasons unknown, replaceHookTokens requires a pre-computed list of
// hook *categories* (aka entities aka namespaces). We'll cache
// this in the TokenProcessor's context.

$hookTokens = [];
\CRM_Utils_Hook::tokens($hookTokens);
$categories = array_keys($hookTokens);
$e->getTokenProcessor()->context['hookTokenCategories'] = $categories;
// For reasons unknown, replaceHookTokens used to require a pre-computed list of
// hook *categories* (aka entities aka namespaces). We cache
// this in the TokenProcessor's context but can likely remove it now.

$e->getTokenProcessor()->context['hookTokenCategories'] = \CRM_Utils_Token::getTokenCategories();

$messageTokens = $e->getTokenProcessor()->getMessageTokens();
$returnProperties = array_fill_keys($messageTokens['contact'] ?? [], 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function testPostProcessGroupByContact(): void {
<td></td>
</tr>
<!--
-->
-->
<tr>
<td><strong>Total</strong></td>
<td><strong>$ 100.00</strong></td>
Expand All @@ -322,7 +322,7 @@ public function testPostProcessGroupByContact(): void {
<th>Source</th>
</tr>
<!--
-->
-->
<tr>
<td>25 December 2016</td>
<td>$ 10.00</td>
Expand Down Expand Up @@ -354,7 +354,9 @@ public function testPostProcessGroupByContact(): void {
$this->assertEquals($html[2], $activities['values'][1]['details']);
// Checking it is not called multiple times.
// once for each contact create + once for the activities.
$this->assertEquals(3, $this->hookTokensCalled);
// & once for the token processor, for now.
// By calling the cached function we can get this down to 1
$this->assertEquals(4, $this->hookTokensCalled);
$this->mut->checkAllMailLog($html);

}
Expand Down