From a95f041b799ab1828da6c0ebd9adc9b1b6c1c48f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 18 Sep 2021 10:41:39 +1200 Subject: [PATCH] dev/core#2832 Switch membership pdf to use token processor Replaces final instances of https://lab.civicrm.org/dev/core/-/issues/2832 --- CRM/Member/Form/Task/PDFLetter.php | 19 +++++---- CRM/Member/Form/Task/PDFLetterCommon.php | 42 ++++++++----------- CRM/Utils/Token.php | 2 + .../CRM/Member/Form/Task/PDFLetterTest.php | 8 ++-- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/CRM/Member/Form/Task/PDFLetter.php b/CRM/Member/Form/Task/PDFLetter.php index 4b8436ddcd..80115f9ee0 100644 --- a/CRM/Member/Form/Task/PDFLetter.php +++ b/CRM/Member/Form/Task/PDFLetter.php @@ -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. @@ -119,20 +121,19 @@ class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task { * */ 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; } diff --git a/CRM/Member/Form/Task/PDFLetterCommon.php b/CRM/Member/Form/Task/PDFLetterCommon.php index 1d36c5e806..7fdc8dce85 100644 --- a/CRM/Member/Form/Task/PDFLetterCommon.php +++ b/CRM/Member/Form/Task/PDFLetterCommon.php @@ -1,5 +1,7 @@ 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( @@ -64,35 +66,27 @@ class CRM_Member_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLett * @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; } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index c2ec7615ab..37b2c56fa3 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1276,6 +1276,8 @@ class CRM_Utils_Token { * Get Membership Token Details. * @param array $membershipIDs * Array of membership IDS. + * + * @deprecated */ public static function getMembershipTokenDetails($membershipIDs) { $memberships = civicrm_api3('membership', 'get', [ diff --git a/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php b/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php index 2f73473780..33fd6dbb7f 100644 --- a/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php +++ b/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php @@ -60,8 +60,8 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { $params = array_merge($params, [ 'fee' => '100.00', - 'type' => 'General', - 'status' => 'New', + 'membership_type_id:label' => 'General', + 'status_id:label' => 'New', ] ); @@ -105,8 +105,8 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { 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', -- 2.25.1