Skip to content

Commit

Permalink
Merge pull request #21521 from eileenmcnaughton/member_tokens
Browse files Browse the repository at this point in the history
dev/core#2832 Switch membership pdf to use token processor
  • Loading branch information
monishdeb authored Sep 29, 2021
2 parents 525027a + a95f041 commit 89d280e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
19 changes: 10 additions & 9 deletions CRM/Member/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Membership;

/**
* This class provides the functionality to create PDF letter for a group of
* contacts or a single contact.
Expand Down Expand Up @@ -119,20 +121,19 @@ public function postProcessMembers($membershipIDs, $skipOnHold, $skipDeceased, $
*
*/
public function generateHTML($membershipIDs, $messageToken, $html_message): array {
$memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
$memberships = Membership::get(FALSE)
->addWhere('id', 'IN', $membershipIDs)
->addSelect('contact_id')->execute();
$html = [];

foreach ($membershipIDs as $membershipID) {
$membership = $memberships[$membershipID];
// get contact information
$contactId = $membership['contact_id'];
$tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $html_message, $messageToken);
foreach ($memberships as $membership) {
$html[] = CRM_Core_BAO_MessageTemplate::renderTemplate([
'messageTemplate' => ['msg_html' => $tokenHtml],
'contactId' => $contactId,
'messageTemplate' => ['msg_html' => $html_message],
'contactId' => $membership['contact_id'],
'schema' => ['contactId', 'membershipId'],
'tokenContext' => ['membershipId' => $membership['id']],
'disableSmarty' => !defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY,
])['html'];

}
return $html;
}
Expand Down
42 changes: 18 additions & 24 deletions CRM/Member/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Civi\Api4\Membership;

/**
* This class provides the common functionality for creating PDF letter for
* members
Expand All @@ -24,7 +26,7 @@ class CRM_Member_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLett
public static function postProcessMembers(&$form, $membershipIDs, $skipOnHold, $skipDeceased, $contactIDs) {
CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$formValues = $form->controller->exportValues($form->getName());
list($formValues, $categories, $html_message, $messageToken, $returnProperties) = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues);
[$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues);

$html
= self::generateHTML(
Expand Down Expand Up @@ -64,35 +66,27 @@ public static function postProcessMembers(&$form, $membershipIDs, $skipOnHold, $
* @param $html_message
* @param $categories
*
* @deprecated
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
* @deprecated
*/
public static function generateHTML($membershipIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $html_message, $categories) {
CRM_Core_Error::deprecatedFunctionWarning('no alternative');
$memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
$memberships = Membership::get(FALSE)
->addWhere('id', 'IN', $membershipIDs)
->addSelect('contact_id')->execute();
$html = [];

foreach ($membershipIDs as $membershipID) {
$membership = $memberships[$membershipID];
// get contact information
$contactId = $membership['contact_id'];
$params = ['contact_id' => $contactId];
//getTokenDetails is much like calling the api contact.get function - but - with some minor
// special handlings. It precedes the existence of the api
list($contacts) = CRM_Utils_Token::getTokenDetails(
$params,
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
'CRM_Contribution_Form_Task_PDFLetterCommon'
);

$tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $html_message, $messageToken);
$html[] = CRM_Core_BAO_MessageTemplate::renderTemplate(['messageTemplate' => ['msg_html' => $tokenHtml], 'contactId' => $contactId])['html'];

foreach ($memberships as $membership) {
$html[] = CRM_Core_BAO_MessageTemplate::renderTemplate([
'messageTemplate' => ['msg_html' => $html_message],
'contactId' => $membership['contact_id'],
'schema' => ['contactId', 'membershipId'],
'tokenContext' => ['membershipId' => $membership['id']],
'disableSmarty' => !defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY,
])['html'];
}
return $html;
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,8 @@ public static function getAnonymousTokenDetails($contactIDs = [0],
* Get Membership Token Details.
* @param array $membershipIDs
* Array of membership IDS.
*
* @deprecated
*/
public static function getMembershipTokenDetails($membershipIDs) {
$memberships = civicrm_api3('membership', 'get', [
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function testMembershipTokenReplacementInPDF(): void {
$params = array_merge($params,
[
'fee' => '100.00',
'type' => 'General',
'status' => 'New',
'membership_type_id:label' => 'General',
'status_id:label' => 'New',
]
);

Expand Down Expand Up @@ -105,8 +105,8 @@ public function testMembershipTokenReplacementInPDF(): void {
public static function getSampleHTML() {
$tokens = [
'Test Fee' => 'fee',
'Test Type' => 'type',
'Test Status' => 'status',
'Test Type' => 'membership_type_id:label',
'Test Status' => 'status_id:label',
'Test Join Date' => 'join_date',
'Test Start Date' => 'start_date',
'Test End Date' => 'end_date',
Expand Down

0 comments on commit 89d280e

Please sign in to comment.