Skip to content

Commit

Permalink
[REF] Complete decommissioning on CRM/Contribute/Form/Task/PDFLetterC…
Browse files Browse the repository at this point in the history
…ommon.php

This removes the last function fromCRM/Contribute/Form/Task/PDFLetterCommon.php

The function was calling a protected function on the parent which had to be unravelled.

I used the method we used in
https://github.com/civicrm/civicrm-core/blob/5e67eb7ff3f1f202d5112d15ad5c6a5f23f35795/CRM/SMS/Form/Upload.php#L341
to replace the code with the part that we have tested as being done in via render - ie replacing
hook, contact, domain tokens & smarty parsing.

However, there are quite a few tests on this code & one checked that we don't over-call
the token hook. I added some caching for the results of this hook so we can start to
eliminate these but I also allowed the calls to increment by 1 because it is not 1 per row

I made the results of this hook optional to replaceHookTokens & in general I think that
is the only place it needs to be called - perhaps once it used to pass more parameters &
the results were dynamic but now they really aren't
  • Loading branch information
eileenmcnaughton committed Apr 27, 2021
1 parent 6e53da3 commit f99d0e5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
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'] = array_keys(\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

0 comments on commit f99d0e5

Please sign in to comment.